Torchvision.transfors: How to perform identical transform on both image and target?

Alternatively to the functions from the tutorial, you could use torchvision’s functional API.
Here is a small example for an image and the corresponding mask image:

class MyDataset(Dataset):
    def __init__(self, image_paths, target_paths, train=True):
        self.image_paths = image_paths
        self.target_paths = target_paths

    def transform(self, image, mask):
        # Resize
        resize = transforms.Resize(size=(520, 520))
        image = resize(image)
        mask = resize(mask)

        # Random crop
        i, j, h, w = transforms.RandomCrop.get_params(
            image, output_size=(512, 512))
        image = TF.crop(image, i, j, h, w)
        mask = TF.crop(mask, i, j, h, w)

        # Random horizontal flipping
        if random.random() > 0.5:
            image = TF.hflip(image)
            mask = TF.hflip(mask)

        # Random vertical flipping
        if random.random() > 0.5:
            image = TF.vflip(image)
            mask = TF.vflip(mask)

        # Transform to tensor
        image = TF.to_tensor(image)
        mask = TF.to_tensor(mask)
        return image, mask

    def __getitem__(self, index):
        image = Image.open(self.image_paths[index])
        mask = Image.open(self.target_paths[index])
        x, y = self.transform(image, mask)
        return x, y

    def __len__(self):
        return len(self.image_paths)
68 Likes
Data Augmentor for 3D images
How to random crop a image tuple
How make customised dataset for semantic segmentation?
Unet doesnot work after dataaugmentation
Loss problem in net finetuning
Got cuda out of memory while implementing LeNet5
How to crop image and mask in patches?
Multiclass Segmentation
Applying equal transformation on label as on image (e.g. RandomPerspective())
Using PyTorch Transformers
Dataloader for semantic segmentation
T.Compose | TypeError: __call__() takes 2 positional arguments but 3 were given
Taget and input must have the same number of elements
Torchvision same random transforms on multi images
Using Histopathological Image Dataset with Ground Truth
How to do the same random crop on 2 images?
*Please Help: Data Loader for Image and Mask*
How to speedup the back-propagation function of AdderNet
Do the same transformations on a list of PIL Image
How to normalize a tensor to 0 mean and 1 variance?
How to do inverse transformation in the test image data
How to process two images with the same augmentation?
Image rotation or image flip and knowledge of new location of certain pixels
Same transform for a paired image?
Label and image are the same when augument data in problem reconstruct image
Data augmentation results network not learning at all
Identical Transformation on Input and Label?
Problems about Loading & Processing Multi-bands Data - pytorch
Cant apply both 2 independent transform to image and binary mask
How to make targets go through the same augmentations as the images?
How to use torch.cat to concat pictures belonging to two different folders
Transforms aren't the same for images and masks
Image data augmentation in pytorch
How to load image dataset into pytorch
Apply same transformation to CycleGAN for same patches
How do I apply same transformations to image and mask?
Adding augmentation to both image and segmentation mask
Pre-processing using torchvision.transforms.functional
About torchvision.transforms.ToTensor for segmentaion task
`torchvision` v2 transforms not applying to both image and mask