One number for how well two boxes agree
- : the predicted box.
- : the ground truth box.
The overlap divided by the total area covered. It is 1 for a perfect match, 0 for boxes that do not touch, and scale free.
The union is computed by inclusion and exclusion rather than directly, which is why the intersection is the only geometry that has to be worked out.
Computing the intersection
The clamp at zero is the whole trick. Without it, two disjoint boxes produce a negative width times a negative height, which is a positive area and a silently wrong answer.
One evaluation, in order
- Compute IoU between every prediction and every ground truth box.
- Match each prediction to the ground truth it overlaps most.
- Call it correct if that IoU passes the threshold, usually 0.5.
- Count unmatched predictions as false positives and unmatched truths as false negatives.
- Sweep the confidence threshold and average the precision across it.
The standard detection metric averages that whole procedure across IoU thresholds from 0.5 to 0.95, so a model cannot win by being roughly right everywhere.
Why it is a poor loss, and what replaced it
Used directly as a loss, IoU has zero gradient whenever the boxes do not overlap, which is exactly the case where the model most needs to be told which way to move.
- : the smallest box enclosing both.
Generalised IoU adds a penalty for the empty space in that enclosing box, so two separated boxes still produce a gradient pulling them together.
Distance IoU penalises the distance between centres normalised by the diagonal of the enclosing box, which converges faster because it moves the box directly rather than through its area.
Where the threshold hides a decision
At 0.5 a box can be half wrong and still count as a detection, which flatters every model on large objects.
Small objects are the opposite case. A box a few pixels off on a 20 pixel object drops below the threshold, so the same absolute error is fatal at small scale and free at large scale.
IoU is also used inside non-maximum suppression to decide which overlapping predictions are duplicates, so the same number is doing two different jobs in one pipeline.