类型错误:无法处理 PIL Image 中的数据类型

Ash*_*hra 2 python numpy image-processing python-imaging-library pytorch

我有一个大小为 (4,3,224,224) 的 Pytorch 张量。当我尝试将第一个张量转换为 Image 对象时,它说:

TypeError: Cannot handle this data type
Run Code Online (Sandbox Code Playgroud)

我运行了以下命令:

img = Image.fromarray(data[0][i].numpy().astype(np.uint8))
Run Code Online (Sandbox Code Playgroud)

其中 data 是 Pytorch 张量

我尝试了其他解决方案,但找不到任何解决方案。

请建议!!

Sha*_*hai 6

您正在尝试将 3x224x224np.array转换为图像,但PIL.Image希望其图像的形状为 224x224x3,因此出现错误。
如果您转置张量以使通道维度成为最后一个(而不是第一个),则应该没有问题

img = Image.fromarray(data[0][i].transpose(0,2).numpy().astype(np.uint8))
Run Code Online (Sandbox Code Playgroud)