Error with functional image rotation

I try to rotate an image using torchvision.transforms.functional.rotate and I get this error:

  File "C:\Users\User\Documents\programming\proj\utils.py", line 55, in __call__
    image = torchvision.transforms.functional.rotate(PIL_image, angle)
  File "C:\Users\User\Anaconda3\envs\surv\lib\site-packages\torchvision\transforms\functional.py", line 729, in rotate
    return img.rotate(angle, resample, expand, center, fillcolor=fill)
  File "C:\Users\User\Anaconda3\envs\surv\lib\site-packages\PIL\Image.py", line 1915, in rotate
    fillcolor=fillcolor)
  File "C:\Users\User\Anaconda3\envs\surv\lib\site-packages\PIL\Image.py", line 2205, in transform
    im = new(self.mode, size, fillcolor)
  File "C:\Users\User\Anaconda3\envs\surv\lib\site-packages\PIL\Image.py", line 2375, in new
    return Image()._new(core.fill(mode, size, color))
TypeError: must be real number, not tuple

If I do PIL_image.rotate(angle) it works (using PIL’s rotation utility), but for some reason using pytorch’s torchvision.transforms.functional.rotate gives an error. How should I resolve this?

3 Likes

I believe this occurs when your image is mode = F (floating point) and thus only has a single channel. In this case color is expected to be a single number and not a tuple. Pytorch however sees floating point numbers and passes a tuple of one item downstream to color and hence the error from PIL. Not sure if it’s a bug or a feature.

It sounds rather like a bug.
@agt could you confirm @pford221’s assumption?

I am also facing simular issue

I’m getting a similar issue as well! My model was working perfectly a few days ago, and I don’t think I’ve changed anything, but I’m seeing the following when I try to run the model:

/usr/lib/python3/dist-packages/PIL/ImageOps.py in expand(image, border, fill)
360 width = left + image.size[0] + right
361 height = top + image.size[1] + bottom
–> 362 out = Image.new(image.mode, (width, height), _color(fill, image.mode))
363 out.paste(image, (left, top))
364 return out

/usr/lib/python3/dist-packages/PIL/Image.py in new(mode, size, color)
2542 im.palette = ImagePalette.ImagePalette()
2543 color = im.palette.getcolor(color)
-> 2544 return im._new(core.fill(mode, size, color))
2545
2546

TypeError: must be real number, not tuple

Could this be a new bug? I’m processing .tif images and each of the images are going through the following transform:

def transform_function(degrees,scale,flip_prob):

transform_list.append(transforms.RandomAffine(degrees, scale = scale))
transform_list.append(transforms.RandomHorizontalFlip(p=flip_prob))
transform_list.append(transforms.Pad(37)) #all images should be 182x182 before padding. 
transform_list.append(transforms.ToTensor())

I’m not sure but it sounds reasonable. I think I was working with 1-channel images.

That sounds strange and I guess some package was (accidentally) updated/downgraded.

Could you post an executable code snippet as well as the used PyTorch and torchvision verions?

I’ve downgraded my workspace to the following:

print(torch.__version__)
print(torchvision.__version__)
print(torch.version.cuda)

1.4.0
0.5.0
10.1

Without altering any of my code, it’s now working again! The torch and torchvision versions that are available as of October 22nd, 2020 (with corresponding CUDA version) were causing my issues, which arose from the RandomAffine(degrees, scale = scale) operation (inputs below).

degrees = 25
scale = [1,1.5]

Thanks for the version update.
I cannot reproduce it with torch==1.8.0.dev20201022 and torchvision==0.8.0.dev20201022 using:

import torch
import torchvision.transforms as transforms

degrees = 25
scale = [1, 1.5]
flip_prob = 0.5

trans = transforms.Compose([
    transforms.RandomAffine(degrees, scale = scale),
    transforms.RandomHorizontalFlip(p=flip_prob),
    transforms.Pad(37),
    transforms.ToTensor()])

img = transforms.ToPILImage()(torch.randn(3, 224, 224))
out = trans(img)
print(out.shape)

Any possibility that the issue could be with torch 1.6.x, and
img = transforms.ToPILImage()(torch.randn(1, 224, 224))?

Hi. I believe the issue was with an image that has has only 2 axes (mode = F).