Error with functional image rotation

agt: <p>I try to rotate an image using <code>torchvision.transforms.functional.rotate</code> and I get this error:</p>
<pre><code class="lang-auto">  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
</code></pre>
<p>If I do <code>PIL_image.rotate(angle)</code> it works (using PIL’s rotation utility), but for some reason using pytorch’s <code>torchvision.transforms.functional.rotate</code> gives an error. How should I resolve this?</p>

pford221: <p>I believe this occurs when your image is <code>mode = F</code> (floating point) and thus only has a single channel. In this case <code>color</code> 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 <code>color</code> and hence the error from PIL. Not sure if it’s a bug or a feature.</p>

ptrblck: <p>It sounds rather like a bug.<br>
<a class="mention" href="/u/agt">@agt</a> could you confirm <a class="mention" href="/u/pford221">@pford221</a>’s assumption?</p>

Rahul_Kumar1: <p>I am also facing simular issue</p>

mccrinbc: <p>I’m getting a similar issue as well! My model was working <strong>perfectly</strong> 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:</p>
<p>/usr/lib/python3/dist-packages/PIL/ImageOps.py in expand(image, border, fill)<br>
360     width = left + image.size[0] + right<br>
361     height = top + image.size[1] + bottom<br>
–&gt; 362     out = Image.new(image.mode, (width, height), _color(fill, image.mode))<br>
363     out.paste(image, (left, top))<br>
364     return out</p>
<p>/usr/lib/python3/dist-packages/PIL/Image.py in new(mode, size, color)<br>
2542         im.palette = ImagePalette.ImagePalette()<br>
2543         color = im.palette.getcolor(color)<br>
-&gt; <strong>2544     return im._new(core.fill(mode, size, color))</strong><br>
2545<br>
2546</p>
<p><strong>TypeError: must be real number, not tuple</strong></p>
<p>Could this be a new bug? I’m processing .tif images and each of the images are going through the following transform:</p>
<p>def transform_function(degrees,scale,flip_prob):</p>
<pre><code>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())</code></pre>

agt: <p>I’m not sure but it sounds reasonable. I think I was working with 1-channel images.</p>

ptrblck: <aside class="quote no-group" data-username="mccrinbc" data-post="5" data-topic="73366">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/9d8465/48.png" class="avatar"> mccrinbc:</div>
<blockquote>
<p>My model was working <strong>perfectly</strong> a few days ago, and I don’t think I’ve changed anything</p>
</blockquote>
</aside>
<p>That sounds strange and I guess some package was (accidentally) updated/downgraded.</p>
<p>Could you post an executable code snippet as well as the used PyTorch and <code>torchvision</code> verions?</p>

mccrinbc: <p>I’ve downgraded my workspace to the following:</p>
<pre><code class="lang-auto">print(torch.__version__)
print(torchvision.__version__)
print(torch.version.cuda)

1.4.0
0.5.0
10.1
</code></pre>
<p>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).</p>
<p>degrees = 25<br>
scale = [1,1.5]</p>

ptrblck: <p>Thanks for the version update.<br>
I cannot reproduce it with <code>torch==1.8.0.dev20201022</code> and <code>torchvision==0.8.0.dev20201022</code> using:</p>
<pre><code class="lang-python">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)
</code></pre>

mccrinbc: <p>Any possibility that the issue could be with torch 1.6.x, and<br>
img = transforms.ToPILImage()(torch.randn(<strong>1</strong>, 224, 224))?</p>

pford221: <p>Hi. I believe the issue was with an image that has has only 2 axes (mode = F).</p>