我有一个形状为3,256,256
的PyTorch张量,其中3是通道数,图像尺寸是256,所有浮点值。
我正在尝试将其输入到网络中,并使用PIL进行一些转换。为此,我这样做:
img = Image.fromarray((255*imgs[i]).numpy().astype(np.uint8))
但我得到了:
TypeError: Cannot handle this data type: (1, 1, 256), |u1
但是,当我检查(255*imgs[i]).numpy().astype(np.uint8)
的输出时,我确实看到了,例如:
[[[ 62 57 59 ... 63 46 36]
[ 72 71 67 ... 80 76 82]
[ 58 63 63 ... 145 152 169]
...
[238 240 243 ... 7 7 7]
[241 239 240 ... 5 5 6]
[241 243 242 ... 4 3 5]]
[[ 83 78 80 ... 86 70 61]
[ 91 90 85 ... 95 93 98]
[ 80 83 80 ... 141 150 168]
...
[176 178 181 ... 14 14 14]
[177 176 178 ... 15 15 17]
[179 180 180 ... 13 13 15]]
[[147 141 143 ... 150 136 128]
[147 149 148 ... 154 149 154]
[141 149 148 ... 178 182 196]
...
[129 131 134 ... 43 43 43]
[130 130 131 ... 45 45 47]
[133 134 133 ... 44 44 46]]]
从长远来看,我不是图像专家,现在我正在努力解决这个问题。
发布于 2020-05-20 19:57:14
您需要匹配维度的顺序,而不仅仅是dtype
。PIL.Image
希望其RGB图像的形状是h
xw
x3 -通道维度最后,而pytorch更喜欢将图像表示为3xh
xw
-通道维度。
因此,您需要:
img = Image.fromarray((255*imgs[i]).numpy().astype(np.uint8).transpose(1, 2, 0))
https://stackoverflow.com/questions/61912083
复制相似问题