[英]PyTorch is tiling images when loaded with Dataloader
I am trying to load an Images Dataset using the PyTorch dataloader, but the resulting transformations are tiled, and don't have the original images cropped to the center as I am expecting them.我正在尝试使用 PyTorch 数据加载器加载图像数据集,但生成的转换是平铺的,并且没有像我期望的那样将原始图像裁剪到中心。
transform = transforms.Compose([transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor()])
dataset = datasets.ImageFolder('ml-models/downloads/', transform=transform)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=32, shuffle=True)
images, labels = next(iter(dataloader))
import matplotlib.pyplot as plt
plt.imshow(images[6].reshape(224, 224, 3))
The resulting image is tiled, and not center cropped.[![as shown in the Jupyter snapshot here][1]][1]生成的图像是平铺的,而不是中心裁剪的。[![如这里的 Jupyter 快照所示][1]][1]
Is there something wrong in the provided transformation?提供的转换有问题吗? (Image shown below on link: ) [1]: https://i.stack.imgur.com/HtrIa.png
(链接如下图所示:)[1]: https : //i.stack.imgur.com/HtrIa.png
Pytorch stores tensors in channel-first format, so a 3 channel image is a tensor of shape (3, H, W). Pytorch 以通道优先格式存储张量,因此 3 通道图像是形状为 (3, H, W) 的张量。 Matplotlib expects data to be in channel-last format ie (H, W, 3).
Matplotlib 期望数据采用通道最后的格式,即 (H, W, 3)。 Reshaping does not rearrange the dimensions, for that you need Tensor.permute .
重塑不会重新排列尺寸,为此您需要Tensor.permute 。
plt.imshow(images[6].permute(1, 2, 0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.