← All answersObject detection concepts

What are anchor boxes, and how are their aspect ratios determined?

Short answer

Anchor boxes are a fixed set of reference rectangles, in a few sizes and aspect ratios, tiled across the image. Instead of predicting box coordinates from scratch, the detector predicts small offsets from the nearest anchor, which is much easier to learn. The best aspect ratios are typically found by running k-means clustering on the width/height of the boxes in your training set.

Predicting a box from nothing is hard; nudging a sensible starting shape is easy. Anchors are those starting shapes.

You define, say, 3 scales and 3 aspect ratios (tall, square, wide) = 9 anchors per location. The network then only has to predict how much to shift and scale the anchor to fit the real object, plus a confidence.

Where the aspect ratios come from

The classic approach (from YOLOv2 onward) is to run k-means on the ground-truth box dimensions in your dataset, using IoU as the distance. The cluster centers become your anchors, so they match the shapes you actually have. A dataset of pedestrians wants tall anchors; a dataset of cars wants wide ones.

Note that many modern detectors have gone anchor-free and dropped hand-tuned anchors entirely, which removes this dataset-specific tuning step. See the anchor-based vs anchor-free question.

Stuck on this in a real project?

Book a free call and I’ll give you a straight answer on your specific case.

Book a consultation