Data augmentation: trasformazioni per la robustezza
Imparerete ad applicare flip orizzontali casuali, ritagli e variazioni del colore tramite torchvision.transforms e misurare il miglioramento dell’accuratezza ottenuto con la data augmentation.
Data augmentation: trasformazioni per la robustezza è una lezione Machine Learning Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Machine Learning Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Machine Learning Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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 reversedRandomCrop 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.
Domande Frequenti
La lezione «Data augmentation: trasformazioni per la robustezza» è gratuita?
Sì — il testo completo di «Data augmentation: trasformazioni per la robustezza» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Machine Learning Academy, passa a CoddyKit PRO. Il corso Machine Learning Academy include 4 lezioni in totale.
Cosa imparerò in «Data augmentation: trasformazioni per la robustezza»?
Imparerete ad applicare flip orizzontali casuali, ritagli e variazioni del colore tramite torchvision.transforms e misurare il miglioramento dell’accuratezza ottenuto con la data augmentation. Eserciti Machine Learning Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Machine Learning Academy?
Non è richiesta alcuna esperienza precedente. Machine Learning Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Data augmentation: trasformazioni per la robustezza»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Machine Learning Academy?
Sì. Ogni lezione Machine Learning Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Convoluzione e filtri: rilevare bordi e pattern
- Layer di pooling: downsampling spaziale e invarianza
- Costruire e addestrare una CNN su CIFAR-10
- Data augmentation: trasformazioni per la robustezza