Is there any built-in way in PyTorch to augment the dataset?

I have a training dataset of 1000 images and 5 classes, which is indeed very fewer for a deep learning model. Is there any built-in way in PyTorch to augment this dataset? i.e. cropping the images randomly or changing their orientation or doing some other transformations to those training images and then add them to dataset to increase dataset size?

Hi Shaida!

Yes, but you do have to write just a little bit of code.

Take a look at torchvision.transforms. You might also work through
parts of the Writing Custom Datasets tutorial.

The basic idea is that before you pass a batch of images to your
model you apply various random transforms to your images.
Every time you run an epoch (iterate through your dataset) you
will get a different set of random transforms (applied, of course,
to the same original images in your dataset).

You could, of course, build in advance a larger, augmented dataset
that includes your original images as well as transformed images,
and then train on that normally, But it seems like less of a bother
just to apply the transforms on the fly.


K. Frank

Perhaps the following snippets might help.

transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),

transform_test = transforms.Compose([
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),

I applied

to data, but the data points remain the same in train data when I print(dataset_train).
take a look to the code.

transform_in = transforms.Compose([transforms.Grayscale(num_output_channels=1),
transforms.Resize([256, 256]),
transforms.Normalize(mean=[0.5], std=[0.5])])

dataset_train = dsets.ImageFolder(train_dir, transform=transform_in)
dataset_test = dsets.ImageFolder(test_dir, transform=transform_in)


The output is the same as before transforms.RandomHorizontalFlip()


I’d recommend you try kornia, it performs augmentations on GPU (which is beneficial in your case since your don’t have much data so it’s possible to achieve high throughput, possibly even load it into memory if you have enough RAM), plus they have a lot more augments and decent docs.

1 Like