简体   繁体   English

使用 Dataloader 加载时,PyTorch 正在平铺图像

[英]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))

水平平铺人像的算法(w <h) images into 16:9 ratio landscape montages< div><div id="text_translate"><p> 我想写一个程序,将一组具有相同高度但不同宽度的纵向(即宽度&lt;高度)图像文件,将它们水平平铺成N个合成图像,这样每个合成图像都尽可能接近16:9的比例尽可能。 这甚至可能吗? 我能找到的最接近我想要的算法是<a href="https://en.wikipedia.org/wiki/Knapsack_problem" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Knapsack_problem</a> 。</p></div></h)> - Algorithm for horizontally tiling portrait (w<h) images into 16:9 ratio landscape montages

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pytorch从Dataloader加载两个图像 - Pytorch Loading two Images from Dataloader Pytorch DataLoader多个数据源 - Pytorch DataLoader multiple data source 使用 pytorch Dataloader 加载并正确显示图像数据集 - Loading and correctly displaying an image dataset using pytorch Dataloader Pytorch:“KeyError:在 DataLoader 工作进程 0 中捕获到 KeyError。” - Pytorch: "KeyError: Caught KeyError in DataLoader worker process 0." Pytorch 使用增强图像创建数据集 - Pytorch creating dataset with augmented images 水平平铺人像的算法(w <h) images into 16:9 ratio landscape montages< div><div id="text_translate"><p> 我想写一个程序,将一组具有相同高度但不同宽度的纵向(即宽度&lt;高度)图像文件,将它们水平平铺成N个合成图像,这样每个合成图像都尽可能接近16:9的比例尽可能。 这甚至可能吗? 我能找到的最接近我想要的算法是<a href="https://en.wikipedia.org/wiki/Knapsack_problem" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Knapsack_problem</a> 。</p></div></h)> - Algorithm for horizontally tiling portrait (w<h) images into 16:9 ratio landscape montages 使用PyTorch加载图像的自定义数据集 - Loading custom dataset of images using PyTorch Pytorch:在图像预测中向图像添加信息 - Pytorch: Add information to images in image prediction Pytorch - 转移学习教程中图像预处理的目的 - Pytorch - Purpose of images preprocessing in the transfer learning tutorial Pytorch/torchvision - 修改数据集 object 的图像和标签 - Pytorch/torchvision - modify images and labels of a Dataset object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM