Search code examples
tensorflowdeep-learningtensortorchvisiondata-augmentation

How to perform the same crop augmentation on both the image and its corresponding mask?


My code is:

randomScale = random.uniform(0.08, 1.0)
        CPtransform = transforms.RandomResizedCrop((self.height, self.width), scale=(randomScale, randomScale), ratio=(1,1), interpolation=2)
        toImage = T.ToPILImage()
        padImage= CPtransform(toImage(image).convert("L"))
        padMask = CPtransform(toImage(mask).convert("L"))
        return TF.to_tensor(padImage), TF.to_tensor(padMask)

But the mask are not correspond to its image after augmentation as the graph shows. The function that I used on them are all the same but the result are different.

enter image description here

Can anybody help? Thanks!


Solution

  • You can concat image and mask and convert it to a single tensor and do the transformation.

    image = T.PILToTensor()(pil_image)
    mask = T.PILToTensor()(pil_mask)
    
    # concatenate the images and apply transform:
    both_images = torch.cat((image, mask),0)
    
    # Apply the transformations to both images simultaneously
    transformed_images = CPtransform(both_images)
    
    #get transformed image and mask separately
    image_trans = transformed_images[:image.shape[0]]
    mask_trans = transformed_images[image.shape[0]:]