-2

What is the best way to execute data augmentation in Pytorch?

I created a regression UNet to predict a cell service coverage map. I want to add data augmentation. I already have the dataset and transforms complete (pasted below), but I don't know how to productively augment data in pytorch, since it seems like pytorch replaces the originals with the transforms. My thinking is: if you only augment the data before you train and lose the originals, doesn't it defeat the purpose of data augmentation? Is there a way of adding augmented images to the dataset rather than replacing the originals?

rotation_angle = 90
rotation_transform = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomRotation(degrees=rotation_angle),
    transforms.ToTensor()
])


dataset = FullDataset(dir_building, dir_cm, transform=rotation_transform)

Code snippit inside my FullDataset getitem():

        if self.transform:
            aug_input = self.transform(input)
            aug_mask = self.transform(mask)

        return aug_input, aug_mask
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Well you could, for instance, add a second dataset and use `ConcatDataset` to combine the original with the concattenated dataset (see e.g., [this](https://discuss.pytorch.org/t/appending-augmented-data-to-original-training-set/105213) pytorch-discussion. What I see more commonly is that you apply the augmentation with some probability such that in each iteration some images are left unchanged and others are augmented, – i regular Aug 09 '23 at 20:21
  • Appreciate the response. Regarding your second suggestion, will the model's performance improve even if the dataset size stays the same? Unless the dataloader transforms the dataset differently for each epoch, shouldn't the model performance be similar considering that you use the same amount of total data? Don't know if I am correct with that assumption, though. – girthquake Aug 09 '23 at 20:26
  • It depends on your implementation, but in most cases you will load the data from the dataloader in each epoch. In that case if you have random probability in your transform or a range in the parameters of the transform, it will apply the transformations differently at each epoch. Whether the model performance is the same will depend on many factors such as the problem you are trying to solve, the augmentation you use and how you apply it – i regular Aug 09 '23 at 20:34
  • In that case, should I initialize the dataloader within my epoch for loop or will it change the transformations per epoch even if the dataloader is outside the loop? – girthquake Aug 09 '23 at 20:42
  • The transformation happens whenever the `get_item()` function is called so for instance when you call: `for data in data_loader:` There are some quite extensive PyTorch threads on the topic if you want more information, e.g. [here](https://discuss.pytorch.org/t/basic-question-about-torchvision-transforms/40213) – i regular Aug 09 '23 at 20:47
  • Got it, appreciate the help. – girthquake Aug 09 '23 at 20:49
  • Sorry, I meant to point you [here](https://discuss.pytorch.org/t/data-augmentation-in-pytorch/7925/2?u=nikronic) which is more extensive than the discussion in the link above – i regular Aug 09 '23 at 20:51

0 Answers0