Data Augmentation: Build the Variations
Transform an image, generate a repeatable batch and inspect each crop, rotation and color adjustment. Decide what should stay invariant.
Which changes preserve the label your model is supposed to learn?
Augmentation samples different views of a training observation. The useful transformations depend on the task and deployment conditions. Split original observations first so related views do not leak across training and evaluation.
Make a prediction.
Then move a control.
One example.
Many useful variations.
Design a training pipeline, generate a repeatable batch and ask whether the class still makes sense.
One apple. Nine new views.
These are new views of the same observation. They do not become nine independent observations.
Loading the source image…
A possible training batch
Each tile samples a different crop, flip, rotation and color adjustment.
A useful transformation should preserve the target label under the conditions your model will encounter.
Pipeline order: square crop → resize to 224 × 224 → optional horizontal flip → rotation → color adjustments. Fill outside rotated images is light gray.
Your augmentation pipeline in PyTorch
Split originals first. Augment training examples.
Related views of the same original must stay within the same data split. Use deterministic preprocessing for ordinary validation and testing. Test-time augmentation is a separate, explicit evaluation protocol.
The seed belongs to this browser renderer. Torchvision uses different random sampling, interpolation and ColorJitter ordering, so its pixels will not exactly match these previews.
Images are transformed locally in your browser. Built-in examples are original illustrations; no classifier assigns or verifies labels.
The idea,
in Python.
A compact implementation of the core concept. Open the code when you are ready to connect the visual to the algorithm.
Open the core implementation Python ↗
import torch
from torchvision.transforms import v2
from torchvision import models
# Square crop: scale is a fraction of ORIGINAL IMAGE AREA.
train_transforms = v2.Compose([
v2.RandomResizedCrop((224, 224), scale=(0.78, 1.0), ratio=(1.0, 1.0), antialias=True),
v2.RandomHorizontalFlip(p=0.50),
v2.RandomRotation(degrees=18, fill=244),
v2.ColorJitter(brightness=0.18, contrast=0.16, saturation=0.12),
v2.ToImage(),
v2.ToDtype(torch.float32, scale=True),
v2.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# Deterministic validation preprocessing for these pretrained weights.
validation_transforms = models.ResNet18_Weights.DEFAULT.transforms()
# Split ORIGINAL observations first; augment only the training split.
# Fresh random variants are sampled when training examples are accessed.
# Browser interpolation/colour rendering differs from torchvision.Explain the change.
Test your reasoning.
- Use the same seed to compare mild and strong augmentation. Inspect which details disappear.
- Switch from an apple to a digit or a sign. Which transformations would you keep, and which could invalidate the label?
- Build a justified pipeline and download its PyTorch configuration. Explain why validation uses deterministic preprocessing.