Intersection over Union (IoU)
Intuition
How well does a predicted box match the true box? Divide the overlapping area by the total area covered by either box. 1 = perfect match, 0 = no overlap.
Precise explanation
Boxes as [x1, y1, x2, y2] with , (continuous coordinates):
- area ;
- intersection corners: , , , ; width/height clipped at 0;
- .
Pairwise for
(m, 4),(n, 4): broadcastA[:, None, :]againstB[None, :, :]to get(m, n)matrices. Detection benchmarks count a prediction as correct if IoU ≥ 0.5 (or several thresholds).
Worked example
A = [0, 0, 2, 2], B = [1, 1, 3, 3]: intersection 1×1 = 1; union 4 + 4 − 1 = 7 → IoU 1/7 ≈ 0.143.
Common mistakes
- Forgetting to clip negative widths (non-overlapping boxes give negative "intersection").
- Mixing
[x, y, w, h]and[x1, y1, x2, y2]formats. - +1 pixel conventions from integer-pixel datasets (we use continuous coordinates).
Why this matters
IoU drives detection evaluation (mAP), anchor matching during training and non-maximum suppression (CV-24).
References
- Dive into Deep Learning — Aston Zhang, Zachary C. Lipton, Mu Li, Alexander J. Smola (
S013)