Search

Search papers and lessons

Anchor Box

Level 3 · 11 min read

An anchor box is a fixed template box of a chosen size and shape, placed at many image positions, that a detector matches to real objects and then corrects with small offsets.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • Neural network basics
  • Computer vision
  • ReadReady to readNot yet

TL;DR

An anchor box is a template box with a fixed size and shape, such as 128 pixels wide and 64 tall. A detector places many anchors across the image, at every position of its feature grid and at several sizes and shapes. Each anchor is matched to the real objects it overlaps, and the network only has to predict a small correction from the anchor to the true box. Anchors turn the hard question "where is the box?" into an easier one: "which template is close, and how should it move?"

Builds on: Object Detection, Bounding-Box Regression, Intersection over Union · Leads to: region proposal networks, and the one-stage detectors of the YOLO and SSD families

Where it's used

Driving perception. A self-driving car must find cars, cyclists and pedestrians in every frame. Detectors in the Faster R-CNN and YOLO families use anchors of several shapes, because a nearby pedestrian is tall and thin while a bus at the same distance is wide and low.

Satellite and aerial imagery. Ships, aircraft and vehicles appear at very different sizes in the same scene. A set of anchors across several scales covers both a few-pixel boat and a large harbour crane in one pass.

Security and retail cameras. Counting people or products on shelves needs a box for each item. Anchors let the same detector handle crowds of small objects and the occasional large one without a separate model per size.

How it works

Trace one tiny instance: a single feature location at pixel (50, 50), three square anchors, and one ground-truth box that the detector should find. Boxes are written as centre x, centre y, width and height, in pixels.

  • Anchors at (50, 50): sizes 20, 40 and 80 pixels square.
  • Ground truth: centre (50, 50), 36 pixels square.
  1. Place the anchors. The detector puts the three templates at the same location. Check: the anchors differ only in size, so any difference in their match comes from size alone.

  2. Measure the overlap of each anchor with the true box. Use Intersection over Union. For the 40-pixel anchor, the overlap is the full 36 by 36 square, which is 1,296 pixels, and the union is 1,600 pixels, so IoU is 1296/1600=0.811296/1600 = 0.81. For the 20-pixel anchor, the overlap is 400 pixels, the union is 1,296, so IoU is about 0.310.31. For the 80-pixel anchor, the overlap is 1,296 and the union is 6,400, so IoU is about 0.200.20. Check: the best-sized template overlaps the object most, as it should.

  3. Label each anchor. The 40-pixel anchor has IoU 0.81, which is above the positive cut-off of 0.7, so it is labelled as containing the object. The 80-pixel anchor has IoU 0.20, below the negative cut-off of 0.3, so it is labelled background. The 20-pixel anchor sits in the ignore band between 0.3 and 0.7, so it is not trained on either way. Check: three anchors, three different labels.

  4. Compute the regression target for the positive anchor. Using the offset formulas from box regression, the centre offsets are (50−50)/40=0(50 - 50)/40 = 0 and (50−50)/40=0(50 - 50)/40 = 0. The size targets are ln⁡(36/40)=ln⁡0.9≈−0.105\ln(36/40) = \ln 0.9 \approx -0.105 for width and for height. Check: the true box is slightly smaller than the anchor, so the log target is negative.

  5. Decode a prediction. Suppose the network outputs (0.05,−0.02,−0.12,−0.10)(0.05, -0.02, -0.12, -0.10) for this anchor. The centre becomes (50+0.05⋅40,50−0.02⋅40)=(52,49.2)(50 + 0.05 \cdot 40, 50 - 0.02 \cdot 40) = (52, 49.2). The width becomes 40e−0.12≈35.540 e^{-0.12} \approx 35.5 and the height 40e−0.10≈36.240 e^{-0.10} \approx 36.2. Check: the decoded box is close to the true 36 by 36 square, 2 pixels off in the centre.

The key idea is step 4. The network never has to produce a pixel coordinate from scratch. It starts from a template that already has a plausible shape, so the outputs are small numbers near zero.

flowchart LR
    A["Place anchors at every grid position"] --> B["Match each anchor to ground truth by IoU"]
    B --> C["Label positive, negative or ignored"]
    C --> D["Regress offsets for positive anchors"]
    D --> E["Decode into boxes, then suppress duplicates"]

The math

Definition: Anchor box

An anchor is a box a=(xa,ya,wa,ha)a = (x_a, y_a, w_a, h_a) fixed in advance, with centre (xa,ya)(x_a, y_a) and size (wa,ha)(w_a, h_a). A detector assigns each anchor a label from its overlap with the ground-truth boxes, and for a positive anchor it predicts four offsets that map aa onto the matching ground-truth box g=(xg,yg,wg,hg)g = (x_g, y_g, w_g, h_g).

Step 2 in symbols: the overlap

For two boxes aa and gg, IoU(a,g)=∣a∩g∣∣a∪g∣\mathrm{IoU}(a, g) = \dfrac{|a \cap g|}{|a \cup g|}, where ∣a∩g∣|a \cap g| is the area they share and ∣a∪g∣=∣a∣+∣g∣−∣a∩g∣|a \cup g| = |a| + |g| - |a \cap g|. In the example, ∣a∩g∣=362=1296|a \cap g| = 36^2 = 1296 for the 40-pixel anchor, and ∣a∪g∣=402+362−1296=1600|a \cup g| = 40^2 + 36^2 - 1296 = 1600.

Step 3 in symbols: the labelling rule

Let τ+=0.7\tau_+ = 0.7 and τ−=0.3\tau_- = 0.3. An anchor is positive when IoU(a,g)≥τ+\mathrm{IoU}(a, g) \ge \tau_+ for some ground-truth box, and negative when its best IoU is below τ−\tau_-. Anchors in between are ignored. Each ground-truth box also makes its single best-overlapping anchor positive, so no object is left without an anchor.

Step 4 in symbols: the targets

For a positive anchor, the four targets are

tx=xg−xawa,ty=yg−yaha,tw=ln⁡wgwa,th=ln⁡hgha.t_x = \frac{x_g - x_a}{w_a}, \qquad t_y = \frac{y_g - y_a}{h_a}, \qquad t_w = \ln\frac{w_g}{w_a}, \qquad t_h = \ln\frac{h_g}{h_a}.

Centres are measured in units of the anchor's size, and sizes on a log scale.

Step 5 in symbols: the decoding

Given the network's outputs (t^x,t^y,t^w,t^h)(\hat t_x, \hat t_y, \hat t_w, \hat t_h), the predicted box is

x^=xa+t^xwa,y^=ya+t^yha,w^=waet^w,h^=haet^h.\hat x = x_a + \hat t_x w_a, \qquad \hat y = y_a + \hat t_y h_a, \qquad \hat w = w_a e^{\hat t_w}, \qquad \hat h = h_a e^{\hat t_h}.

The example gives x^=50+0.05⋅40=52\hat x = 50 + 0.05 \cdot 40 = 52 and w^=40e−0.12≈35.5\hat w = 40 e^{-0.12} \approx 35.5.

The training loss sums a classification term over the sampled anchors and a smooth regression term over the positive anchors only. Negative anchors have no box to regress toward, so they are excluded from the regression loss.

Worked examples

Example
Example 1: the overlap test (tiny)

Let the ground truth be a 36 by 36 square centred at (50, 50), and let the anchor be a 20 by 20 square centred at (50, 50). Find the IoU and the label.

  1. The anchor lies entirely inside the true box, so the shared area is the anchor's area: 20⋅20=40020 \cdot 20 = 400.
  2. The union is the true box's area, since the anchor adds nothing outside it: 36⋅36=129636 \cdot 36 = 1296.
  3. IoU is 400/1296≈0.309400/1296 \approx 0.309.
  4. That is above 0.3 and below 0.7, so the anchor is ignored.

Interpretation: a small anchor inside a big object is not a good template. It covers too little of the object to be trained as a positive, and it covers too much of the object to be trained as background.

Example
Example 2: two neighbouring anchors (standard)

Let the ground truth be the box with centre (30, 60), width 20 and height 40. Compare two anchors of the same size, 20 by 40, one centred at (30, 60) and one at (40, 60).

  1. The first anchor equals the ground truth exactly. Shared area 800, union 800, so IoU is 1.0. It is positive.
  2. The second anchor is shifted 10 pixels right. Its x-range is [30,50][30, 50] and the true box's x-range is [20,40][20, 40]. The shared x-range is [30,40][30, 40], width 10.
  3. Shared area: 10⋅40=40010 \cdot 40 = 400. Union: 800+800−400=1200800 + 800 - 400 = 1200.
  4. IoU is 400/1200=1/3≈0.333400/1200 = 1/3 \approx 0.333. It lies in the ignore band.
  5. Target for the first anchor: all four offsets are zero, since the anchor and the box coincide. Target for the second: tx=(30−40)/20=−0.5t_x = (30 - 40)/20 = -0.5.

Interpretation: a half-width shift already costs the anchor its positive label. Regression can still correct it, but it is not trained on it, which keeps the labels sharp.

Example
Example 3: a car in a street image (realistic)

A feature location at pixel (304, 208) has a 128×64128 \times 64 anchor, which is a plausible shape for a car seen from the side. The true car box is centred at (300,200)(300, 200) with size 120×60120 \times 60.

  1. Anchor box: x from 240 to 368, y from 176 to 240. Area 128⋅64=8192128 \cdot 64 = 8192.
  2. True box: x from 240 to 360, y from 170 to 230. Area 120⋅60=7200120 \cdot 60 = 7200.
  3. Shared area: x-overlap 360−240=120360 - 240 = 120, y-overlap 230−176=54230 - 176 = 54, so 120⋅54=6480120 \cdot 54 = 6480.
  4. Union: 8192+7200−6480=89128192 + 7200 - 6480 = 8912.
  5. IoU: 6480/8912≈0.7276480/8912 \approx 0.727. This is above 0.7, so the anchor is positive.
  6. Targets: tx=(300−304)/128=−0.03125t_x = (300 - 304)/128 = -0.03125, ty=(200−208)/64=−0.125t_y = (200 - 208)/64 = -0.125, tw=ln⁡(120/128)=ln⁡0.9375≈−0.0645t_w = \ln(120/128) = \ln 0.9375 \approx -0.0645, and th=ln⁡(60/64)≈−0.0645t_h = \ln(60/64) \approx -0.0645.

Interpretation: the anchor was a good enough template that the regressor only has to make a few-percent corrections. An image has many such positions, and training uses only a sample of the anchors, so the same kind of match repeats across the picture.

Try it: Keep the same anchor and the same car centre, but suppose the car is 135 pixels wide instead of 120, with its height still 60. What is the width target twt_w?

Answer: tw=ln⁡(135/128)=ln⁡1.0547≈0.053t_w = \ln(135/128) = \ln 1.0547 \approx 0.053. The sign is now positive, because the car is larger than the anchor.

Common mistakes and tradeoffs

It is tempting to think that anchors are the final boxes the detector outputs. In fact they are only starting templates. The network outputs offsets from them, and a detector without regression would be limited to the anchors' fixed shapes.

It is tempting to think that more anchors always give better detection. In fact most anchors are negatives, so a very dense set makes training dominated by background, and the number of IoU computations grows with the product of the anchor count and the number of objects. Detectors therefore sample a balanced subset of anchors for each training image.

It is tempting to think that the default sizes always fit your data. In fact the best sizes depend on the dataset. YOLOv2 chose its anchor shapes by running k-means on the training boxes, which fits the templates to the objects that actually occur.

Use anchors when you want a mature, well-understood one-stage or two-stage detector and you can tune the sizes to your data. Consider anchor-free detectors such as FCOS (2019) or CenterNet (2019) when you want to avoid tuning those sizes and the matching rules, at the cost of a different set of design choices.

History

Anchors were introduced in the region proposal network of Faster R-CNN (Ren, He, Girshick and Sun, 2015), which used nine anchors per location: three scales and three aspect ratios. The Single Shot MultiBox Detector (Liu and colleagues, 2016) used a closely related set of default boxes, and YOLOv2 (Redmon and Farhadi, 2016) brought in k-means to choose the shapes. The idea caught on because it turned a hard regression from scratch into a classification over a fixed grid of templates followed by a small correction, which is easier to train and faster to run.

Recap

  • An anchor is a fixed template box placed at many positions and at several sizes.
  • Each anchor is labelled by its IoU with the true boxes: positive above 0.7, negative below 0.3, ignored in between.
  • Positive anchors are trained to predict offsets in units of the anchor's size, with log scales for the size.
  • Decoding the offsets gives the final box, and a later step removes duplicates.
  • Read next: region proposal networks, which use anchors to propose objects, and non-maximum suppression, which removes duplicate boxes.

Check yourself

QuestionWhat does a detector do with an anchor whose IoU with the best ground-truth box is 0.5?Show answer

It ignores it, unless that anchor is the single best match for some ground-truth box, in which case it is made positive anyway so that no object lacks an anchor. Otherwise the rule uses one positive threshold (0.7) and one negative threshold (0.3), and anchors between them contribute no positive or negative label, because they are neither good enough to teach as objects nor clearly background.

QuestionAn anchor is 40 by 40 and the true box is 30 by 40, with the same centre. What are the width and height targets, and what is the IoU?Show answer

The width target is tw=ln⁡(30/40)=ln⁡0.75≈−0.288t_w = \ln(30/40) = \ln 0.75 \approx -0.288. The height target is ln⁡(40/40)=0\ln(40/40) = 0. The shared area is 30⋅40=120030 \cdot 40 = 1200 and the union is 1600+1200−1200=16001600 + 1200 - 1200 = 1600, so the IoU is 1200/1600=0.751200/1600 = 0.75, which is positive.

QuestionWhy do detectors measure the centre offset in units of the anchor's size rather than in raw pixels?Show answer

Because then a small object and a large object produce targets of similar magnitude. A 10-pixel error on a 20-pixel anchor is a target of 0.5, and the same 10-pixel error on a 200-pixel anchor is 0.05, so one loss scale works across sizes instead of being dominated by large objects.

QuestionA detector has a 40 by 60 feature grid, with 9 anchors at each location. How many anchors does it place in one image, and why is it still a problem if most are negatives?Show answer

There are 40⋅60⋅9=21,60040 \cdot 60 \cdot 9 = 21{,}600 anchors. Most of them cover background, so if all were trained equally, the background labels would dominate the classification loss and the detector would learn to predict background everywhere. This is why detectors sample a balanced subset of anchors for training.

Sources