【问题标题】:Display Pytorch tensor as image using Matplotlib使用 Matplotlib 将 Pytorch 张量显示为图像
【发布时间】:2020-10-13 21:28:07
【问题描述】:

我正在尝试显示存储为 pytorch 张量的图像。

trainset = datasets.ImageFolder('data/Cat_Dog_data/train/', transform=transforms)
trainload = torch.utils.data.DataLoader(trainset, batch_size=32, shuffle=True)

images, labels = iter(trainload).next()
image = images[0]
image.shape 

>>> torch.Size([3, 224, 224]) # pyplot doesn't like this, so reshape

image = image.reshape(224,224,3)
plt.imshow(image.numpy())

此方法显示同一图像的 3 x 3 网格,始终以灰度显示。例如:

如何解决这个问题,以便正确显示单色图像?

【问题讨论】:

    标签: python matplotlib machine-learning pytorch


    【解决方案1】:

    这很奇怪。尝试通过排列而不是重塑将频道放在最后:

    image.permute(1, 2, 0)
    

    【讨论】:

      猜你喜欢
      • 2021-02-19
      • 2018-04-27
      • 1970-01-01
      • 2011-04-18
      • 2022-10-17
      • 2022-06-29
      • 2021-06-29
      相关资源
      最近更新 更多