TypeError: 'tuple' object is not callable

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
train_dataset=Datasetload(train_dir,transform)

train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=8, shuffle=True)
images,labels=next(iter(train_loader))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-105-22d806a2b5c6> in <module>()
----> 1 images,labels=next(iter(train_loader))

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

TypeError: 'tuple' object is not callabl

Could you post the definition of your transform?

1 Like

transform=transforms.Compose([
transforms.ToPILImage(),
transforms.RandomRotation(25),
transforms.RandomResizedCrop(200),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.5],
[0.5])
]),

The trailing comma might create transform as a tuple.
Could you remove it and try your code again?

3 Likes

and what’s wrong with my code please, It works fine with older Pytorch versions but it raises this error with Pytorch 1.9
The code

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]

the error

    return [transform(im) for im in imgs]
TypeError: 'tuple' object is not callable

ColorJitter.get_params 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 forward method of the original implementation (i.e. via the functional API).

1 Like

solved , thank you so much

this is another issue happened when I have switched to torch 1.9 too. could you please tell me how to resolve it
the code

# 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)

the warning

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)

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?