Error in datset of PascalVoc

887574002: <p>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,</p>
<p><strong>call</strong>() takes 1 positional argument but 3 were given</p>
<p>I can’t understand where the problem is. Here is my code:</p>
<p>class myVOCSegmentation(VOCSegmentation):</p>
<pre><code>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&gt;20]=0
        
    img = to_tensor(img)
        
    target = torch.from_numpy(target).type(torch.long)
    
    return(img,target)          
</code></pre>
<p>h,w = 520,520</p>
<p>mean = [0.485, 0.456, 0.406]<br>
std = [0.229, 0.224, 0.225]</p>
<p>transform_train = Compose([Resize(h,w),<br>
HorizontalFlip(p=0.5),<br>
Normalize(mean=mean,std=std)])</p>
<p>transform_val = Compose([Resize(h,w),Normalize(mean=mean,std=std)])</p>
<p>PATH = ‘./data’</p>
<p>train_set =VOCSegmentation(PATH,year=‘2012’,download = True,image_set =‘train’,transforms=transform_train)</p>
<p>val_set = VOCSegmentation(PATH,year=‘2012’,download = True,image_set =‘val’,transforms=transform_val)</p>
<p>for item in train_set:<br>
print(item)</p>
<pre><code>break
</code></pre>
<p><strong>call</strong>() takes 1 positional argument but 3 were given</p>

harsha_g: <aside class="quote no-group" data-username="887574002" data-post="1" data-topic="87125">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/887574002/48/35835_2.png" class="avatar"> 887574002:</div>
<blockquote>
<p>VOCSegmentation</p>
</blockquote>
</aside>
<p>Without knowing how your <code>call()</code> functions looks like, it seems that it has been defined as:</p>
<pre><code class="lang-auto">def call(self):
    ...
</code></pre>
<p>However, you may be calling it somewhere as:</p>
<pre><code class="lang-auto">my_object.call(input_arg1, input_arg2)
</code></pre>
<p>which is why you see that error.</p>

887574002: <p>I do not have any call function. The error comes back to transformation, as I see in the error, this trace back:</p>
<p>if self.transforms is not None:<br>
–&gt; 127             img, target = self.transforms(img, target)<br>
128<br>
129         return img, target</p>
<p>TypeError: <strong>call</strong>() takes 1 positional argument but 3 were given</p>

harsha_g: <aside class="quote no-group" data-username="887574002" data-post="1" data-topic="87125">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/887574002/48/35835_2.png" class="avatar"> 887574002:</div>
<blockquote>
<p><code>self.transforms(image= np.array(img),mask = np.array(target))</code></p>
</blockquote>
</aside>
<p>Applying the same reasoning to <code>transforms()</code>, it doesn’t take any input arguments but you gave two inputs and hence the error.</p>

887574002: <p>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 <code>https://pytorch.org/docs/stable/_modules/torchvision/datasets/voc.html#VOCSegmentation</code>, they also apply transformations on two arguments</p>

harsha_g: <aside class="quote no-group" data-username="887574002" data-post="1" data-topic="87125">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/887574002/48/35835_2.png" class="avatar"> 887574002:</div>
<blockquote>
<p>class myVOCSegmentation(VOCSegmentation):</p>
</blockquote>
</aside>
<p>OK then, change <code>class myVOCSegmentation(VOCSegmentation):</code> to <code>class myVOCSegmentation(VisionDataset):</code>. Your error will be taken care of.</p>

887574002: <p>I did that. Another problem appeared. It doesn’t know <code>VisionDataset</code> and when I imported<br>
<code>from .vision import VisionDataset</code>,<br>
I got this error</p>
<p>No module named ‘<strong>main</strong>.vision’; ‘<strong>main</strong>’ is not a package</p>

harsha_g: <aside class="quote no-group" data-username="887574002" data-post="7" data-topic="87125">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/887574002/48/35835_2.png" class="avatar"> 887574002:</div>
<blockquote>
<p>from .vision import VisionDatase</p>
</blockquote>
</aside>
<p>Try <code>from torchvision.datasets import VisionDataset</code> and if that doesn’t work try <code>from torchvision.datasets.voc import VisionDataset</code></p>

887574002: <p>I did. The class is known now but the previous problem of</p>
<p>__ call __() takes 1 positional argument but 3 were given</p>
<p>came back!</p>

harsha_g: <p>I realized it should be <code>torchvision.datasets.vision import VisionDataset</code>. I hope that had been corrected.</p>

887574002: <p>I found the problem. I had made train_set on VOCSegmentation class rathet than myVOCSegmentation.</p>
<p>train_set =VOCSegmentation(PATH,year=‘2012’,download = True,image_set =‘train’,transforms=transform_train)</p>
<p>I corrected it and now it is working.</p>

harsha_g: <p>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!</p>

887574002: <p>Ok, sure. It was something that I wanted to ask.  Thanks.</p>