Useful variation, preserved meaning

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.

10 min to exploreMachine Learning · Computer Vision · Computer Vision with PyTorch
01 / UNDERSTAND

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.

02 / EXPLORE

Make a prediction.
Then move a control.

The augmentation studio

One example.
Many useful variations.

Design a training pipeline, generate a repeatable batch and ask whether the class still makes sense.

Real image transformations
The source

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.

View 01 · crop area—
Rotation—
Horizontal flip—
Brightness / contrast / saturation—
Preserve the task, not just the pixels.

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
Training augmentation + deterministic validation preprocessing

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.

Built-in images are original teaching illustrations. No classifier is running or assigning new labels here.Explore generalization →

Images are transformed locally in your browser. Built-in examples are original illustrations; no classifier assigns or verifies labels.

03 / SEE THE CODE

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 ↗
Standalone example · illustrative valuesDownload .py
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.
04 / TRY IT

Explain the change.
Test your reasoning.

  1. Use the same seed to compare mild and strong augmentation. Inspect which details disappear.
  2. Switch from an apple to a digit or a sign. Which transformations would you keep, and which could invalidate the label?
  3. Build a justified pipeline and download its PyTorch configuration. Explain why validation uses deterministic preprocessing.
← All visual labsNext: Regularization: Learn Beyond the Sample

Find your next idea.

Search across courses, research, projects, and resources.

Ideas, connected.esc to close