Sub topic 8
Composing a Randomized Augmentation Pipeline
Assume you know basic Python, image arrays, supervised learning, and PyTorch training loops. A pipeline is an ordered list of image transforms. Each transform receives the output of the previous transform. During training, random transforms create a different view of an image each time the dataset returns it.
The goal is variety without changing the image's label. A useful pipeline usually combines:
- Geometric changes, such as crops and flips.
- Photometric changes, such as brightness and contrast changes.
- Occlusion changes, such as erased regions.
- Conversion and normalization steps required by the model.
Choose the order deliberately
Order affects the result. Apply spatial operations while the image still has a normal image representation. Apply tensor-only operations after converting the image to a tensor.
A common order is:
- Crop or resize the image.
- Flip or rotate it.
- Change color or lighting.
- Convert it to a tensor.
- Normalize its channels.
- Apply tensor-based operations such as random erasing.
For example, a random crop followed by a horizontal flip produces a different view on each access:
from torchvision import transforms
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
train_tf = transforms.Compose([
transforms.RandomResizedCrop(224, scale=(0.5, 1.0)),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ColorJitter(
brightness=0.2,
contrast=0.2,
saturation=0.2,
),
transforms.ToTensor(),
transforms.Normalize(mean=mean, std=std),
transforms.RandomErasing(p=0.25, scale=(0.02, 0.20)),
])Compose runs the transforms from top to bottom. RandomResizedCrop selects a random region, resizes it to 224 by 224 pixels, and can make the model less dependent on exact object position. RandomHorizontalFlip changes half the images on average. ColorJitter changes appearance while keeping the object identity.
The transform probabilities and ranges are hyperparameters. A probability controls how often a transform runs. A range controls how strong its effect can be.
Understand sampling and reproducibility
Online augmentation applies transforms when a sample is loaded. It does not usually save modified images. This saves disk space and gives the model many possible views of the same source image.
A pipeline is stochastic, but debugging often requires repeatable behavior. Set random seeds when comparing experiments, and record the transform configuration with each run. A seed does not make a poor pipeline correct. It only makes a particular sequence of random choices repeatable.
Keep training and evaluation separate
Random transforms belong in the training pipeline. Validation and test transforms should be deterministic so that metrics are comparable between runs.
from torchvision import transforms
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
eval_tf = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=mean, std=std),
])Both pipelines should produce the input size and numeric scale expected by the model. Do not use random crops, random flips, or random erasing for ordinary validation. Otherwise, the measured score also depends on random image alterations.
Add stronger policies carefully
RandAugment chooses a fixed number of operations and applies them at a shared magnitude. It can replace a long list of individually tuned color and geometric transforms.
from torchvision import transforms
train_tf = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandAugment(num_ops=2, magnitude=9),
transforms.ToTensor(),
transforms.Normalize(
[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225],
),
])Start with a small number of operations and moderate magnitudes. Increase strength only after checking sample images and validation performance.
Exercise
Why should RandomErasing appear after ToTensor, while RandAugment usually appears before it?
Show answer
RandomErasing operates on tensors, whereas RandAugment is designed to operate on PIL images or uint8 tensors. The pipeline must therefore convert to a tensor before erasing, but it can apply RandAugment earlier.