Error while using transforms ToPILImage and RandomResizedCrop

username: barrel-roll
cooked: <p>Hi,</p>
<p>I’m trying to use the transforms ToPILImage and RandomResizedCrop and run into the following errors.</p>
<p>For ToPILImage:</p>
<pre><code class="lang-auto">
  File "/home/usr/anaconda3/lib/python3.6/site-packages/torchvision-0.2.0-py3.6.egg/torchvision/transforms/functional.py", line 139, in to_pil_image
    raise TypeError('Input type {} is not supported'.format(npimg.dtype))

TypeError: Input type float32 is not supported
</code></pre>
<p>For RandomResizedCrop:</p>
<pre><code class="lang-auto">
  File "/home/usr/anaconda3/lib/python3.6/site-packages/torchvision-0.2.0-py3.6.egg/torchvision/transforms/transforms.py", line 396, in get_params
    area = img.size[0] * img.size[1]

TypeError: 'int' object is not subscriptable
</code></pre>
<p>The code to reproduce this error for ToPILImage:</p>
<pre><code class="lang-auto">import numpy as np
import torch
from torchvision import transforms
from torch.utils.data import DataLoader, Dataset

class given_data(Dataset):

    def __init__(self,images,labels, transform = None):
        self.images = images
        self.labels = labels
        self.transform = transform
    
    def __len__(self):
        return len(self.labels)
    
    def __getitem__(self,indx):
        imgs = self.images[indx]
        labls = self.labels[indx]
        
        if self.transform:
            imgs = self.transform(np.float32(imgs))
        return (imgs,labls)
    
X_inp = np.random.rand(100,75,75,2)
Y_inp = np.random.rand(100)


txfm = transforms.Compose([transforms.ToPILImage()])

transformed_dataset = given_data(X_inp,Y_inp,transform= txfm)

print(transformed_dataset[2])
</code></pre>
<p>For RandomResizedCrop simply replace the following line:</p>
<pre><code class="lang-auto">txfm = transforms.Compose([transforms.RandomResizedCrop((80,80),ratio=(1,1))])

</code></pre>
<p>I’ve installed torchvision from source.</p>
<p>Can someone please help me out?</p>

username: KaiyangZhou
cooked: <p>Hi <a class="mention" href="/u/barrel-roll">@barrel-roll</a>,</p>
<p>The reason why you got <code>TypeError: Input type float32 is not supported</code> is because the default <code>transforms.ToPILImage()</code> has <code>mode=None</code> and the third dimension in your synthetic data is 2 (which is usually set to 3 for color images), leading to the <code>TypeError</code> here <a href="https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional.py#L138" rel="nofollow noopener">https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional.py#L138</a>. (you can follow the source code from here <a href="https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional.py#L81" rel="nofollow noopener">https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional.py#L81</a> where the pic you gave is (75, 75, 2) and mode is None).</p>
<p>One solution is to set the data shape to (75, 75, 3) and change <code>imgs = self.transform(np.float32(imgs))</code> to <code>imgs = self.transform(np.uint8(imgs))</code>.</p>
<p>What the <code>ToPILImage()</code> does is basically to call <code>Image.fromarray()</code>, so you might want to have a look at <a href="https://pillow.readthedocs.io/en/5.0.0/reference/Image.html" rel="nofollow noopener">https://pillow.readthedocs.io/en/5.0.0/reference/Image.html</a>.</p>

username: barrel-roll
cooked: <p>Hi <a class="mention" href="/u/kaiyangzhou">@KaiyangZhou</a> ,</p>
<p>Thanks, that does explain why it ToPILImage didn’t work. Unfortunately I’m dealing with Radar data for the Iceberg challenge hence, so neither can I convert it to a 3 channel image nor have uint8 format.</p>
<p>Did you find why RandomResizedCrop throws an error?</p>

username: KaiyangZhou
cooked: <p>You can directly transform Radar data into torch tensor, but you need to perform operations (e.g. resize) beforehand (i.e. implement your operations manually).</p>
<p>I could not reproduce the error with RandomResizedCrop.</p>

username: barrel-roll
cooked: <p>Thanks. I’ll try the manual implementation then.</p>
<p>Regarding RandomResizedCrop, what’s your version of torchvision? Did you build from source? I rechecked and it’s still giving me error.</p>

username: KaiyangZhou
cooked: <p>My version is 0.2.0.</p>
<p>Try <code>transforms.RandomResizedCrop(80, ratio=(1,1))</code>?</p>

username: barrel-roll
cooked: <p>My version is the same. And nope, still the same error.</p>
<p>This is really weird…</p>

username: jionie
cooked: <p>I also faced this problem, in my case I read image from cv2, then I used transforms.RandomResziedCro()p. I solved this by adding transforms.ToPILImage() before transforms.RandomResziedCrop().</p>