我正在嘗試測驗transforms.Resize當我發現一個令人困惑的點時的原理。當我運行以下代碼時:
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()
出現錯誤:
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
但是,當我改變張量的大小時,問題就解決了:
tim = np.array([[[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]]) # (1, 3, 3)
我想知道為什么會發生這種情況,因為錯誤描述與大小無關,而與型別無關。如果有人對原因有任何想法,請告訴我,謝謝您的時間!
uj5u.com熱心網友回復:
將資料型別更改為浮動...
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)
確保您知道輸入資料是什么。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/492163.html
