Trying to use the transform torchvision.transforms.resize but getting image has wrong mode error?

Vivek_Reddy: <p>Hello,</p>
<p>I am trying to use the a sequence of transforms in a dataloader of images I am loading.  The order is<br>
RandomRotation(degrees=(-30, 30), resample=False, expand=False)<br>
RandomCrop(size=(246, 246), padding=None)<br>
RandomHorizontalFlip(p=0.5)<br>
Resize(size=(128, 128), interpolation=PIL.Image.NEAREST)<br>
)</p>
<p>The problem is I want to use PIL.Image.BILINEAR for the resize.  However, for everything except NEAREST, I am getting an error that hs the following stack trace:</p>
<p>File “/mnt/Analytics/InHouseProjects/Clustering/IIC_folder/cluster/data.py”, line 70, in <strong>getitem</strong><br>
image = self.transform(image)<br>
File “/home/.local/share/virtualenvs/PyTorch-rjjGfeb_/lib/python3.7/site-packages/torchvision/transforms/transforms.py”, line 70, in <strong>call</strong><br>
img = t(img)<br>
File “/home/.local/share/virtualenvs/PyTorch-rjjGfeb_/lib/python3.7/site-packages/torchvision/transforms/transforms.py”, line 207, in <strong>call</strong><br>
return F.resize(img, self.size, self.interpolation)<br>
File “/home/.local/share/virtualenvs/PyTorch-rjjGfeb_/lib/python3.7/site-packages/torchvision/transforms/functional.py”, line 254, in resize<br>
return img.resize((ow, oh), interpolation)<br>
File “/home/.local/share/virtualenvs/PyTorch-rjjGfeb_/lib/python3.7/site-packages/PIL/Image.py”, line 1922, in resize<br>
return self._new(self.im.resize(size, resample, box))<br>
ValueError: image has wrong mode</p>
<p>On torchvision.transforms.resize but I am unable to use any kind of interpolation except nearest?  Even using it as the only transform is throwing this error.  The images as 16 bit tiff images that are cast to np.float32.<br>
Thank you!</p>

ptrblck: <p>The error is raised by <code>PIL</code>, which doesn’t seem to support the <code>BILINEAR</code> resize option for this type of image.<br>
From <a href="https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.resize">their docs</a>:</p>
<blockquote>
<p><strong>resample</strong> – An optional resampling filter. This can be one of <a href="https://pillow.readthedocs.io/en/stable/handbook/concepts.html#PIL.Image.NEAREST"><code>PIL.Image.NEAREST</code></a>, <a href="https://pillow.readthedocs.io/en/stable/handbook/concepts.html#PIL.Image.BOX"><code>PIL.Image.BOX</code></a>, <a href="https://pillow.readthedocs.io/en/stable/handbook/concepts.html#PIL.Image.BILINEAR"><code>PIL.Image.BILINEAR</code></a>, <a href="https://pillow.readthedocs.io/en/stable/handbook/concepts.html#PIL.Image.HAMMING"><code>PIL.Image.HAMMING</code></a>, <a href="https://pillow.readthedocs.io/en/stable/handbook/concepts.html#PIL.Image.BICUBIC"><code>PIL.Image.BICUBIC</code></a> or <a href="https://pillow.readthedocs.io/en/stable/handbook/concepts.html#PIL.Image.LANCZOS"><code>PIL.Image.LANCZOS</code></a>. Default filter is <a href="https://pillow.readthedocs.io/en/stable/handbook/concepts.html#PIL.Image.BICUBIC"><code>PIL.Image.BICUBIC</code></a>. If the image has mode “1” or “P”, it is always set to <a href="https://pillow.readthedocs.io/en/stable/handbook/concepts.html#PIL.Image.NEAREST"><code>PIL.Image.NEAREST</code></a>. See: <a href="https://pillow.readthedocs.io/en/stable/handbook/concepts.html#concept-filters">Filters</a>.</p>
</blockquote>