TypeError: 'tuple' object is not callable

Abdelrahman_Mohamed: <pre><code class="lang-auto">class Datasetload(Dataset):
    
    def __init__(self, file_path, transform=None):
        self.data = pd.read_csv(file_path)
        self.transform = transform
        
    def __len__(self):
        return len(self.data)
    
    def __getitem__(self, index):
        image = self.data.iloc[index, 1:].values.astype(np.uint8).reshape((1, 28, 28))[:,:,:,None]
        label = self.data.iloc[index, 0]
        
        if self.transform is not None:
            image = self.transform(image)
        print(type(image))
            
        return image , label
</code></pre>
<pre><code class="lang-auto">train_dataset=Datasetload(train_dir,transform)

train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=8, shuffle=True)
</code></pre>
<pre><code class="lang-auto">images,labels=next(iter(train_loader))
</code></pre>
<pre><code class="lang-auto">---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-105-22d806a2b5c6&gt; in &lt;module&gt;()
----&gt; 1 images,labels=next(iter(train_loader))

2 frames
&lt;ipython-input-99-3070471367a5&gt; in __getitem__(self, index)
     13 
     14         if self.transform is not None:
---&gt; 15             image = self.transform(image)
     16         print(type(image))
     17 

TypeError: 'tuple' object is not callabl
</code></pre>

ptrblck: <p>Could you post the definition of your <code>transform</code>?</p>

Abdelrahman_Mohamed: <p>transform=transforms.Compose([<br>
transforms.ToPILImage(),<br>
transforms.RandomRotation(25),<br>
transforms.RandomResizedCrop(200),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.5],<br>
[0.5])<br>
]),</p>

ptrblck: <p>The trailing comma might create <code>transform</code> as a <code>tuple</code>.<br>
Could you remove it and try your code again?</p>

Roqyah_bdeen: <p>and what’s wrong with my code please, It works fine with older Pytorch versions but it raises this error with Pytorch 1.9<br>
The code</p>
<pre><code class="lang-auto">import numpy as np
import torch
from torchvision import transforms as tf
from PIL import ImageFilter


def get_ap_transforms(cfg):
    transforms = [ToPILImage()]
    if cfg.cj:
        transforms.append(ColorJitter(brightness=cfg.cj_bri,
                                      contrast=cfg.cj_con,
                                      saturation=cfg.cj_sat,
                                      hue=cfg.cj_hue))
    if cfg.gblur:
        transforms.append(RandomGaussianBlur(0.5, 3))
    transforms.append(ToTensor())
    if cfg.gamma:
        transforms.append(RandomGamma(min_gamma=0.7, max_gamma=1.5, clip_image=True))
    return tf.Compose(transforms)


# from https://github.com/visinf/irr/blob/master/datasets/transforms.py
class ToPILImage(tf.ToPILImage):
    def __call__(self, imgs):
        return [super(ToPILImage, self).__call__(im) for im in imgs]


class ColorJitter(tf.ColorJitter):
    def __call__(self, imgs):
        transform = self.get_params(self.brightness, self.contrast, self.saturation, self.hue)
        return [transform(im) for im in imgs]
</code></pre>
<p>the error</p>
<pre><code class="lang-auto">    return [transform(im) for im in imgs]
TypeError: 'tuple' object is not callable

</code></pre>

ptrblck: <p><code>ColorJitter.get_params</code> returns the order of operations as well as the parameters for each transformation, which you would then have to use in the same manner as in the <code>forward</code> method of the <a href="https://github.com/pytorch/vision/blob/8541c214740f9cfaf2e60ad1579faf6f4af1257b/torchvision/transforms/transforms.py#L1167-L1188">original implementation</a> (i.e. via the functional API).</p>

Roqyah_bdeen: <p>solved , thank you so much</p>

Roqyah_bdeen: <p>this is another issue happened when I have switched to torch 1.9 too. could you please tell me how to resolve it<br>
the code</p>
<pre><code class="lang-auto"># reshape
        v_flat = _bchw2bhwc(v).contiguous().view(-1, channels)
        torch.index_select(v_flat, dim=0, index=self._i00.view(-1), out=self._v00)
        torch.index_select(v_flat, dim=0, index=self._i01.view(-1), out=self._v01)
        torch.index_select(v_flat, dim=0, index=self._i10.view(-1), out=self._v10)
        torch.index_select(v_flat, dim=0, index=self._i11.view(-1), out=self._v11)
</code></pre>
<p>the warning</p>
<pre><code class="lang-auto">UserWarning: An output with one or more elements was resized since it had shape [638976, 2], which does not match the required output shape [1277952, 2].This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0). (Triggered internally at  /opt/conda/conda-bld/pytorch_1623448265233/work/aten/src/ATen/native/Resize.cpp:23.)
  torch.index_select(v_flat, dim=0, index=self._i00.view(-1), out=self._v00)

</code></pre>

ptrblck: <p>Could you post an executable code snippet for the new warning, i.e. tensors using random values in the right shapes, so that we could have a look, please?</p>