Kumita← Open visual lesson

Make the box fit

The math behind the picture

The classifier says Watermelon for a search window, but not where the watermelon is. A box says where; four predicted numbers reshape the window into a tighter box.

Connection 1 of 4

Use the candidate as a coordinate frame

A box is two corners, or a centre with a width and height. The search window runs from (48, 24) to (128, 104): 80 × 80, centre (88, 64). A horizontal adjustment tx is measured in box widths: the trained head gives tx = −0.20617, so the centre moves −0.20617 × 80 = −16.49 pixels. The vertical adjustment ty = +0.16483 uses the height: +13.19 pixels.

new centre x = old centre x + tx × old width

Read it as: tx is a relative displacement, not an absolute image coordinate.

Connection 2 of 4

Why multiply size by an exponential?

The trained box head's four adjustments decode the search window into the predicted box (43.26, 50.28) to (99.75, 104.09), computed from the window's features alone.
From guided stage 5 · Static lesson frame

The trained box head's four adjustments decode the search window into the predicted box (43.26, 50.28) to (99.75, 104.09), computed from the window's features alone.

Width and height must stay positive. Multiplying a positive width by exp(tw) guarantees a positive new width. A zero tw gives a factor of one, preserving the size. Here tw = −0.34783 gives a factor 0.7062, so the width shrinks from 80 to 56.50; th = −0.39659 gives 0.6726 and a height of 53.81. The centre does not move.

new width = old width × exp(tw)

Read it as: To enlarge width 100 to 120, use tw = log(1.2), whose exponential is 1.2.

Connection 3 of 4

Overlap is an evaluation, not an instruction

IoU of the predicted box and the separately supplied reference: 2588.50 ÷ 3205.57 = 0.8075, beside identical, partial, containment, touching and disjoint cases.
From guided stage 9 · Static lesson frame

IoU of the predicted box and the separately supplied reference: 2588.50 ÷ 3205.57 = 0.8075, beside identical, partial, containment, touching and disjoint cases.

Once a predicted box exists, compare it with a labelled reference. Intersection is the shared area. Union is everything covered by either box, counting their shared area only once.

union = area(A) + area(B) − intersection
IoU = intersection / union

Read it as: Predicted (43.26, 50.28)–(99.75, 104.09) and reference (49, 52)–(103, 103) share 50.7549 × 51 = 2588.50; the union is 3040.07 + 2754 − 2588.50 = 3205.57, so IoU = 0.8075.

Connection 4 of 4

A low IoU does not tell the box where to go

Two bad boxes can have the same IoU while needing opposite corrections. IoU alone does not specify the four movements. The trained head predicts corrections from image features; the reference is used to evaluate the result afterward. The original window contains the whole reference, yet scores only 0.4303: its extra area counts in the union.

IoU measures box overlap only, not class correctness or confidence; touching boxes score 0, and an empty union is defined as 0. The animation unfolds one prediction for us to inspect; it never consults the reference.

Connect it back to the picture

The four box corrections create a prediction. IoU measures its geometric overlap when a reference is available; it does not supply the correction directions.

Full notation and technical reference

The expressions below collect the operations discussed above. You can return here when you want the compact notation.

x₂ = x₁ + w   y₂ = y₁ + h   centre = (x₁ + w/2, y₁ + h/2)
c′ₓ = cₓ + tₓw   c′ᵧ = cᵧ + tᵧh   w′ = w·exp(tw)   h′ = h·exp(th)
IoU = intersection area / (area A + area B − intersection area)
  • Coordinates are continuous picture pixels: origin top-left, x right, y down; a box covers [x₁, x₂) × [y₁, y₂).
  • Training targets run the rules backwards from a reference: tₓ* = (cₓ,ref − cₓ)/w, tw* = ln(w_ref/w). At inference there is no reference.

Numerical reference

The search window (48, 24) to (128, 104) gets t = (−0.20617, +0.16483, −0.34783, −0.39659) from the trained head: the centre moves −16.49 and +13.19 pixels, the sides become 56.50 × 53.81, giving (43.26, 50.28) to (99.75, 104.09). Against the reference (49, 52) to (103, 103): intersection 2588.50, union 3205.57, IoU 0.8075.

Where this explanation stops

IoU measures box overlap only: not class correctness, confidence or the share of the object's pixels. Touching or separate boxes score 0, and an empty union is defined as 0. Cutoffs such as 0.5 are chosen per task. This encoding is common in two-stage detectors but not universal.

Follow the same idea in the Python or JavaScript notebook →