Error while using transforms ToPILImage and RandomResizedCrop

Hi,

I’m trying to use the transforms ToPILImage and RandomResizedCrop and run into the following errors.

For ToPILImage:


  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

For RandomResizedCrop:


  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

The code to reproduce this error for ToPILImage:

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

For RandomResizedCrop simply replace the following line:

txfm = transforms.Compose([transforms.RandomResizedCrop((80,80),ratio=(1,1))])

I’ve installed torchvision from source.

Can someone please help me out?

Hi @barrel-roll,

The reason why you got TypeError: Input type float32 is not supported is because the default transforms.ToPILImage() has mode=None and the third dimension in your synthetic data is 2 (which is usually set to 3 for color images), leading to the TypeError here https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional.py#L138. (you can follow the source code from here https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional.py#L81 where the pic you gave is (75, 75, 2) and mode is None).

One solution is to set the data shape to (75, 75, 3) and change imgs = self.transform(np.float32(imgs)) to imgs = self.transform(np.uint8(imgs)).

What the ToPILImage() does is basically to call Image.fromarray(), so you might want to have a look at https://pillow.readthedocs.io/en/5.0.0/reference/Image.html.

3 Likes

Hi @KaiyangZhou ,

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.

Did you find why RandomResizedCrop throws an error?

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

I could not reproduce the error with RandomResizedCrop.

Thanks. I’ll try the manual implementation then.

Regarding RandomResizedCrop, what’s your version of torchvision? Did you build from source? I rechecked and it’s still giving me error.

My version is 0.2.0.

Try transforms.RandomResizedCrop(80, ratio=(1,1))?

My version is the same. And nope, still the same error.

This is really weird…

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().