The Price of One More Box

Using Vision ModelsHard+30 XP+30

The Price of One More Box

A detector does not return one answer. It returns a pile of boxes, each with a confidence, and scoring that pile against the truth needs two decisions the lesson named: how much overlap counts as a hit, and how confident the detector must be before it is allowed to speak.

Turn the second dial up and the detector reports only what it is sure of — fewer boxes, more of them right. Turn it down and it catches more objects but reports more junk. There is no single correct setting, which is why detection results are reported as mAP, averaged across the whole curve rather than read at one setting. You are going to produce that curve.

Boxes are [x, y, w, h]: the top-left corner, then the width and height, with xxx running right and yyy running down. A prediction carries a fifth number, its confidence.

Task: write detection_scoreboard(truths, predictions, iou_threshold, confidence_levels), returning one [precision, recall] pair per confidence level, in the order given, each number rounded to 4 decimal places.

At each confidence level, score the detector like this:

  1. Keep every prediction whose confidence is ≥\ge≥ the level. The rest were never reported, so they can neither help nor hurt.
  2. Order what survives by confidence, highest first. Equal confidences keep the order they were given in.
  3. Match, one prediction at a time in that order. A prediction claims the unclaimed truth box it overlaps most, measured by IoU — the shared area divided by the total area the two cover between them. If that best overlap is below iou_threshold, or every truth it overlaps is already claimed, it claims nothing. Ties in IoU go to the earlier truth in the list.
  4. Count. A prediction that claimed a truth is a hit. Then

precision=hitsboxes kept,recall=hitstruth boxes\text{precision} = \frac{\text{hits}}{\text{boxes kept}}, \qquad \text{recall} = \frac{\text{hits}}{\text{truth boxes}}precision=boxes kepthits​,recall=truth boxeshits​

If the level filters away every prediction, the detector reported nothing and so got nothing wrong: report precision 1.0 and recall 0.0. There is always at least one truth box.

Step 3 is the whole problem. Consider one truth box [0, 0, 4, 4] and two predictions, [0, 0, 4, 4, 0.9] and [1, 0, 4, 4, 0.6], at iou_threshold = 0.5:

  • The first is an exact match, IoU =1.0= 1.0=1.0, and claims the truth.
  • The second overlaps that same truth by 12/20=0.612 / 20 = 0.612/20=0.6, comfortably over the threshold — and it is still wrong, because the object has already been found. Two boxes on one object means one detection and one false alarm.
  • At confidence_levels = [0.5, 0.8] the answer is therefore [[0.5, 1.0], [1.0, 1.0]]: at 0.50.50.5 both boxes are reported and half are right; at 0.80.80.8 the second is never reported and the detector looks perfect.

That second row is the trade-off in miniature — the same detector, the same weights, a better-looking number, and not one extra object found.

A detector that reports every plausible box would have near-perfect recall and be useless. Precision alone is just as easy to fake by reporting almost nothing. Only the pair means anything, and only with the IoU threshold quoted alongside it.

Examples

truths = [[0, 0, 10, 10], [20, 20, 10, 10]]
predictions = [[0, 0, 10, 10, 0.9], [1, 1, 10, 10, 0.8], [20, 20, 8, 8, 0.6], [50, 50, 10, 10, 0.3]]
iou_threshold = 0.5
confidence_levels = [0.0, 0.5, 0.7, 0.95]
returns [[0.5, 1.0], [0.6667, 1.0], [0.5, 0.5], [1.0, 0.0]]
truths = [[10, 10, 20, 20], [60, 10, 10, 20]]
predictions = [[12, 12, 20, 20, 0.8], [60, 10, 10, 20, 0.4]]
iou_threshold = 0.75
confidence_levels = [0.3]
returns [[0.5, 0.5]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run