Skip to content
AI360Xpert
Beta

Geometric Augmentation

Move the pixels but keep the label. Flips, crops and rotations teach the model that a cat on the left and a cat on the right are the same cat.

A horizontal flip maps a detection box from 10 to 30 onto 70 to 90 in a 100 pixel image, preserving the label while moving every pixel.
A horizontal flip maps a detection box from 10 to 30 onto 70 to 90 in a 100 pixel image, preserving the label while moving every pixel.

Why Does This Exist?

A classifier trained on centered dogs fails on dogs near the border. The model memorized where dogs sit, not what dogs look like, because every training photo framed them the same way. Image classification needs position and scale tolerance, and collecting fresh photos at every position costs too much.

Geometric augmentation manufactures that variety from photos you already have. Flip left to right, take a random crop, rotate a few degrees, shift or zoom slightly. The pixels move but the label stays, so one photo becomes many viewpoints. This page covers spatial warps where geometry changes. Color changes that keep geometry fixed live in color augmentation, and the full menu lives in image augmentation.

You need one idea first. A detection label is a box plus a class. When you move pixels you must move the box with them, or you teach the model a lie.

Think of It Like This

Photocopies slid around on a desk

You photocopy a cat photo, then slide the copy left, rotate it slightly, and trim the margins with scissors. Nobody doubts it still shows a cat. A child shown all four versions learns the cat, not the frame.

The model is that child. Each warp is another photocopy in a new position. Where the analogy stops: you must also slide the sticky note marking the cat, or the child learns the wrong spot.

How It Actually Works

What moves and what stays

A geometric transform remaps coordinates. Horizontal flip sends column xx to W−1−xW - 1 - x in zero based indexing, or W−xW - x in one based box math. Rotation by angle θ\theta turns (x,y)(x, y) around the center. Random crop picks a subwindow and rescales it to the network size. Scale and translation zoom and shift. The class label does not change for classification, but boxes, masks and keypoints must follow the same warp.

A worked box flip

Image width W=100W = 100. Box xmin⁡=10x_{\min} = 10, xmax⁡=30x_{\max} = 30, ymin⁡=20y_{\min} = 20, ymax⁡=40y_{\max} = 40. Flip horizontally:

xmin⁡′=W−xmax⁡=100−30=70,xmax⁡′=W−xmin⁡=100−10=90x'_{\min} = W - x_{\max} = 100 - 30 = 70, \quad x'_{\max} = W - x_{\min} = 100 - 10 = 90

yy values stay 2020 and 4040. The box [10,20,30,40][10, 20, 30, 40] becomes [70,20,90,40][70, 20, 90, 40]. Same cat, mirrored position. For detection you must write the new box to the label file. Forgetting this step is the most common bug in augmentation pipelines.

When to use which warp

Horizontal flip is safe for most natural scenes and nearly free. Random crop with scale 0.80.8 to 1.01.0 teaches scale tolerance. Small rotations up to 1515 degrees help tilted photos. Larger rotations, vertical flips and heavy shear change the meaning for text, faces and medical scans, so gate them by task.

Code

import numpy as np

def hflip_box(boxes: np.ndarray, width: int) -> np.ndarray:    """Mirror boxes for a horizontal flip. Boxes are [x_min, y_min, x_max, y_max]."""    out = boxes.copy()    out[:, 0] = width - boxes[:, 2]  # new x_min from old x_max    out[:, 2] = width - boxes[:, 0]  # new x_max from old x_min    return out

boxes = np.array([[10.0, 20.0, 30.0, 40.0]])print(hflip_box(boxes, 100))  # -> [[70. 20. 90. 40.]]

Watch Out For

Flipping what must not flip

Digits, text and traffic scenes break under mirroring. A flipped 3 becomes a different glyph, and a flipped left arrow means right. The symptom is validation accuracy that drops after augmentation that helped on natural photos. Keep a no flip list per dataset and check it before enabling mirrors.

Moving pixels without moving boxes

Flipping the image but keeping the old box teaches the detector that cats sit where they no longer are. Loss falls while mean average precision stalls, because the model chases ghosts. Transform boxes, masks and keypoints with the same matrix, then clip boxes to the frame and drop empty ones.

The Quick Version

  • Geometric augmentation moves pixels while keeping the class label, teaching position and scale tolerance.
  • A 100 pixel wide flip maps a box edge at 30 to 70 and at 10 to 90.
  • Boxes, masks and keypoints must follow the same warp or labels turn wrong.
  • Horizontal flips and small crops are safe defaults, while vertical flips and large rotations need task approval.