Applications of Machine Learning

Lecture 14 · Animations

Detection and segmentation

Non-maximum suppression thinning a cluster of candidate boxes one round at a time. Press Step to advance one round and read what happened; press Play to let it run.

All boxes, scores and overlaps on this page are synthetic, generated in your browser from a fixed seed, so Reset always gives back the same picture. None of it comes from COCO.

1Non-maximum suppression, one round at a time

Fourteen scored candidate boxes over two objects that overlap each other, the way two people standing close do. The dotted outlines are the objects themselves; suppression never sees them. Each Step is one round of the greedy rule: keep the highest remaining score, then delete every remaining box whose IoU with it is above the threshold. The list on the right is the ranking.

The mathematics

Let $S$ be the candidates sorted by score. Repeat until $S$ is empty:

  1. remove the first box $b^\ast$ from $S$ and keep it;
  2. remove from $S$ every $b$ with $\mathrm{IoU}(b, b^\ast) > t$.

This is torchvision.ops.nms, which discards a box when its IoU is strictly above iou_threshold. Every round keeps exactly one box, so the number of rounds equals the number of boxes kept. The rule is greedy: a box suppressed in round $k$ is never compared with anything again.

Watch for
  • Round 1 keeps the 0.97 box on object 1 and deletes its duplicates. The best box on object 2 survives only because its IoU with the 0.97 box is below $t$.
  • The two stray boxes, which cover no object, survive at every threshold. Suppression removes duplicates, not mistakes; that is the score threshold's job.
  • Once a box is kept, nothing later can remove it. The order of the scores decides everything.
Try this
  1. Lower $t$ to 0.2 and press Play. Box #2, the best on object 2, overlaps #1 with IoU 0.23 and is suppressed; object 2 survives only through a worse box. Lower $t$ to 0.1 and object 2 disappears entirely: two objects become one detection, the deck's failure condition.
  2. Raise $t$ to 0.7. Duplicates on both objects survive, because their IoU with the kept box is below 0.7.