Art of DS

Appearance

Palette
Theme
Unlock $20 one time
Sign in, or create an account

Sign in to Art of DSCreate your Art of DS account

We email you a link and a six-digit code. Either one signs you in; there is no password.Enter your email. We send a link and a six-digit code to confirm it — no password to remember. The account is free.

  • You come back to this page, right where you were.
  • Already have an account? The same email simply signs you in.

By continuing you accept the Terms and the Privacy policy.

Your account

Free accountLifetime access

difficulty 2/5 Free

Data augmentation

Definition

Applying random label-preserving transformations to training examples, so the model sees each one many ways and cannot rely on the accidents of how it happened to be captured.

Intuition

Your training set has 800 photographs. Every one of them was taken from some particular angle, in some particular light, with the subject at some particular size. The network has no way to know which of those properties are the subject and which are the circumstances, so it will happily use any of them that predicts the label [1, §14.1].

Augmentation removes that option by contradiction. Show the same photograph flipped, cropped, brightened — each time with the same label — and "the subject faces left" stops being a usable rule, because sometimes it does not. What survives repeated contradiction is whatever is actually invariant, which is the thing you wanted the model to learn [3, §7.4].

It is not more data in any information-theoretic sense. Every pixel came from photographs you already had. What it adds is a statement about which transformations should not change the answer — a piece of domain knowledge, injected as examples rather than as architecture [3, §7.4].

Formal definition

D2L: "image augmentation generates similar but distinct training examples after a series of random changes to the training images, thereby expanding the size of the training set" [1, §14.1].

Two mechanisms are offered, and they are different. One is the expansion of the training set. The other is the interesting one: "random tweaks of training examples allow models to rely less on certain attributes, thereby improving their generalization ability" [1, §14.1].

Rely less on certain attributes is the whole argument. The point is not volume, it is the removal of shortcuts.

The standard operations are flipping, cropping and colour: horizontal and vertical flipping with left-right most common, random resized cropping with varying aspect ratios, and adjustments to "brightness, contrast, saturation, and hue" [1, §14.1.1].

How it works

originalsame labelh-fliprandom croprotatebrightnesshue
One labelled example, and five things the network is told are also that label. None of them is new information — every pixel came from the same photograph — but each one removes an excuse the model could otherwise use: it can no longer key on the object facing left, or on it filling the frame, or on the lighting that day. The randomness is the mechanism, and it belongs to training only; at prediction time you show the image once, unaltered, or the answer stops being reproducible.

Randomness is the mechanism, not an implementation detail. "Most image augmentation methods have a certain degree of randomness" [1, §14.1.1] — a fixed flip applied to every image would just produce a mirrored dataset, teaching nothing. It is the fact that the model sees a different version each epoch that stops it keying on any one of them.

It applies at training time only. D2L is unambiguous: "in order to obtain definitive results during prediction, we usually only apply image augmentation to training examples, and do not use image augmentation with random operations during prediction" [1, §14.1.2]. Leaving it on at inference makes predictions non-reproducible — run the same image twice, get two answers.

In practice it is a composed pipeline attached to the dataset. Torchvision's canonical classification recipe is exactly the set above — a random resized crop, a horizontal flip with probability 0.5, then normalisation [2, Transforming images] — and the same transforms can be applied to bounding boxes and masks alongside the image [2, Transforming images], which matters because a flipped image with unflipped boxes is silently corrupt training data.

Where it's used

  • Every vision training run of consequence, to the point that the augmentation recipe is part of how architectures are compared: two algorithms are only comparable under the same augmentation scheme [3, §7.4].
  • Small-data regimes, alongside transfer learning — the two attack the same shortage from different directions.
  • Detection and segmentation, where the transform must be applied to the targets as well as the input [2, Transforming images].
  • Audio, as time stretching, pitch shifting and noise injection — the same idea, different invariances.
  • Text, more cautiously, since most transformations of a sentence are not label-preserving.

Why & when

Reach for it whenever the model is larger than the dataset justifies, which for deep networks is the normal case.

Choose transformations by asking what should not change the label. This is the entire design decision, and it is domain knowledge, not a hyperparameter.

Do not apply a transformation your test data will never contain. Vertical flips help on satellite imagery, where there is no up; on street photographs they teach the model to recognise things that do not occur.

Turn it off for evaluation [1, §14.1.2]. This is the most common bug in the area and it makes results irreproducible rather than merely worse.

Watch for it breaking the label. A horizontal flip changes a 6 into something that is not a 6, and turns a photograph of a left hand into a right one. Character recognition has to tell "b" from "d" and "6" from "9", so flips and 180° rotations are out [3, §7.4].

Worked example

The property that makes augmentation legitimate, checked directly: the array changes and the label does not.

Python 16 lines Show the code Hide the code
import numpy as np

img = np.arange(36).reshape(6, 6)
row = 2
views = {
    "original":      img,
    "h-flip":        np.fliplr(img),
    "crop + resize": img[1:4, 1:4].repeat(2, 0).repeat(2, 1),
    "brightness":    np.rint(np.clip(img * 1.25, 0, 35)).astype(int),
}
for name, a in views.items():
    print(f"  {name:<15} row {row}: {a[row].tolist()}")

print(f"\n  every view differs from the original: "
      f"{all(not np.array_equal(img, a) for n, a in views.items() if n != 'original')}")
print("  the label attached to all four        : unchanged")

Output

  original        row 2: [12, 13, 14, 15, 16, 17]
  h-flip          row 2: [17, 16, 15, 14, 13, 12]
  crop + resize   row 2: [13, 13, 14, 14, 15, 15]
  brightness      row 2: [15, 16, 18, 19, 20, 21]

  every view differs from the original: True
  the label attached to all four        : unchanged

Four different arrays, one label. Any rule the network might learn that depends on the exact pixel values — "position 0 of row 2 is 12" — is contradicted by three of the four. A rule that depends on the relationships between values survives all of them.

Notice what each transformation destroys. The flip reverses order, so left-right position becomes unusable. The crop changes scale and duplicates values, so absolute size becomes unusable. The brightness shift moves every value by a multiplicative factor, so absolute intensity becomes unusable. Each one is a specific shortcut being closed, chosen because the thing it closes off is genuinely irrelevant to the label.

That is why the choice of transformations is domain knowledge rather than a knob. Adding a vertical flip here would close off "which way up it is" — useful if that is irrelevant to your problem, and destructive if it is not.

Pitfalls

Leaving augmentation on at inference. Predictions stop being reproducible [1, §14.1.2], and a metric computed under it is measuring a randomised model.

Augmenting the image and not the label. In detection and segmentation the targets must move with the pixels; torchvision passes both through the same transform for exactly this reason [2, Transforming images].

Transformations that change the class. Flips on digits and text, aggressive hue shifts where colour is the label, crops that remove the object entirely.

Augmenting before the train/validation split. Augmented copies of the same source image landing on both sides is leakage, and the validation score becomes meaningless.

Treating it as a substitute for data. It reduces reliance on incidental attributes [1, §14.1]; it does not add subjects, poses or conditions your collection never contained.

Not read yetReach the end of the page and it counts as read.

Keep your progressWith lifetime access, every page you finish is marked and you pick up where you stopped, on all your devices.

Unlock

Check yourself

3 questions

Each answer is explained, with the section it tests and its source.

Sources

  1. Zhang, Lipton, Li, Smola. Dive into Deep Learningbook · retrieved 2026-09-05https://d2l.ai/
  2. Torchvision documentation — Transforming images, videos, boxes and moredocs · retrieved 2026-09-05https://docs.pytorch.org/vision/stable/transforms.html
  3. Goodfellow, Bengio, Courville. Deep Learning (2016)bookhttps://www.deeplearningbook.org/

Have a promo code?

A code that takes 100% off unlocks everything at once. A smaller one takes its share off the price in the payment window: enter the same code there.

Write to support

A question, a problem with access, a refund? Write to us and we answer by email.

What is it about?

Your address is used only to answer you.

Message sent

Thank you. We will reply to the address you gave, by email.

Or write to us directly

support@artofds.com

Message sent Thank you. We will reply to the address you gave, by email.

Check the email address: we need it to reply.

That is our own address. Enter yours, so the reply reaches you.

This address cannot receive email. Check it for a typo.

Write a little more: at least 10 characters.

Too many messages in a short time. Try again in a few minutes.

The message could not be sent. Please write to us directly: support@artofds.com