Skip to main content

Bounding-Box Regression and Generalized IoU

Learn how normalized box coordinates, L1 loss, IoU, and Generalized IoU guide object localization, including disjoint boxes and containment limits.

Bounding boxes describe an object's location, and intersection over union measures overlap. A detector must also learn to move an inaccurate box toward its target. What numerical signal tells it how wrong the location is, especially before the boxes touch?

Box regression predicts continuous coordinates. A training loss compares those coordinates or their geometry with an annotated box. Start with one prediction already paired with one target; deciding which prediction belongs to which target comes later.

Four numbers in image coordinates

Use b=(cx,cy,w,h)b=(c_x,c_y,w,h): horizontal center, vertical center, width, and height. Divide horizontal pixel coordinates and widths by image width, and vertical coordinates and heights by image height. Each component is dimensionless. For example, a box centered at pixel (120, 100) with size (80, 40) in a 400-by-200 image becomes (0.3,0.5,0.2,0.2)(0.3,0.5,0.2,0.2).

These are absolute normalized box parameters, not offsets from an anchor. Other detector families can regress anchor-relative offsets. Here widths and heights are strictly positive, and the explorer keeps corners inside the image.

The simplest discrepancy is the sum of four absolute differences:

L1(b^,b)=∣c^x−cx∣+∣c^y−cy∣+∣w^−w∣+∣h^−h∣.L_1(\hat b,b)=|\hat c_x-c_x|+|\hat c_y-c_y|+|\hat w-w|+|\hat h-h|.

For the target (0.3,0.5,0.2,0.2)(0.3,0.5,0.2,0.2) and prediction (0.7,0.5,0.2,0.2)(0.7,0.5,0.2,0.2), only the horizontal center is wrong, so L1=0.4L_1=0.4. This is a sum, not the mean of the four coordinates. A positive error supplies a direction toward the target through the loss derivative; at zero error, the absolute value has a kink.

From coordinates to overlap

To compute areas, convert each box to corners:

(x0,y0,x1,y1)=(cx−w2,cy−h2,cx+w2,cy+h2).(x_0,y_0,x_1,y_1)=\left(c_x-\frac w2,c_y-\frac h2,c_x+\frac w2,c_y+\frac h2\right).

The intersection has width max⁡(0,min⁡(x^1,x1)−max⁡(x^0,x0))\max(0,\min(\hat x_1,x_1)-\max(\hat x_0,x_0)), with the analogous expression for height. Multiply them to get intersection area II. Union area is U=Ab^+Ab−IU=A_{\hat b}+A_b-I, so IoU⁡=I/U\operatorname{IoU}=I/U. Areas here are fractions of the image area; IoU is a ratio with no units.

A loss 1−IoU⁡1-\operatorname{IoU} directly rewards overlap. But if two boxes remain separated under small coordinate changes, their intersection stays zero. The loss stays at one, and its local derivative is zero. It cannot distinguish a small gap from a large gap in this region.

Let the enclosure expose empty space

Generalized IoU adds the smallest axis-aligned rectangle CC enclosing both boxes:

GIoU⁡=IoU⁡−∣C∣−U∣C∣,LGIoU=1−GIoU⁡.\operatorname{GIoU}=\operatorname{IoU}-\frac{|C|-U}{|C|},\qquad L_{\mathrm{GIoU}}=1-\operatorname{GIoU}.

The correction measures enclosure area not covered by either box. It can provide a changing loss when overlap is zero, as introduced in the GIoU paper by Rezatofighi and colleagues. GIoU can be negative; its loss can exceed one.

Move the predicted center and size below. Compare Disjoint with Overlap, then select Contained and shift its center slightly. The values are calculated from authored boxes, not measured detector outputs.

One prediction, one target
Geometry
0.700
0.500
0.200
0.200
Approximate slopes
Image coordinates: x and y from 0 to 1
PredictionTargetEnclosure (dashed)

Authored analytic geometry, rounded to three decimals. Areas are fractions of image area. Target (cx, cy, w, h) = (0.3, 0.5, 0.2, 0.2). All changes are manual.

In the disjoint preset, both boxes have area 0.040.04. Their union is 0.080.08 and their enclosing rectangle has area 0.120.12. Thus IoU⁡=0\operatorname{IoU}=0, GIoU⁡=−(0.12−0.08)/0.12=−1/3\operatorname{GIoU}=-(0.12-0.08)/0.12=-1/3, and LGIoU=4/3L_{\mathrm{GIoU}}=4/3. Moving the prediction left shrinks the enclosure before the boxes overlap.

In the overlap preset, shifting the prediction's center to 0.40.4 gives intersection 0.020.02 and union 0.060.06, so IoU is 1/31/3. Here the boxes jointly fill the enclosure, so GIoU also equals 1/31/3.

GIoU has limits too

Containment is a useful counterexample. A centered prediction of width and height 0.10.1 lies inside the target of width and height 0.20.2. Its area is 0.010.01 and the target's area is 0.040.04, giving IoU 0.250.25. The enclosure equals the union, so the GIoU correction is zero: GIoU is also 0.250.25.

Translate that small box while it remains fully contained. Neither its area nor the union changes, so both losses stay constant even though its center moves. GIoU is not a universal cure for unhelpful derivatives. Coordinate L1 still detects that center error. Minima, maxima, and absolute values also make these objectives piecewise smooth rather than differentiable everywhere.

The optional slope readout approximates a derivative by evaluating each loss at cx+εc_x+\varepsilon and cx−εc_x-\varepsilon, then dividing the difference by 2ε2\varepsilon, with ε=0.001\varepsilon=0.001. It is not an automatic gradient. At exact alignment or another kink, a symmetric finite difference can be zero even when moving either way increases the loss.

Put the losses back into a detector

Original DETR combines coordinate and geometric supervision for matched boxes:

Lbox=λL1L1(b^,b)+λGIoU(1−GIoU⁡(b^,b)).L_{\mathrm{box}}=\lambda_{L1}L_1(\hat b,b)+\lambda_{\mathrm{GIoU}}(1-\operatorname{GIoU}(\hat b,b)).

Its official box utility uses the order (cx,cy,w,h)(c_x,c_y,w,h) and converts to corners for GIoU. The official criterion sums coordinate errors for matched pairs and normalizes losses by the number of target boxes. The expression above describes one pair; classification supervision and weighting remain separate parts of training.

An evaluation rule such as “count a detection as localized when IoU is at least 0.5” is a discrete scoring decision. It is not the continuous box training loss, and training assignment is not evaluation matching. See object detection metrics for that distinction.

Next, set prediction and bipartite matching shows how a detector pairs an unordered set of predictions with targets before applying these losses. Then modern object detection places the box head, matching, and classification into the complete pipeline.

If you found this explanation helpful, consider sharing it with others.

Mastodon