简体   繁体   English

Pytorch从Dataloader加载两个图像

[英]Pytorch Loading two Images from Dataloader

I'm trying to make a GAN which takes a lo-res image, and tries to create a hi-res image from it. 我正在尝试制作可拍摄低分辨率图像的GAN,并尝试从中创建高分辨率图像。 To do this, I need to user a Dataloader which has both the hi-res and low-res training images stored in it. 为此,我需要使用一个Dataloader,其中同时存储了高分辨率和低分辨率的训练图像。

     data_transform = transforms.Compose([transforms.Resize(imageSize),
                                         transforms.Grayscale(num_output_channels=1),
                                         transforms.ToTensor()])

    dataset_hi = "./hi-res-train"
    dataset_lo = "./low-res-train"

    img_data_hi = dset.ImageFolder(root=dataset_hi,transform=data_transform)
    img_data_lo = dset.ImageFolder(root=dataset_lo,transform=data_transform)

    dataloader_hi = torch.utils.data.DataLoader(img_data_hi, batch_size = batchSize, shuffle = True, num_workers = 2) 
    dataloader_lo = torch.utils.data.DataLoader(img_data_lo, batch_size = batchSize, shuffle = True, num_workers = 2) 

I've tried using two separate data loaders (shown above) but when they are shuffled, I cant enumerate through them both because the hi-res and low-res images are not matched up. 我尝试使用两个单独的数据加载器(如上所示),但是将它们混洗后,由于高分辨图像和低分辨率图像不匹配,因此无法枚举它们。 How can I make it so I can enumerate and shuffle both with pytorch? 我该如何做才能用pytorch枚举和混洗?

Assuming you have similar names for hi & low resolution images (say img01_hi & img01_low), one option is to create a custom Dataloader that returns both images by overriding __getitem__ method. 假设您为高分辨率图像和低分辨率图像使用相似的名称(例如img01_hi和img01_low),一种选择是创建一个自定义的Dataloader,该方法通过覆盖__getitem__方法返回两个图像。

As both images are returned in one call, you can make sure they match by appending _hi & _low to the filename. 在一次调用中返回两个图像时,您可以通过在文件名后附加_hi和_low来确保它们匹配。

You may need to create a "cue" text file containing list of all your image file names to make sure you are processing each image file only once. 您可能需要创建一个“提示”文本文件,其中包含所有图像文件名的列表,以确保每个图像文件仅处理一次。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM