问题描述
我有一个大小为(4,3,224,224)的Pytorch张量.当我尝试将第一个张量转换为Image对象时,它说:
I have a Pytorch tensor of size (4,3,224,224). When I am trying to convert the first tensor into an Image object, it says:
TypeError: Cannot handle this data type
我运行了以下命令:
img = Image.fromarray(data[0][i].numpy().astype(np.uint8))
其中数据是Pytorch张量
where data is the Pytorch tensor
我尝试了其他解决方案,但找不到任何解决方案.
I tried other solutions but couldn't find any solution.
请提出建议!
推荐答案
您正在尝试将3x224x224 np.array
转换为图像,但是PIL.Image
期望其图像的形状为224x224x3,以免出现错误. br>如果对张量进行转置,以使通道尺寸成为最后一个(而不是第一个),那么应该没问题
You are trying to convert 3x224x224 np.array
into an image, but PIL.Image
expects its images to be of shape 224x224x3, threfore you get an error.
If you transpose your tensor so that the channel dimension will be the last (rather than the first), you should have no problem
img = Image.fromarray(data[0][i].transpose(0,2).numpy().astype(np.uint8))
这篇关于TypeError:无法处理PIL映像中的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!