Change of Tensor Dimension Cause an Error
I'm trying to test the principle of transforms.Resize while I find a confusing point. When I run the below codes:
import numpy as np
import torch
from torchvision import transforms

tim = np.array([[[1, 2, 3],
                 [1, 2, 3],
                 [1, 2, 3]],
                [[1, 2, 3],
                 [1, 2, 3],
                 [1, 2, 3]]]) # (2, 3, 3)

tim = torch.from_numpy(tim)

tf = transforms.Compose([  # Principle?
     transforms.ToPILImage(),
     transforms.Resize((6, 6)), # HW
     transforms.ToTensor()
])

mask = tf(tim)
squ = mask.squeeze()

A bug occurs:
Traceback (most recent call last):
  File &quot;C:/Users/Tim/Desktop/U-Net/test.py&quot;, line 62, in &lt;module&gt;
    mask = tf(tim)
  File &quot;C:\Users\Tim\.conda\envs\Segment\lib\site-packages\torchvision\transforms\transforms.py&quot;, line 95, in __call__
    img = t(img)
  File &quot;C:\Users\Tim\.conda\envs\Segment\lib\site-packages\torchvision\transforms\transforms.py&quot;, line 227, in __call__
    return F.to_pil_image(pic, self.mode)
  File &quot;C:\Users\Tim\.conda\envs\Segment\lib\site-packages\torchvision\transforms\functional.py&quot;, line 315, in to_pil_image
    raise TypeError(f&quot;Input type {npimg.dtype} is not supported&quot;)
TypeError: Input type int32 is not supported

However, when I change the size of the tensor, the problem is solved:
tim = np.array([[[1, 2, 3],
                 [1, 2, 3],
                 [1, 2, 3]]]) # (1, 3, 3)

I’m wondering why this happens as the bug description has nothing to do with size but type. If anyone has any ideas on the reason, please let me know, thanks for your time!

Change the datatype to float ...
tim = np.array([[[1, 2, 3],
                 [1, 2, 3],
                 [1, 2, 3]],
                [[1, 2, 3],
                 [1, 2, 3],
                 [1, 2, 3]]], dtype=np.float32) # (2, 3, 3)

Make sure you know what the input data is.
