Search

Search papers and lessons

Bounding-Box Regression

Level 3 · 12 min read

Bounding-box regression trains a model to nudge a rough box around an object into a tight one, by predicting small offsets in position and log-scale size.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Calculus
  • Linear algebra
  • Classical models
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

Bounding-box regression is the step in an object detector that turns a rough rectangle around an object into a tighter one. The detector proposes a starting box, and a model predicts four small numbers: how far to move the centre and how much to scale the width and height. It matters because a detector that only says "there is a car here" is far less useful than one that says exactly where the car's edges are.

Builds on: Linear Regression, Loss Function · Leads to: Anchor Box, Region Proposal Network

Where it's used

Self-driving cars. A perception system must know where each car, cyclist and pedestrian is, to within a few pixels, so that the planner can keep a safe distance. The first detection is rough, and a box regression step tightens it.

Photo apps and cameras. Face boxes around people are refined the same way. A loose box makes a focus point or a face crop drift, so the tighter the box, the more reliable the result.

Warehouse and retail robots. A picking arm needs the box of a parcel or a product on a shelf, so it can plan a grip. The box is only useful if its edges are close to the real edges, which is why regression is trained on the difference between the rough and the true box.

Medical imaging research. Detectors that outline lesions in scans, such as nodules on a chest CT, also rely on box regression to tighten their first guess. This is an example of how the technique is built, not a tool for diagnosis.

How it works

Trace one tiny instance: a rough box called the anchor, and the true box that the model should learn to reach. Boxes are written as centre x, centre y, width and height, in pixels. Image coordinates grow downwards, so a smaller y means higher on the screen.

  • Anchor: centre (50, 50), width 40, height 20.
  • True box: centre (54, 48), width 60, height 20.
  1. Start with the anchor. The detector's rough box is centred at (50, 50) and is 40 wide and 20 tall. Check: the true box is wider and sits a little to the right and above, so there is something to learn.
  2. Compute the targets. The regression target is not the raw pixel position. It is the offset, measured in units of the anchor's size. The centre moves by 54 minus 50, which is 4 pixels, and 4 divided by the anchor width 40 is 0.1. Check: a target of 0.1 means "move right by a tenth of the anchor's width". The vertical target works the same way, with the anchor's height: 48 minus 50 is minus 2, and minus 2 divided by 20 is minus 0.1.
  3. Encode the size on a log scale. The width changes from 40 to 60, a factor of 1.5. The target is the natural Logarithm of that factor, ln 1.5, about 0.405. The height does not change, so its target is ln 1, which is 0. Check: the log turns any positive size ratio into a number that can be negative or positive, which suits a linear output.
  4. Read the model's output. The model is a learned linear layer over the image features of the anchor, the same kind of weighted sum as in Linear Regression. The features come from a convolutional network run on the image. Suppose it predicts (0.08, −0.05, 0.30, 0.02). These are four numbers, in the same order as the targets: x offset, y offset, log width ratio, log height ratio.
  5. Decode the prediction into a box. Centre x is 50 plus 0.08 times 40, which is 53.2. Centre y is 50 plus (−0.05) times 20, which is 49. Width is 40 times the exponential of 0.30, about 54.0. Height is 20 times e to the 0.02, about 20.4. Check: the predicted box is close to the true box, with its centre 0.8 pixels off on x and 1 pixel off on y.
  6. Measure the error. The gap between predicted and target numbers is (0.08 minus 0.10), (−0.05 minus (−0.10)), (0.30 minus 0.405), (0.02 minus 0). Squared, these are 0.0004, 0.0025, about 0.011 and 0.0004. Their mean is about 0.0036. Training pushes this number down by adjusting the model's weights.

The key idea is step 2 and step 3 together. Because targets are measured in units of the anchor, a small object and a large object produce targets of similar size, so one loss scale works for both.

flowchart LR
    A["Rough anchor box"] --> B["Model predicts four offsets"]
    B --> C["Decode into a new box"]
    C --> D["Compare with true box"]
    D --> E["Update weights to shrink the error"]
    E --> B

The math

Definition: Box encoding

Let an anchor have centre (xa,ya)(x_a, y_a), width waw_a and height hah_a. Let the true box have centre (x,y)(x, y), width ww and height hh. The four regression targets are

tx=x−xawa,ty=y−yaha,tw=ln⁡wwa,th=ln⁡hha.t_x = \frac{x - x_a}{w_a}, \qquad t_y = \frac{y - y_a}{h_a}, \qquad t_w = \ln \frac{w}{w_a}, \qquad t_h = \ln \frac{h}{h_a}.

The model predicts four numbers, written t^x,t^y,t^w,t^h\hat t_x, \hat t_y, \hat t_w, \hat t_h, from the image features of the anchor.

Step 2 in symbols

The offsets in the first two targets divide by the anchor's own size, so they are unitless. Using the tiny instance, tx=(54−50)/40=0.1t_x = (54 - 50)/40 = 0.1, and ty=(48−50)/20=−0.1t_y = (48 - 50)/20 = -0.1.

Step 3 in symbols

The size targets are logarithms of ratios. Since ln⁡(w/wa)=ln⁡w−ln⁡wa\ln(w/w_a) = \ln w - \ln w_a, the model is predicting a difference of log-sizes, and every positive width comes back out of the exponential, so the predicted width is always positive:

w^=wa et^w,h^=ha et^h.\hat w = w_a \, e^{\hat t_w}, \qquad \hat h = h_a \, e^{\hat t_h}.

In the tiny instance, tw=ln⁡1.5≈0.405t_w = \ln 1.5 \approx 0.405.

Steps 4 and 5 in symbols

Decoding inverts the encoding. The predicted centre is x^=xa+t^xwa\hat x = x_a + \hat t_x w_a and y^=ya+t^yha\hat y = y_a + \hat t_y h_a. Applying the model's output (0.08,−0.05,0.30,0.02)(0.08, -0.05, 0.30, 0.02) to the anchor gives centre (53.2,49)(53.2, 49), width about 54.054.0 and height about 20.420.4.

Step 6 in symbols

Training minimises the mean squared error over the four targets, averaged over all anchors and images that the model sees:

L=14∑k∈{x,y,w,h}(t^k−tk)2.L = \frac{1}{4} \sum_{k \in \{x, y, w, h\}} \left( \hat t_k - t_k \right)^2.

This is a mean squared error written for four outputs. Some detectors use a loss that behaves like squared error for small misses and like absolute error for large ones, called smooth L1, which is less sensitive to a single very wrong box. Both are gradient-based, so training uses Gradient Descent on the weights.

Worked examples

Example
Tiny: moving the centre only

Anchor: centre (10, 10), width 10, height 10. True box: centre (12, 10), width 20, height 10.

  1. The centre moves 2 pixels right. Target: t_x = 2 divided by the anchor width 10, which is 0.2.
  2. The centre does not move vertically. Target: t_y = 0.
  3. The width doubles. Target: t_w = ln (20 divided by 10) = ln 2, about 0.693.
  4. The height does not change. Target: t_h = ln 1 = 0.
  5. Interpret: a perfect model outputs (0.2, 0, 0.693, 0). Output of (0, 0, 0, 0) would leave the anchor unchanged, which is the baseline the regression must beat.
Example
Standard: the walkthrough instance

Anchor: centre (50, 50), width 40, height 20. True box: centre (54, 48), width 60, height 20. The model predicts (0.08, −0.05, 0.30, 0.02).

  1. Decode the centre x: 50 plus 0.08 times 40 is 53.2.
  2. Decode the centre y: 50 plus (−0.05) times 20 is 49.
  3. Decode the width: 40 times e to the 0.30 is 40 times 1.350, which is 54.0.
  4. Decode the height: 20 times e to the 0.02 is 20 times 1.020, which is 20.4.
  5. Compare the four targets with the four outputs: the squared errors are 0.0004, 0.0025, 0.011 and 0.0004.
  6. Their mean is (0.0004 + 0.0025 + 0.011 + 0.0004) divided by 4, which is 0.0036.
  7. Interpret: the largest error is on the width. The model is too timid, predicting a width ratio of about 1.35 when the truth is 1.5. More training on wide objects would reduce this term.
Example
Realistic: does the refined box pass the usual test?

A detector scores a candidate box by overlap with the true box, measured by IoU. Take the true box from the standard example: x from 24 to 84, y from 38 to 58, area 1200. Take the predicted box from the same example: x from 26.2 to 80.2, y from 38.8 to 59.2, area about 1101.6.

  1. The overlap in x runs from 26.2 to 80.2, a width of 54.0.
  2. The overlap in y runs from 38.8 to 58, a height of 19.2.
  3. The intersection area is 54.0 times 19.2, which is 1036.8.
  4. The union is 1200 plus 1101.6 minus 1036.8, which is 1264.8.
  5. IoU is 1036.8 divided by 1264.8, which is about 0.82. Many benchmarks count a box as correct at 0.5 or higher, so this refinement passes comfortably.

Try it: the same anchor (centre (50, 50), width 40, height 20) with predicted t_w = −0.10 gives what predicted width? Answer: the width is 40 times e to the minus 0.10, which is 40 times 0.905, about 36.2 pixels. The model would shrink the box, which is the wrong direction for this true box, and the error term for the width would be about (−0.10 minus 0.405) squared, roughly 0.25.

Common mistakes and tradeoffs

It is tempting to think a detector should regress raw pixel coordinates. In fact offsets measured in units of the anchor work better, because a 10-pixel error means very different things on a 20-pixel object and on a 400-pixel object. Raw coordinates let large objects dominate the loss.

It is tempting to think a regressed width can be predicted as a plain number. In fact the log scale guarantees a positive width after decoding and treats a doubling and a halving as symmetric changes. A plain output can go negative and gives no natural scale.

It is tempting to think a small squared error on coordinates means a good box. In fact the overlap measure, IoU, is what the benchmark uses, and a small coordinate error on a small box can cut IoU sharply. Some detectors therefore train with a loss that targets IoU directly, which is a tradeoff: it matches the goal more closely but is harder to optimise.

It is tempting to think box regression also decides what the object is. In fact it only fixes the location and size. The class label comes from a separate classifier that looks at the same features, and the two are trained together. Several refined boxes often land on the same object, so a non-maximum suppression step later keeps the best one and removes the duplicates.

Box regression works best when the anchor is already near the object, which the proposal step is meant to ensure. When the anchor is far off, the offsets become large and the linear output struggles, and a second round of refinement helps. Alternatives include predicting corners directly, and anchor-free detectors that predict the centre first and then the size. Those drop the need to design anchor shapes but require different training tricks.

History

The R-CNN paper of 2014 by Girshick and colleagues ran a classifier on region proposals and added a regression step to correct each proposal's box. It is one of the papers that brought learned box regression into common use in detectors.

The 2015 Faster R-CNN paper by Ren and colleagues used anchor boxes at many shapes and scales from its region proposal network, and its regression predicted offsets relative to each anchor, using the encoding above with the log-scale size. This form has been widely reused in later detectors.

Recap

  • Bounding-box regression refines a rough anchor box into a tight box by predicting offsets.
  • Targets are the centre offsets divided by the anchor size, and the log ratio of the widths and heights.
  • Decoding inverts the encoding, and the exponential keeps every width and height positive.
  • Training minimises a squared-error loss over the four targets, by gradient descent on the weights.
  • The benchmark measures success by overlap, so later work often trains on overlap directly.

Read next: Anchor Box, which defines the starting boxes this regression refines, and Region Proposal Network, which proposes them.

Check yourself

QuestionAn anchor has centre (20, 30), width 10 and height 10. The true box has centre (20, 30), width 30 and height 10. What are the four targets?Show answer

The centre is unchanged, so t_x = 0 and t_y = 0. The width grows from 10 to 30, so t_w = ln (30 divided by 10) = ln 3, about 1.099. The height is unchanged, so t_h = ln 1 = 0. The targets are (0, 0, 1.099, 0).

QuestionAn anchor has centre (100, 100), width 50 and height 50. A model predicts (0.2, 0, ln 0.5, 0). Where is the predicted box, and what is its width?Show answer

The centre x is 100 plus 0.2 times 50, which is 110. The centre y is 100 plus 0 times 50, which is 100. The width is 50 times e to the ln 0.5, which is 50 times 0.5, so 25. The predicted box is centred at (110, 100) and is 25 wide and 50 tall. Its width is half the anchor's, which matches the log target ln 0.5.

QuestionWhy is the regression target measured in units of the anchor's size instead of in raw pixels?Show answer

A raw pixel error means different things on different objects. Ten pixels is a small miss on a large object and a large miss on a small one. Dividing by the anchor size makes the targets unitless and of similar scale across objects, so the loss does not let large objects dominate training.

QuestionA detector's box has IoU 0.40 with the true box, and the benchmark counts boxes at 0.50 or higher as correct. Name two things the regression could do to improve the box, and say why training on a squared-error loss alone might not guarantee this.Show answer

The regression could move the centre closer to the true centre, or correct the width and height ratios. Squared error on the four targets does not directly measure overlap, so a small coordinate error can still cost a lot of IoU on a small box. This is why some detectors train with a loss that targets IoU directly.

QuestionA model is trained only on boxes of people standing upright, and it is then asked to refine a box around a person lying down. Which part of the encoding is most likely to produce a wrong box, and why?Show answer

The width and height log ratios are most likely to be wrong, because the person's aspect ratio is reversed compared with the training data. The model has learned typical size ratios for upright people, so its predicted log-scale changes will be biased toward those. The centre offsets may still be reasonable because they depend on where the anchor sits.

Sources