Transformations when Dataset returns multiple images

My custom Dataset returns 3 objects: 2 images (as a dictionary) and the label (the same for both images):

def __getitem__(self, idx):
    ...
    return {'x1': img1, 'x2': img2}, label

I’m wondering whether the transformations I have defined are applied to both images:

transform_train = transforms.Compose([
    transforms.RandomRotation(degrees=30), 
    transforms.RandomVerticalFlip(), 
    transforms.RandomHorizontalFlip(), 
    transforms.ToTensor(), 
    transforms.Normalize(means, stds)
])

Is this the case or do I have to modify them?

The class of transforms should be a arguments passed to your custom Dataset.
Then in the func of getitem(), you should apply transforms to each image.
see tutorials here: https://pytorch.org/tutorials/beginner/data_loading_tutorial.html#dataset-class

1 Like

Thank you, I understand. I forgot to apply the transformations in my custom Dataset class…
But now I have another problem. My images have more than 4 channels, and when I apply ToPILImage, the output has only 3 channels. Is it possible to have a PIL image with the original number of channels?

For more details.
Here’s how I create a tensor with 6 channels in my custom Dataset class:

# Iterate over the files corresponding to each channel of 1 image
for path in paths:
    path = self.path_imgs+path

    with pil.Image.open(path) as img:
        # Append channel
        channels.append(tv.transforms.ToTensor()(img))

if self.transform:
    to_append = self.transform(t.cat(channels, dim = 0))

Then my transformation is:

transform_train = transforms.Compose([
    transforms.ToPILImage(), 
    transforms.RandomRotation(degrees=30), 
    transforms.RandomVerticalFlip(), 
    transforms.RandomHorizontalFlip(), 
    transforms.ToTensor(), 
    transforms.Normalize(means, stds)
])