Resizing dataset

eyk: <p>Hi<br>
I am trying to resize my images through transforms, but I get the error: “AttributeError: ‘Resize’ object has no attribute ‘resize’”</p>
<p>Here is my code:</p>
<p>class swe_dataset(Dataset):<br>
def <strong>init</strong>(self, train, label,transform):<br>
self.x_data=train<br>
self.y_data=label<br>
self.len = len(self.x_data)<br>
self.transform=transform<br>
<span class="hashtag">#self</span>.transform= transforms.Resize((120,120))</p>
<pre><code>def __getitem__(self,index):
    sample= self.x_data[index], self.y_data[index]
    if self.transform is not None:
        sample=self.transform(sample)
        
    return sample

def __len__(self):
    return self.len
</code></pre>
<p>#######################################<br>
class resize:<br>
def <strong>call</strong>(self, sample):<br>
inputs,targets = sample<br>
inputs =transform.resize(inputs, (120, 120))<br>
return inputs,targets<br>
#######################################<br>
train_set=swe_dataset(ts[0],tls[0],transform=resize())<br>
#######################################<br>
when I try to access the sample image by: im,_=next(iter(train_set)), I get the mentioned error. Could you help me to understand what is the problem in resizing? (because when I am not using transform everything works well)</p>

ptrblck: <p>I’m not sure, if you are passing the custom <code>resize</code> class as the transformation or <code>torchvision.transforms.Resize</code>.<br>
However, <code>transform.resize(inputs, (120, 120))</code> won’t work.<br>
You could either create an instance of <code>transforms.Resize</code> or use the functional API:</p>
<pre><code class="lang-python">torchvision.transforms.functional.resize(img, size, interpolation)
</code></pre>

eyk: <p>I solved the problem, I noticed that I was not transforming to tensor after resizing…so after changing my resize class, the problem was solved:</p>
<pre><code class="lang-auto">class resize:
    def __call__(self, sample):
        inputs,targets = sample
        inputs=transforms.ToPILImage()(inputs)
        inputs =torchvision.transforms.functional.resize(inputs, (120,120), interpolation=2)
        inputs=transforms.ToTensor()(inputs)
        return inputs,targets
</code></pre>
<p>but now I have another question…<br>
why I got error when I was using transforms in my dataset class?</p>
<pre><code class="lang-auto">class swe_dataset(Dataset):
    def __init__(self, train, label,transform=None):
        self.x_data=train 
        self.y_data=label
        self.len = len(self.x_data)
        #self.transform=transform
        
        self.transform= transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize((120,120)),
            transforms.ToTensor()
        ])
</code></pre>
<p>while I was doing the same thing when I used my resize class, it worked well…<br>
why it didnt work when I used transforms in dataset class?</p>

ptrblck: <p>It should work using your <code>self.transform</code> definition.<br>
Could you post an executable code snippet to reproduce the error you are seeing so that we can have a look, please?</p>

eyk: <p>Sure, here is the code and error:</p>
<pre><code class="lang-auto">class swe_dataset(Dataset):
    def __init__(self, train, label,transform=None):
        self.x_data=train 
        self.y_data=label
        self.len = len(self.x_data)
        #self.transform=transform
        
        self.transform= transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize((120,120)),
            transforms.ToTensor()
        ])
        
    def __getitem__(self,index):
        sample= self.x_data[index], self.y_data[index]
        if self.transform is not None:
            sample=self.transform(sample)
            
        return sample
    
    def __len__(self):
        return self.len
</code></pre>
<pre><code class="lang-auto">TypeError                                 Traceback (most recent call last)
&lt;ipython-input-335-3765493ebbad&gt; in &lt;module&gt;
----&gt; 1 im,_=next(iter(train_set))

&lt;ipython-input-331-2ded7f573365&gt; in __getitem__(self, index)
     15         sample= self.x_data[index], self.y_data[index]
     16         if self.transform is not None:
---&gt; 17             sample=self.transform(sample)
     18 
     19         return sample

~/anaconda3/envs/gpu-env/lib/python3.8/site-packages/torchvision/transforms/transforms.py in __call__(self, img)
     68     def __call__(self, img):
     69         for t in self.transforms:
---&gt; 70             img = t(img)
     71         return img
     72 

~/anaconda3/envs/gpu-env/lib/python3.8/site-packages/torchvision/transforms/transforms.py in __call__(self, pic)
    134 
    135         """
--&gt; 136         return F.to_pil_image(pic, self.mode)
    137 
    138     def __repr__(self):

~/anaconda3/envs/gpu-env/lib/python3.8/site-packages/torchvision/transforms/functional.py in to_pil_image(pic, mode)
    118     """
    119     if not(isinstance(pic, torch.Tensor) or isinstance(pic, np.ndarray)):
--&gt; 120         raise TypeError('pic should be Tensor or ndarray. Got {}.'.format(type(pic)))
    121 
    122     elif isinstance(pic, torch.Tensor):

TypeError: pic should be Tensor or ndarray. Got &lt;class 'tuple'&gt;.
</code></pre>

ptrblck: <p>In your functional approach you are unpacking <code>sample</code> to <code>inputs</code> and <code>target</code>, thus passing a <code>tensor</code> to <code>resize</code>, while you are trying to apply <code>self.transform</code> on the <code>tuple</code> directly, which won’t work.<br>
Unpack <code>sample</code> or pass <code>self.x_data[index]</code> directly to <code>self.transform</code>.</p>