Anchor Boxes

Detectors do not invent boxes. They start from a fixed set at every position and learn how to nudge them.

Overview

Why detectors do not just predict boxes

A detector must output an unknown number of boxes at unknown positions and sizes. Neural networks are much better at producing a fixed-size output than a variable-length one, and much better at making a small correction than at producing a coordinate from nothing.

Anchors convert the hard problem into the easy one. Place a fixed set of reference boxes at every position of a feature map, and ask the network two things per anchor: is there an object here, and how should this box be adjusted to fit it?

The output is now a fixed-size tensor, and the regression target is a small offset instead of an absolute position.

Anchor Boxes

This module needs JavaScript: the images are computed in the page rather than downloaded.

Worth knowing

At every position in a feature map the detector places several boxes of fixed size and aspect ratio.
Each anchor is assigned by IoU against the ground truth: high is positive, low is negative, and the band between is ignored.
The network predicts an offset from its anchor, not a box. Regressing a small correction is far easier than regressing coordinates.
Anchor-free detectors predict the box directly from a point, which removes the tuning but not the assignment problem.

Anchor Boxes

The prior that turns 'find every object' into 'adjust these boxes slightly', and the assignment rule that trains it.

What the visualisation shows

The dashed grid is the feature map: one set of anchors per cell. The orange rectangle is a ground-truth object. Drag the cell control to move the anchor set around, and the size control to change the scale.

Three anchors are drawn at each position, one per aspect ratio — wide, square and tall — because objects come in shapes and one square box fits few of them. Real detectors use three scales as well, so nine anchors per position is a common arrangement.

The readout gives the best IoU against the ground truth and what that IoU means for training.

The assignment rule

Every anchor gets a label before training, decided by IoU:

IoU with the best ground-truth boxLabel
Above ~0.5positive — predict this object, regress the offset
Below ~0.3negative — predict background, no box loss
In betweenignored — contributes nothing to the loss

The ignore band exists because those anchors are genuinely ambiguous, and forcing them either way teaches the network something untrue.

The page opens on a positive anchor. Drag the size control down and the readout walks through all three labels — positive, then ignored, then negative — without the ground-truth box moving at all. That transition is where an anchor stops being useful for an object, and it is why the anchor set has to match the objects you expect.

The imbalance nobody warns you about

A detector places tens of thousands of anchors on an image. An image with three objects makes almost every one of them negative — ratios of a thousand to one are normal.

Trained naively, the classification loss is dominated by easy background anchors and the model learns to say "background" very confidently and nothing else. Three responses became standard:

Hard negative mining — keep only the worst-scoring negatives so the ratio stays around 3:1.

Focal loss — down-weight examples the model already gets right, so the easy negatives stop dominating. This is what RetinaNet introduced, and it is why one-stage detectors caught up with two-stage ones.

Two-stage detection — a region proposal network filters down to a few hundred candidates before classification, which sidesteps the imbalance.

Choosing the anchor set

Anchors are a prior, and a wrong prior is expensive. Scales and ratios tuned for everyday photographs do badly on aerial imagery, where objects are small and often square, or on text detection, where boxes are extremely wide.

YOLOv2 introduced picking them by running k-means over the ground-truth box shapes in the training set, using IoU as the distance. That replaced a hand-tuned guess with a fitted one, and it is still the sensible default when a dataset's objects are unusual.

Anchor-free detectors

FCOS, CenterNet and DETR drop anchors entirely. FCOS predicts, for each point inside an object, the four distances to the box edges. CenterNet predicts object centres as a heatmap. DETR predicts a fixed set of boxes directly and matches them to ground truth with a Hungarian assignment.

They remove the anchor hyperparameters, which is a real simplification. What they do not remove is the assignment problem — deciding which prediction is responsible for which object — which is simply solved differently.

Where it goes wrong

Default anchors on unusual data. Check the IoU distribution of your anchors against your ground-truth boxes before training.

Ignoring the imbalance. Without focal loss or mining, the model predicts background everywhere and the loss looks fine.

Too many anchors. Each one costs memory and computation at every position of every feature map, and past a point they overlap so much that extra ones add nothing.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does the network predict for each anchor?

  2. Why do anchors with middling IoU get ignored rather than labelled?

  3. Why does focal loss exist?

Cheat sheet

Anchor Boxes

A detector must output an unknown number of boxes at unknown positions and sizes. Neural networks are much better at producing a fixed-size output than a variable-length one, and much better at making a small correction than at producing a coordinate from nothing.

COMPUTER VISION · vizlearn.in/computer_vision/anchor_boxes.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.