Augmentation Implementation in PyTorch
What you'll learn: How to efficiently apply data augmentation in PyTorch using built-in transforms and custom pipelines during training.
The Core Idea
Now that you understand various augmentation techniques (flips, crops, color changes, Cutout, Mixup, etc.), you need to actually implement them in your training pipeline. PyTorch provides torchvision.transforms for common augmentations that integrate seamlessly with data loaders, applying transformations on-the-fly as batches are loaded—no need to pre-generate augmented copies of your entire dataset.
Using torchvision.transforms
Think of transforms as a recipe that gets applied to each image as it's loaded. You compose multiple transforms into a pipeline:
from torchvision import transforms
# Define augmentation pipeline
train_transforms = transforms.Compose([
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomRotation(degrees=15),
transforms.ColorJitter(brightness=0.2, contrast=0.2),
transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
# Apply in dataset
from torchvision.datasets import CIFAR10
train_dataset = CIFAR10(root='./data', train=True,
transform=train_transforms)
The beauty: each epoch sees different augmented versions of your data because transforms apply randomness during loading.
Custom Transforms
For techniques not built-in (like Cutout), create custom callable classes:
class Cutout:
def __init__(self, n_holes=1, length=16):
self.n_holes = n_holes
self.length = length
def __call__(self, img):
# img is a tensor [C, H, W]
# Apply random cutout logic here
return img
Add it to your Compose chain just like built-in transforms.
Key Takeaway: Use torchvision.transforms.Compose() to build efficient augmentation pipelines that apply random transformations during data loading, ensuring each epoch trains on fresh variations without storing multiple copies of your dataset.