Search

Search papers and lessons

Data Augmentation

Level 2 · 9 min read

Data augmentation makes new training examples by transforming existing ones in ways that keep their labels, so a model sees more variety and overfits less.

On this page

  • Colour = topic mix
  • Probability
  • Learning basics
  • Losses & regularization
  • Discrete math
  • Sign in to see your progress

TL;DR

A model learns from a finite set of examples, and it can memorise the particular examples it was given. Data augmentation fights this by creating extra examples from the ones you already have. You take an image, flip it, crop it, or change its brightness, and the result is a new training example with the same label. The model sees more variety for free.

Augmentation is a cheap way to get more data without collecting or labelling anything new. It only works when the transformation really keeps the label. A rotated photo of a cat is still a cat. A rotated "6" is a "9".

Where you'll see it: in nearly every image classifier trained from a modest dataset, in speech recognition where audio gets speed and noise changes, and in the training pipeline of the U-Net style segmentation models.

Intuition

More views of the same thing

Think of a student preparing for an exam. If they only practise with last year's paper, they may learn its exact wording. If they practise with the same questions reworded, turned around, and given with different numbers, they learn the subject. Augmentation is the second kind of practice for a model.

A photo of a dog is still a dog when it is mirrored left to right. It is still a dog when it is a little brighter, a little zoomed in, or cropped so the ears are cut off. None of those changes alter the answer, but each one changes every pixel. The model cannot memorise the exact pixels of one photo if it keeps seeing new arrangements of them.

This is why augmentation is a form of Overfitting control. A model that memorises its training set has a large gap between training and test error. Augmentation makes the training set harder to memorise, so the gap shrinks.

flowchart LR
  A["Training image (label: cat)"] --> B["Random transform t"]
  B --> C["Augmented image t(x), label still cat"]
  C --> D["Model prediction"]
  D --> E["Loss against the cat label"]
  E --> F["Gradient step"]

A plot of what it buys

Suppose you train on different amounts of data and measure error on new images. Without augmentation, error falls as the training set grows. With augmentation, the same curve sits lower, and the gain is largest when data is scarce.

102030405000.10.20.30.40.50.6training images (thousands)test error
  • no augmentation
  • with augmentation
Illustrative curves, not measured results. Augmentation lowers the whole curve, and the gap is widest when there are few images.

The math

Setting up the objective

Let (x,y)(x, y) be a training example, where xx is the input (an image) and yy is its label. Let tt be a transformation, such as a flip or a crop, and let T\mathcal{T} be a distribution over transformations we choose from.

An augmentation is label-preserving when the label of the transformed input is the same as the original label:

yt(x)=yfor every t∼T.y_{t(x)} = y \quad \text{for every } t \sim \mathcal{T}.

Let fθf_\theta be the model with parameters θ\theta, and let LL be the loss, for example Cross-Entropy Loss. Ordinary training minimises the average loss over the nn original examples:

1n∑i=1nL(fθ(xi),yi).\frac{1}{n} \sum_{i=1}^{n} L\big(f_\theta(x_i), y_i\big).

With augmentation, each example is replaced by its expected loss over the transformations:

1n∑i=1nEt∼T[L(fθ(t(xi)),yi)].\frac{1}{n} \sum_{i=1}^{n} \mathbb{E}_{t \sim \mathcal{T}}\Big[ L\big(f_\theta(t(x_i)), y_i\big) \Big].

In practice the expectation is estimated by sampling. Each time an example is used in a training step, a fresh tt is drawn, so over many epochs the model sees many different versions of it.

Counting the examples

If each image gets kk random augmentations, a dataset of nn images gives n(k+1)n(k+1) examples per epoch, counting the originals. The number of possible views can be much larger than this, because the transformations combine. Two simple parts multiply:

views=(positions)×(flip choices).\text{views} = (\text{positions}) \times (\text{flip choices}).

Why it helps

Augmentation is a regulariser. It adds the belief that the label should not change under the transformations we chose. The model is pushed to give the same output for xx and for t(x)t(x), which removes some of the freedom it had to fit accidental details. For small transformations this behaves roughly like a penalty on how much the output moves when the input is nudged, which is the same idea behind lowering variance by constraining a model.

Choosing transformations

Each transformation encodes a claim about what should not matter. Choose claims that are true for your task.

TransformationKeeps the label forBreaks the label for
Horizontal flipObjects, animals, scenesText, left and right hand gestures
Random cropObjects that stay mostly in frameCrops that remove the object
Small rotationMost natural photosDigits such as 6 and 9, letters such as b and d
Brightness or colour jitterMost natural photosTasks where colour is the label, such as ripeness
Added noiseTasks robust to noiseTasks where fine detail is the label

Worked examples

Example
A flip on a tiny image

Take a 3 by 3 greyscale image, written as rows of pixel values:

  1. Original: rows [1,2,3][1, 2, 3], [4,5,6][4, 5, 6], [7,8,9][7, 8, 9].
  2. Horizontal flip reverses each row, so row one becomes [3,2,1][3, 2, 1].
  3. Row two becomes [6,5,4][6, 5, 4], and row three becomes [9,8,7][9, 8, 7].
  4. The flipped image is [3,2,1][3, 2, 1], [6,5,4][6, 5, 4], [9,8,7][9, 8, 7].
  5. Every pixel value is still present, but every position has moved. A network that saw only the original has never seen this arrangement.
  6. If the label is "cat", the flipped image is still labelled "cat". Training on both gives two examples from one.

Interpretation: a single transformation doubles the number of distinct training arrays, at zero labelling cost, as long as the label is unchanged.

Example
How many crops a 32 by 32 image gives

Many image models are trained on 32×3232 \times 32 images. A common recipe pads each image with 4 pixels of zeros on every side, making a 40×4040 \times 40 image, and then takes random 32×3232 \times 32 crops.

  1. A 32×3232 \times 32 window can start at any row from 0 to 8 in the padded image, which is 8+1=98 + 1 = 9 positions.
  2. The same holds for columns, giving 9 positions.
  3. The number of distinct crops is 9×9=819 \times 9 = 81.
  4. Adding a random horizontal flip doubles this: 81×2=16281 \times 2 = 162 possible views per image.
  5. A dataset of 50,000 images then has 50,000×162=8,100,00050{,}000 \times 162 = 8{,}100{,}000 possible views.
  6. Each epoch the model sees one random view of each image, so successive epochs show it different versions of the same picture.

Interpretation: the augmentation does not add information about the image, but it multiplies the number of configurations the model could memorise, which makes memorising the exact training set much harder.

Tradeoffs and failure modes

Augmentation is cheap and usually helps, but it has real costs.

  • Label changes. If the transformation changes the true answer, you teach the model the wrong thing. A rotated 6 labelled as 6 is noise.
  • Distribution mismatch. If training images are flipped and test images never are, the model spends capacity on views it will never meet. Augmentation should match the variation you expect at test time.
  • Tasks with fixed geometry. Medical scans, satellite images, and documents often have a meaningful orientation. Heavy geometric changes can destroy the signal.
  • Segmentation needs care. The mask must receive exactly the same geometric transform as the image. Colour changes only touch the image.
  • Too much is worse. Very strong augmentation can make training slow and underfit the data, which is the opposite of the goal.
SituationProblemBetter choice
Small image dataset, natural photosOverfittingFlips, crops, colour jitter
Digits or lettersRotation flips the meaningSmall shifts and noise only
Already huge datasetLittle gain, longer trainingLight or no augmentation
Fixed-orientation scansGeometry carries signalIntensity changes, small elastic warps
Learning the policy itselfHand-tuning is slowSearch for policies, such as AutoAugment

History

The idea is old. In the early 2000s, Simard, Steinkraus, and Platt (2003) showed that applying small elastic distortions to handwritten digits made a neural network generalise better. Elastic deformation, which bends an image smoothly while keeping its shape, is still one of the augmentations listed in the aliases of this lesson.

It became standard practice with the 2012 AlexNet paper, which trained a large convolutional network on ImageNet and reduced overfitting by using random crops, horizontal reflections, and changes to the colour intensities of the image. Those choices were part of why the network generalised well, and most image pipelines since have used a similar set.

Later work asked which augmentations to use. AutoAugment, in 2018, used a search procedure to find augmentation policies from data rather than by hand. Surveys in 2019 collected the many variants that had appeared, and the field now treats augmentation as a standard part of training rather than an extra trick.

Check yourself

QuestionWhy is a horizontal flip a good augmentation for photos of cats but a poor one for the digit 2 written in a handwriting dataset?Show answer

A cat mirrored left to right is still a cat, so the label is preserved. A mirrored 2 looks like a different shape and is not a clean example of the digit 2. The question to ask is whether the transformation keeps the label, not whether it produces a new image.

QuestionA dataset has 10,000 images and each is given 3 random augmentations. How many examples are there per epoch if the originals are kept?Show answer

There are 10,000×(3+1)=40,00010{,}000 \times (3 + 1) = 40{,}000 examples per epoch. The originals are kept, so each image contributes itself plus three augmented copies.

QuestionAn image is padded by 2 pixels on each side and then cropped to its original size. How many distinct crop positions are there along each axis, and in total?Show answer

Along one axis the crop can start at offsets 0 to 4, which is 4+1=54 + 1 = 5 positions, using 2p+12p + 1 with p=2p = 2. In total there are 5×5=255 \times 5 = 25 distinct crop positions.

QuestionWhy can augmentation help even though it adds no new real images?Show answer

It multiplies the number of distinct inputs the model sees while keeping the labels, so memorising the exact training pixels becomes harder. The model is pushed to learn the features that survive the transformations, which is the same thing a better model of the underlying rule would do. This is a form of regularisation, and it narrows the gap between training and test error.

Sources