3.4.15 · D3Convolutional Neural Networks

Worked examples — Data augmentation for images

2,369 words11 min readBack to topic

This page is the hands-on companion to the parent note on data augmentation. There we built the formulas. Here we run them on numbers — every sign, every corner case, every degenerate input — so that when you meet one in code or on an exam, you have already seen it.

Before starting, recall the toolbox from the parent:

  • Rotation matrix turns a point into a new point by angle .
  • Horizontal flip mirrors columns: .
  • Brightness/contrast: .
  • MixUp: blend two images and their labels with weight .
  • Cutout: zero out a rectangle.

We treat a pixel as a point on a grid. The top-left pixel is ; grows rightwards, grows downwards. That is the convention images use, and we keep it everywhere below so you never get confused about which way "up" is.


The scenario matrix

Every augmentation problem you will face is one of these cells. The table below enumerates them; each worked example afterwards is tagged with the cell it covers.

Cell Case class What makes it tricky
A Rotation, small positive angle ordinary case, both signs of sine/cosine positive-ish
B Rotation, negative angle signs flip — clockwise vs counter-clockwise
C Rotation, degenerate and identity and the "clean" swap
D Flip on even vs odd width off-by-one and centre pixel
E Brightness/contrast with clipping values escaping
F Brightness degenerate the "do nothing" transform
G MixUp interior soft labels between classes
H MixUp limiting and recovers a pure original
I Cutout coverage fraction how much information is erased
J Real-world word problem choosing which augmentation is label-preserving
K Exam twist rotate a point already off-centre

The eleven examples below hit all eleven cells.


Example 1 — Rotation, small positive angle (Cell A)

Forecast: Guess first — will the eye go up (negative ) or down (positive )? Will shrink or grow?

Steps.

  1. Write the rotation formula for a centre at the origin (the translation terms vanish): Why this step? With the "translate to origin, rotate, translate back" collapses to just the matrix — nothing to subtract or add.

  2. Plug in : , . Why this step? These two numbers are the rotation — cosine says "how much of the old x survives along the new x", sine says "how much leaks into the other axis."

  3. Compute with :

Verify: The point started at distance . Check the new distance: . ✓ Rotation preserves distance from centre — that is its defining property. Because our -axis points down, a positive means the eye moved slightly downward, which is what a screen-convention CCW-on-paper rotation does here.

Figure — Data augmentation for images

Example 2 — Rotation, negative angle (Cell B)

Forecast: Should the answer be the mirror of Example 1 across the x-axis?

Steps.

  1. Recall but . Why this step? Cosine is even, sine is odd. This single fact tells us that flipping the rotation direction flips only the sine terms — the geometry stays symmetric.

  2. So , . Plug in:

Verify: Distance . ✓ And indeed is Example 1's answer reflected across the x-axis, exactly the forecast. A negative angle mirrors the outcome — this is why "" in the parent note means "either rotation direction is a valid augmentation."


Example 3 — Rotation, degenerate angles (Cell C)

Forecast: One should be "nothing happens"; the other should swap axes. Which is which?

Steps.

  1. : , . Then , . The point is unchanged. Why this step? This confirms the identity case — a "rotation by nothing" must leave every pixel where it was, or the formula would be wrong.

  2. : , . Then , . Why this step? At exactly the matrix becomes , which cleanly maps the x-axis onto the y-axis. No irrational numbers — a useful sanity anchor.

Verify: Both outputs have distance from the origin. ✓ At we got the input back (identity); at the point that was to the right is now downward. These two "clean" cases are the ones to memorise as guardrails.


Example 4 — Horizontal flip: even vs odd width (Cell D)

Forecast: For odd width there is a true centre column. Does it move at all?

Steps.

  1. Even width , : . Why this step? The formula pairs column with column ; here it pairs . The exists because columns are 0-indexed: the last column is , not .

  2. Odd width , centre : . Why this step? The middle column maps to itself — a fixed point of the flip. Only odd widths have one.

Verify: Check the endpoints of the image: and . They swap, and . ✓ Every pair sums to , confirming the flip is its own inverse (flip twice, get the original back).


Example 5 — Brightness/contrast with clipping (Cell E)

Forecast: , plus . But pixels only live in . What actually gets saved?

Steps.

  1. Apply the linear map: . Why this step? This is the raw transform; contrast stretches () then brightness shifts up ().

  2. Clip to the valid range: . Why this step? An 8-bit pixel physically cannot exceed — the sensor/screen saturates to pure white. Forgetting to clip is the classic augmentation bug.

Verify: A darker pixel under the same map: , which is inside , so it is kept as . ✓ Bright pixels saturate; mid-tones survive — exactly the "highlights blow out" behaviour you'd expect from raising both contrast and brightness.


Example 6 — Brightness degenerate: the identity (Cell F)

Forecast: This should be the "augmentation that does nothing."

Steps.

  1. . For , . Why this step? Setting (no stretch) and (no shift) recovers the identity. Knowing the identity settings lets you disable an augmentation cleanly in an ablation study.

Verify: Identity leaves every pixel untouched, so the image is bit-for-bit the original. ✓ This is the anchor case: any augmentation with a "strength" knob must reduce to identity at zero strength, or it will corrupt data even when you think it's off.


Example 7 — MixUp, interior blend (Cell G)

Forecast: With weighting the cat, should lean toward cat?

Steps.

  1. Blend the pixel: . Why this step? MixUp is a straight-line interpolation: at we're of the way from the dog toward the cat pixel.

  2. Blend the label the same way: . Why this step? The label must move on the same line as the pixels — that is the whole idea. The network is taught "this is cat, dog," which softens its decision boundary.

Verify: The label entries sum to , so is still a valid probability vector. ✓ And leans toward cat, matching the forecast because weighted the cat.


Example 8 — MixUp limiting cases (Cell H)

Forecast: Each extreme should recover a pure original example, not a blend.

Steps.

  1. : , . Pure cat. Why this step? means "all weight on example " — the second image vanishes.

  2. : , . Pure dog. Why this step? The endpoints of the interpolation must return the endpoints themselves, or MixUp would corrupt un-mixed data.

Verify: At both extremes the label is a clean one-hot vector (sums to 1, no fractional class). ✓ This is why the sampler is safe: even when it draws near 0 or 1, you just get a near-original example — never garbage.


Example 9 — Cutout coverage fraction (Cell I)

Forecast: Side halves the image — is the area erased a half, or a quarter?

Steps.

  1. Total pixels: .
  2. Masked pixels: . Why this step? Cutout erases an area, and area scales with the square of the side. Halving the side quarters the area — a classic dimension trap.
  3. Fraction erased: . Surviving pixels: .

Verify: . ✓ Only a quarter is erased even though the side is half the image — so the network still has of the picture to reason from, forcing it to spread its attention rather than fixating on one region. This is the dropout-like regularization the parent note described.


Example 10 — Real-world word problem (Cell J)

Forecast: Digits are shape-sensitive. Does flipping a "6" keep it a "6"?

Steps.

  1. A vertical flip maps . Applied to a "6", it turns the loop-at-the-bottom into a loop-at-the-top, producing something that reads like "9". Label changed → not label-preserving. Why this step? Label-preservation is domain-specific (parent note): "Would a human give it the same label?" For house digits, no.
  2. A rotation uses , giving — a point-reflection through the centre. A rotated "6" becomes a "9" as well. Also not preserving.

Verify: Check the transform on the corner under : , distance still ✓ (it is a valid rotation) — the geometry is fine; the semantics are what break. Lesson: a mathematically correct augmentation can still be wrong for your task. Contrast with a natural-photo classifier, where these same flips are fine and boost generalization.


Example 11 — Exam twist: rotate an off-centre point (Cell K)

Forecast: The point is units right of centre. After a quarter turn, should it end up units below the centre?

Steps.

  1. Translate to origin (subtract the centre): . Why this step? The bare rotation matrix only spins about the origin. We must move the centre to the origin first, or we'd spin about the wrong point.

  2. Rotate by (): , .

  3. Translate back (add the centre): , . Why this step? The full parent formula is exactly "subtract centre, apply matrix, add centre" — this example makes all three stages explicit.

Verify: Distance from centre before: . After: . ✓ Same radius, and the point moved from "right of centre" to "below centre" (positive = down), matching the forecast.

Figure — Data augmentation for images

Recall Self-test

Rotating by about the origin gives? ::: A image flipped horizontally sends its centre column to? ::: (fixed point) Brightness on stores what value? ::: (clipped) MixUp with on labels and gives? ::: Cutout of side 16 on a image erases what fraction? :::

See also: why these tricks fight overfitting · augmentation with transfer learning · यही टॉपिक Hinglish में.