python - 使用 Matplotlib 将 Pytorch 张量显示为图像
问题描述
我正在尝试显示存储为 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 网格,始终以灰度显示。例如:
如何解决此问题,以便正确显示单色图像?
解决方案
这很奇怪。尝试通过排列而不是重塑将通道放在最后:
image.permute(1, 2, 0)
推荐阅读
- java - 如何使用 ActionListener 更改 JPanel 的颜色
- asp.net - 如何为 Asp.Net Core 3.1 中的所有路由 URL 添加动态前缀?
- java - 反转偶数位置词的逻辑
- laravel - laravel/elequent - 模型和关系
- azure - 如果复制列表中的服务器被禁用和删除,对本地服务器故障转移后正在运行的 VM 是否有任何影响?
- mongodb - 容器之间的通信在 docker 网络中不起作用
- c++ - 如何从 long long 转换为 DRAWITEMSTRUCT*
- python - 将参数传递给列表推导中的函数
- json - 有没有办法将 {"number1":5L,"number2":5L} 反序列化为具有长字段的类?
- javascript - 在 Chrome 上替代 msSaveOrOpenBlob