0Pricing
Machine Learning Academy · Lesson

Data Augmentation: Transforms for Robustness

Learners will apply random horizontal flip, crop, and colour jitter via torchvision.transforms, and measure the accuracy improvement from augmentation.

Data Augmentation: Transforms for Robustness is a free Machine Learning Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Machine Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Data Augmentation?

Data augmentation artificially increases the size and diversity of the training dataset by applying random transformations to existing images. Instead of collecting new data (expensive), augmentation creates new training examples on the fly. The key insight is that for many tasks, the label should remain the same under the transformation — a horizontally flipped image of a cat is still a cat. Augmentation reduces overfitting and teaches the model to be invariant to irrelevant variations.

import torchvision.transforms as transforms
from PIL import Image
import torch

# A simple augmentation pipeline
transform = transforms.Compose([
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.RandomRotation(degrees=15),
    transforms.ColorJitter(brightness=0.3),
    transforms.ToTensor()
])

# Apply to same image -> different result each time
# img = Image.open('cat.jpg')
# aug1 = transform(img)  # one augmented version
# aug2 = transform(img)  # different augmented version
print('Augmentation pipeline defined')

torchvision.transforms: The Augmentation Toolkit

torchvision.transforms provides a rich library of image transforms. Key categories: geometric (flip, rotate, crop, resize, perspective); color (jitter, grayscale, solarize, equalize); pixel-level (Gaussian blur, random erasing); and tensor operations (ToTensor, Normalize). They chain together with transforms.Compose. From PyTorch 2.0, transforms.v2 provides improved speed and additional augmentation primitives.

import torchvision.transforms as transforms

# Catalog of key transforms
train_tf = transforms.Compose([
    transforms.Resize(36),                    # scale
    transforms.RandomCrop(32, padding=4),     # spatial
    transforms.RandomHorizontalFlip(),        # mirror
    transforms.RandomRotation(10),            # rotate
    transforms.RandomGrayscale(p=0.1),        # de-color
    transforms.ColorJitter(
        brightness=0.3, contrast=0.3,
        saturation=0.3, hue=0.1),             # color
    transforms.GaussianBlur(kernel_size=3,    # blur
                            sigma=(0.1, 1.0)),
    transforms.ToTensor(),
    transforms.Normalize([0.5]*3, [0.5]*3)
])
print('Full augmentation pipeline ready')

RandomHorizontalFlip and RandomVerticalFlip

RandomHorizontalFlip(p=0.5) mirrors the image left-to-right with probability p. This is one of the most effective augmentations for natural images — most objects look equally valid when mirrored. RandomVerticalFlip flips top-to-bottom; use it only when vertical flipping makes semantic sense (e.g., satellite imagery, microscopy). Never use vertical flip for upright objects like faces or digits — it would create unrealistic examples that confuse the model.

import torchvision.transforms as transforms
import torch

hflip = transforms.RandomHorizontalFlip(p=0.5)
vflip = transforms.RandomVerticalFlip(p=0.5)

# Both input and output are PIL images or tensors
# On a tensor: shape (C, H, W)

t = torch.arange(12.).reshape(1, 3, 4)  # 1ch, 3x4
flipped = transforms.functional.hflip(t)
print('Original:', t)
print('H-flipped:', flipped)  # columns reversed

RandomCrop for Spatial Invariance

RandomCrop(size, padding) pads the image (typically with zeros) then crops a random sub-region. For CIFAR-10 (32x32), padding=4 and crop size 32 is standard — it shifts the image content by up to 4 pixels in any direction. This teaches the model that the object label is independent of its exact position, providing translation invariance. CenterCrop is used at test time for deterministic central cropping from a slightly larger image.

import torchvision.transforms as transforms
import torch

# Training: random crop -> trains on shifted versions
train_crop = transforms.Compose([
    transforms.Pad(4, padding_mode='reflect'),  # pad 4 pixels
    transforms.RandomCrop(32)                  # random 32x32 region
])

# Test: deterministic center crop (no padding needed if same size)
test_crop = transforms.CenterCrop(32)

# RandomResizedCrop: zoom + crop (ImageNet-style)
rrc = transforms.RandomResizedCrop(
    224, scale=(0.08, 1.0), ratio=(0.75, 1.33)
)
print('Random crop transforms ready')

ColorJitter: Colour Augmentation

ColorJitter randomly perturbs brightness (how light/dark), contrast (difference between darks and lights), saturation (colour intensity), and hue (colour shift). These simulate variations in lighting, camera settings, and environmental conditions. Strong colour augmentation (jitter with 0.4 on each parameter) is part of the SimCLR self-supervised learning recipe. For standard supervised training, moderate values (0.2-0.3) are recommended.

import torchvision.transforms as transforms

# Moderate color augmentation for supervised learning
color_aug = transforms.ColorJitter(
    brightness=0.2,   # multiply by U[0.8, 1.2]
    contrast=0.2,     # multiply by U[0.8, 1.2]
    saturation=0.2,   # multiply by U[0.8, 1.2]
    hue=0.05          # shift hue by U[-0.05, 0.05]
)

# Strong color augmentation for self-supervised learning
strong_color = transforms.ColorJitter(
    brightness=0.8, contrast=0.8,
    saturation=0.8, hue=0.2
)

print('Color augmentation configured')

RandomRotation and RandomPerspective

RandomRotation(degrees) rotates the image by a random angle within [-degrees, +degrees]. Use small angles (10-15 degrees) for natural images; larger for medical imaging or scientific data where arbitrary orientations are valid. RandomPerspective applies a random projective transformation that simulates viewing the object from a different angle — as if the camera had tilted. Both add robustness to viewpoint changes at the cost of distorting rectangular features.

import torchvision.transforms as transforms

# Mild rotation for natural images
rotate = transforms.RandomRotation(
    degrees=15,
    fill=0            # fill empty corners with black
)

# Perspective distortion
perspective = transforms.RandomPerspective(
    distortion_scale=0.3,
    p=0.3             # apply 30% of the time
)

# Affine: combines rotation, scale, shear, translate
affine = transforms.RandomAffine(
    degrees=10,
    translate=(0.1, 0.1),
    scale=(0.9, 1.1)
)
print('Geometric augmentations defined')

Random Erasing: Occlusion Robustness

RandomErasing randomly masks out a rectangular region of the image by replacing it with zeros, random noise, or the mean pixel value. This simulates occlusion — when part of the object is blocked by another object. Models trained with random erasing learn to recognise objects from partial views, making them more robust in real-world scenes where occlusion is common. It is typically applied to the tensor (after ToTensor), not the PIL image.

import torchvision.transforms as transforms
import torch

# Apply to tensor after normalization
erasing = transforms.RandomErasing(
    p=0.5,           # probability of applying
    scale=(0.02, 0.2),  # fraction of image area to erase
    ratio=(0.3, 3.3),   # aspect ratio of erased region
    value=0           # fill with zeros
)

tf = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize([0.5]*3, [0.5]*3),
    erasing           # applied last on tensor
])
print('Random erasing added to pipeline')

AutoAugment and RandAugment

AutoAugment learns the best augmentation policy for a dataset using reinforcement learning — it was discovered that CIFAR-10 benefits from specific policies (shear X, rotate, equalize). RandAugment is simpler: randomly select N of K available operations and apply them at a magnitude M. Both consistently outperform hand-designed augmentation pipelines. PyTorch provides pre-trained policies via transforms.AutoAugment and transforms.RandAugment.

import torchvision.transforms as transforms

# AutoAugment with CIFAR10 policy
auto_aug = transforms.AutoAugment(
    policy=transforms.AutoAugmentPolicy.CIFAR10
)

# RandAugment: N ops, magnitude M
rand_aug = transforms.RandAugment(
    num_ops=2,         # apply 2 random operations
    magnitude=9        # strength of each operation (0-30)
)

train_tf = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.RandomHorizontalFlip(),
    rand_aug,          # or auto_aug
    transforms.ToTensor(),
    transforms.Normalize([0.4914, 0.4822, 0.4465],
                        [0.2023, 0.1994, 0.2010])
])

Online vs Offline Augmentation

There are two approaches to augmentation. Online augmentation applies transforms during training on-the-fly using the DataLoader — new augmented versions are created each epoch, providing effectively unlimited diversity. Offline augmentation pre-generates augmented images and saves them to disk — faster data loading but fixed diversity. Online augmentation (via transforms) is almost always preferred for deep learning because it creates new variety every epoch and requires no additional storage.

# Online augmentation: new random transform every epoch
# -> effectively infinite training data from finite images

train_dataset = torchvision.datasets.CIFAR10(
    root='./data', train=True,
    transform=train_transform,    # applied at load time
    download=True
)

# The SAME image gets a DIFFERENT augmented version each epoch!
# epoch 1: image 0 -> flipped, dark
# epoch 2: image 0 -> not flipped, brighter
# epoch 3: image 0 -> rotated, saturated
# This diversity is the core benefit of online augmentation
print('Online: new augmentation every epoch automatically')

Augmentation for Non-Image Data

Augmentation is not limited to images. For time-series: add Gaussian noise, scale amplitude, time-warp, or random crop sequences. For audio: pitch shift, time stretch, add background noise, random masking (SpecAugment for speech). For text: synonym replacement, back-translation, random insertion/deletion. For tabular data: add Gaussian noise to features, randomly permute rows in a batch. The principle is always the same: label-preserving variation that increases diversity.

import torch

# Tabular augmentation: Gaussian noise
def tabular_augment(X, noise_std=0.05):
    noise = torch.randn_like(X) * noise_std
    return X + noise  # label unchanged

# Time-series augmentation: amplitude scale
def ts_augment(series, scale_range=(0.8, 1.2)):
    scale = torch.empty(1).uniform_(*scale_range)
    return series * scale  # label unchanged

X = torch.randn(32, 10)   # 32 tabular samples, 10 features
X_aug = tabular_augment(X)
print('Augmented tabular shape:', X_aug.shape)

Measuring Augmentation Impact

Always measure the impact of augmentation quantitatively. Train the same model with and without augmentation for the same number of epochs and compare final validation accuracy. Also compare the training vs validation accuracy gap (generalisation gap) — good augmentation closes this gap. Too aggressive augmentation can actually hurt performance by making training too hard. Ablation studies (testing one augmentation type at a time) reveal which transforms contribute most to your specific task.

# Typical CIFAR-10 accuracy improvements from augmentation
results = {
    'No augmentation':               '73% val acc',
    '+ Random crop':                 '79% val acc (+6%)',
    '+ Horizontal flip':             '82% val acc (+3%)',
    '+ Color jitter':                '84% val acc (+2%)',
    '+ Random erasing':              '85% val acc (+1%)',
    '+ RandAugment':                 '87% val acc (+2%)',
    '+ Cutmix/Mixup':                '89% val acc (+2%)'
}

for aug, result in results.items():
    print(f'{aug}: {result}')

Quick Check

Test your understanding of Machine Learning with Python concepts from this lesson.

Lesson Recap

In this lesson you learned: data augmentation applies label-preserving random transforms during training to increase dataset diversity and reduce overfitting, key transforms include RandomCrop, RandomHorizontalFlip, ColorJitter, and RandomErasing, and AutoAugment and RandAugment automatically find effective augmentation policies, adding 2-6% accuracy over hand-designed pipelines. Next up we explore vanilla RNNs for sequential data processing.

Frequently asked questions

Is the “Data Augmentation: Transforms for Robustness” lesson free?

Yes — the full text of “Data Augmentation: Transforms for Robustness” is free to read here on the web, and the Machine Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Machine Learning Academy course, upgrade to CoddyKit PRO.

What will I learn in “Data Augmentation: Transforms for Robustness”?

Learners will apply random horizontal flip, crop, and colour jitter via torchvision.transforms, and measure the accuracy improvement from augmentation. You practise Machine Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Machine Learning Academy?

No prior experience is required. Machine Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Data Augmentation: Transforms for Robustness” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Machine Learning Academy lesson?

Yes. Every Machine Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Convolution and Filters: Detecting Edges and Patterns
  2. Pooling Layers: Spatial Downsampling and Invariance
  3. Building and Training a CNN on CIFAR-10
  4. Data Augmentation: Transforms for Robustness
← Back to Machine Learning Academy