How can I read a image pair at the same time

for the Consumer-to-shop Clothes Retrieval dataset, it need to read a pair image at the same time, it will be okay as followed code?


img1 = read('...')

img2 = read('...')

img = [img1, img2]

thanks for replying me :slight_smile:

1 Like

I assume you would like to create a Dataset returning both images?
The easiest way would be to create an own Dataset class and use your code snippet to load the data:

class MyDataset(Dataset):
    def __init__(self, image1_paths, image2_paths, transform=None):
        self.image1_paths = image1_paths
        self.image2_paths = image2_paths
        self.transform = transform
        
    def __getitem__(self, index):
        img1 = Image.open(self.image1_paths[index])
        img2 = Image.open(self.image2_paths[index])
        
        if self.transform:
            img1 = self.transform(img1)
            img2 = self.transform(img2)
            
        return img1, img2
    
    def __len__(self):
        return len(self.image1_paths)


dataset = MyDataset(
    image1_paths, image2_paths, transform=transforms.ToTensor())

Note that you might want to use torchvision.transforms.functional, if you want to apply random transformations like RandomCrop on both images in the same way.

Also, have a look at the data loading tutorial for more information.

1 Like

Thanks for your reply. :smile:

but how to read a series images pairswise?
the file name is connected very similar
one group is like (“pp_001_cc.gipl”“pp_002_cc.gipl”…)
and the other group is (“pp_001_mm.gipl”,“pp_002_mm.gipl”…)

You could modify the read logic in __getitem__ and e.g. use a for loop to read all corresponding images.
In my current approach I’m just reading two files, but you could also read all matching file names.

2 Likes