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 "C:/Users/Tim/Desktop/U-Net/test.py", line 62, in <module>
mask = tf(tim)
File "C:\Users\Tim\.conda\envs\Segment\lib\site-packages\torchvision\transforms\transforms.py", line 95, in __call__
img = t(img)
File "C:\Users\Tim\.conda\envs\Segment\lib\site-packages\torchvision\transforms\transforms.py", line 227, in __call__
return F.to_pil_image(pic, self.mode)
File "C:\Users\Tim\.conda\envs\Segment\lib\site-packages\torchvision\transforms\functional.py", line 315, in to_pil_image
raise TypeError(f"Input type {npimg.dtype} is not supported")
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!
CodePudding user response:
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.