Error in datset of PascalVoc

Hi, I am trying to practice semantic segmentation using Pascal Voc. I wrote a subclass of VOCSegmentation, and I defined transformation. When I try to read one element of train_set, I got the following error,

call() takes 1 positional argument but 3 were given

I can’t understand where the problem is. Here is my code:

class myVOCSegmentation(VOCSegmentation):

def __getitem__(self,index):
    
    img = Image.open(self.images[index].convert('RGB'))
    
    target = Image.open(self.masks[index])
    
    if self.transforms is not None:

        
        agumented = self.transforms(image= np.array(img),mask = np.array(target))
        
        img = agumented['image']
        
        target = agumented['mask']
        
        # here we have 20 classes, so we set data larger than 20 equal to 0
        target[target>20]=0
        
    img = to_tensor(img)
        
    target = torch.from_numpy(target).type(torch.long)
    
    return(img,target)          

h,w = 520,520

mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]

transform_train = Compose([Resize(h,w),
HorizontalFlip(p=0.5),
Normalize(mean=mean,std=std)])

transform_val = Compose([Resize(h,w),Normalize(mean=mean,std=std)])

PATH = ‘./data’

train_set =VOCSegmentation(PATH,year=‘2012’,download = True,image_set =‘train’,transforms=transform_train)

val_set = VOCSegmentation(PATH,year=‘2012’,download = True,image_set =‘val’,transforms=transform_val)

for item in train_set:
print(item)

break

call() takes 1 positional argument but 3 were given

Without knowing how your call() functions looks like, it seems that it has been defined as:

def call(self):
    ...

However, you may be calling it somewhere as:

my_object.call(input_arg1, input_arg2)

which is why you see that error.

I do not have any call function. The error comes back to transformation, as I see in the error, this trace back:

if self.transforms is not None:
–> 127 img, target = self.transforms(img, target)
128
129 return img, target

TypeError: call() takes 1 positional argument but 3 were given

Applying the same reasoning to transforms(), it doesn’t take any input arguments but you gave two inputs and hence the error.

But transformations are callable and take in an PIL image and return a transformed version. The definition of this subclass is based on pytorch doc here https://pytorch.org/docs/stable/_modules/torchvision/datasets/voc.html#VOCSegmentation, they also apply transformations on two arguments

OK then, change class myVOCSegmentation(VOCSegmentation): to class myVOCSegmentation(VisionDataset):. Your error will be taken care of.

I did that. Another problem appeared. It doesn’t know VisionDataset and when I imported
from .vision import VisionDataset,
I got this error

No module named ‘main.vision’; ‘main’ is not a package

Try from torchvision.datasets import VisionDataset and if that doesn’t work try from torchvision.datasets.voc import VisionDataset

I did. The class is known now but the previous problem of

__ call __() takes 1 positional argument but 3 were given

came back!

I realized it should be torchvision.datasets.vision import VisionDataset. I hope that had been corrected.

I found the problem. I had made train_set on VOCSegmentation class rathet than myVOCSegmentation.

train_set =VOCSegmentation(PATH,year=‘2012’,download = True,image_set =‘train’,transforms=transform_train)

I corrected it and now it is working.

Great. In the future please wrap your code between a pair of 3 backticks ``` as that will make reading and debugging your code easier. Good Luck!

Ok, sure. It was something that I wanted to ask. Thanks.