Unit 06.03: Data augmentation and image normalization
Normalisation and augmentation both transform images, and they follow opposite rules about which split they apply to.
One is always applied; the other is training-only
Normalisation puts pixel values on a consistent scale, per channel. It applies to every split, using statistics computed from the training set.
Augmentation โ flips, crops, rotations โ expands the effective training set by showing the model varied versions of each image. It applies to training only.
import torch
from torch import nn
torch.manual_seed(0)
batch = torch.rand(8, 3, 16, 16) # values in [0, 1]
# Normalisation: subtract the mean, divide by the std, PER CHANNEL.
mean = batch.mean(dim=(0, 2, 3), keepdim=True)
std = batch.std(dim=(0, 2, 3), keepdim=True)
normalised = (batch - mean) / std
print("before: mean %.3f std %.3f" % (batch.mean(), batch.std()))
print("after : mean %.3f std %.3f" % (normalised.mean(), normalised.std()))
# Augmentation with plain tensor ops: a horizontal flip is an index reversal.
flipped = torch.flip(batch, dims=[3])
print("flip changes the image :", not torch.allclose(batch, flipped))
print("flipping twice restores:", torch.allclose(torch.flip(flipped, dims=[3]), batch))
# Augmentation belongs on TRAINING data only. Applying it at evaluation makes
# the score depend on a coin flip, and the number stops being comparable.
print("\naugment train: yes augment validation/test: no")
The normalisation output shows values centred near zero with unit standard deviation, per channel.
The flip demonstrates the augmentation property that matters: it is a genuine transformation of the content, and applying it twice returns the original. Whether it is *valid* depends on the task โ horizontal flips are fine for photographs of animals and wrong for text or for anything where left and right carry meaning.
The mistake this prevents
Augmenting the validation set. The score then depends on which random transformations happened to be drawn, so two evaluations of the same model disagree and neither is comparable to anything.
Takeaway
Normalise every split with training statistics. Augment the training split only, and only with transformations that preserve the label.
