For segmentation how to perform data augmentation in Pytorch?

I am using PyTorch for semantic segmentation, But I am facing a problem, because I am use images , and their masks/labels . I want to perform data augmentation such as RandomHorizontalFlip, and RandomCrop, etc.

Here is my code, please check and let me know, how I can embed the following operations in the provided code.

import torchvision.transforms.functional as F

class ToTensor(object):
def call(self, sample):
image, label = sample[‘image’], sample[‘label’]
return {‘image’: F.to_tensor(image), ‘label’: F.to_tensor(label)}

my_transform = transforms.Compose([ ToTensor() ])

dataset = Mydataset(image_dir, label_dir, transform = my_transform)

Print dataset output

dataset[1]

Output

{‘image’: tensor([[[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
…,
[0.0902, 0.0902, 0.0902, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0745, 0.0745, 0.0745, …, 0.0824, 0.0824, 0.0824]],

     [[0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
      [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
      [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
      ...,
      [0.0902, 0.0902, 0.0902,  ..., 0.0824, 0.0824, 0.0824],
      [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
      [0.0745, 0.0745, 0.0745,  ..., 0.0824, 0.0824, 0.0824]],

     [[0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
      [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
      [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
      ...,
      [0.0902, 0.0902, 0.0902,  ..., 0.0824, 0.0824, 0.0824],
      [0.0824, 0.0824, 0.0824,  ..., 0.0824, 0.0824, 0.0824],
      [0.0745, 0.0745, 0.0745,  ..., 0.0824, 0.0824, 0.0824]]]),

‘label’: tensor([[[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
…,
[0.0902, 0.0902, 0.0902, …, 0.0824, 0.0824, 0.0824],
[0.0824, 0.0824, 0.0824, …, 0.0824, 0.0824, 0.0824],
[0.0745, 0.0745, 0.0745, …, 0.0824, 0.0824, 0.0824]]])}

Please someone answer :frowning:

Hy there, One way to perform transformation on both data and label is to write your own custom Dataset Class.


class CustomDataset(Dataset):
    def __init__(self , tensors , transform = None ):
        assert (tensor.size(0) == tensors[0].size(0) for tensor in tensors)
        self.tensors = tensors
        self.transform = transform
    def __getitem__(self,index):
        x = self.tensors[0][index]  
        y = self.tensors[1][index]
        if self.transform:
            x = self.transform(x) 
            y = self.transform(y)      
        return x , y

    def __len__(self):
        return len(self.tensors[0])  

# Flip tensor horizontally
def hflip(tensor):
    tensor = tensor.flip(2)
    return tensor

traindataset = CustomTensorDataset(tensors=(X_train, y_train), transform=vflip)

Hi. You see here use case transform image argumentation

Sample code

Example transform funtions