如何从图像列表 Pytorch 开始加载图像数据集 [英] How to load a dataset of images starting from list of images Pytorch
本文介绍了如何从图像列表 Pytorch 开始加载图像数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个服务从另一个服务接收二进制格式的图像(我们称之为服务 B):
I have a service that receives images in a binary format from another service (let's call it service B):
from PIL import Image
img_list = []
img_bin = get_image_from_service_B()
image = Image.open(io.BytesIO(img_bin)) # Convert bytes to image using PIL
<小时>当图像通过 PIL 成功转换时,它也会附加到图像列表中.
When an image is successfully converted thanks to PIL it is also appended to a list of images.
img_list.append(image)
<小时>
当我有足够的图像时,我想使用 Pytorch 加载我的图像列表,就像它是一个数据集
When I've enough images I want to load my list of images using Pytorch as if it was a dataset
if img_list.__len__() == 500:
### Load dataset and do a transform operation on the data
<小时>在以前版本的软件中,要求只是从文件夹中检索图像,因此加载所有图像非常简单
In a previous version of the software the requirement was simply to retrieve the images from a folder, so it was quite simple to load all the images
my_dataset = datasets.ImageFolder("path/to/images/folder/", transform=transform)
dataset_iterator = DataLoader(my_dataset, batch_size=1)
现在我的问题是如何执行转换并从列表中加载数据集.
Now my issue is how to perform the transform and load the dataset from a list.
推荐答案
您可以简单地编写自定义数据集:
You can simply write a custom dataset:
class MyDataset(torch.util.data.Dataset):
def __init__(self, img_list, augmentations):
super(MyDataset, self).__init__()
self.img_list = img_list
self.augmentations = augmentations
def __len__(self):
return len(self.img_list)
def __getitem__(self, idx):
img = self.img_list[idx]
return self.augmentations(img)
您现在可以将此自定义数据集插入 DataLoader
并完成.
You can now plug this custom dataset into DataLoader
and you are done.
这篇关于如何从图像列表 Pytorch 开始加载图像数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文