C.W.K.
Stream
Lesson 02 of 07 · published

Image Augmentation — Random Crops, Flips, Color, MixUp, CutMix

~14 min · augmentation, mixup, cutmix, regularization

Level 0Tensor Curious
0 XP0/62 lessons0/13 achievements
0/120 XP to next level120 XP to go0% complete

Augmentation is regularization disguised as data

The more (slightly perturbed) views of each training image the model sees, the harder it is to memorize and the better it generalizes. Augmentation is the single most cost-effective regularizer in computer vision.

Standard image augmentations

  • RandomResizedCrop — crop a random scale and aspect ratio, resize to fixed output. The standard ImageNet-style crop.
  • RandomHorizontalFlip(p=0.5) — universal for natural images.
  • RandomVerticalFlip — only when up/down doesn't change semantics (medical images, satellite, abstract patterns).
  • ColorJitter — brightness, contrast, saturation, hue perturbation.
  • RandomRotation, RandomAffine — small rotations and translations.
  • RandomErasing — zero out a random rectangle (a.k.a. Cutout). Surprisingly effective.

Batch-level augmentation: MixUp and CutMix

These take TWO images and blend them — and blend the labels too. The model is forced to predict a soft label corresponding to the mix. Standard in modern image-classification training (any ImageNet baseline from 2020+ has them).

  • MixUp — pixel-wise linear interpolation between two images with weight α from a Beta distribution.
  • CutMix — cut a rectangle from image B and paste into image A; label is weighted by the area of the paste.

The cardinal rule

Train transforms apply augmentation; val/test transforms apply only deterministic resize + normalize. Augmenting validation data means you're measuring against a moving target. Two separate Compose pipelines.

Code

Train vs val transforms — never mix them up·python
import torch
import torchvision.transforms.v2 as T

# TRAINING: includes augmentation
train_tf = T.Compose([
    T.ToImage(),
    T.ToDtype(torch.float32, scale=True),
    T.RandomResizedCrop(224, scale=(0.8, 1.0)),
    T.RandomHorizontalFlip(p=0.5),
    T.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.05),
    T.RandomRotation(15),
    T.RandomErasing(p=0.1),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# VALIDATION: deterministic only — resize, center-crop, normalize
val_tf = T.Compose([
    T.ToImage(),
    T.ToDtype(torch.float32, scale=True),
    T.Resize(256),
    T.CenterCrop(224),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
MixUp and CutMix — applied per batch·python
import torch
import torch.nn as nn
from torchvision.transforms.v2 import MixUp, CutMix, RandomChoice

mixup = MixUp(alpha=0.2, num_classes=10)
cutmix = CutMix(alpha=1.0, num_classes=10)

# Randomly pick one each batch
batch_aug = RandomChoice([mixup, cutmix])

# Loss: with mixed labels, use CE with soft targets
criterion = nn.CrossEntropyLoss()

for x, y in train_loader:
    x, y = batch_aug(x, y)               # y is now soft (probability over classes)
    out = model(x)
    loss = criterion(out, y)
    loss.backward()
Inspecting one transformed image·python
import torch
import torchvision.transforms.v2 as T
from torchvision import tv_tensors

# Build a synthetic image
img = torch.randint(0, 255, (3, 224, 224), dtype=torch.uint8)

aug = T.Compose([
    T.ToImage(),
    T.ToDtype(torch.float32, scale=True),
    T.RandomResizedCrop(224, scale=(0.5, 1.0)),
    T.RandomHorizontalFlip(p=0.5),
])

# Apply twice — different random outcomes
out1 = aug(img)
out2 = aug(img)
print(torch.equal(out1, out2))   # False — random transforms differ each call

External links

Exercise

Build train and val transform pipelines for CIFAR-10. Apply each to the same image 5 times — train should produce 5 different outputs, val should produce 5 identical outputs. Save them as a 2x5 grid (matplotlib). The visual diff makes the rule memorable.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.