Detection matching and average precision
Quick answer
Detection evaluation asks whether predicted objects match annotated objects. Sort predictions by confidence and match each at most once to an unmatched ground-truth object of the same class and image, subject to an IoU threshold. Examples are static Python snippets tested locally on CPU; Torchvision is required for selected examples. ByteMind execution is unverified.
Duplicate box suppression
import torchfrom torchvision.ops import nmsb=torch.tensor([[0.,0.,4.,4.],[0.,0.,4.,4.],[5.,0.,9.,4.]])s=torch.tensor([.9,.8,.7])print(nms(b,s,.5).tolist())Expected output (locally tested): [0, 2]. The second identical box is suppressed; the disjoint third remains.
How it works
For this teaching protocol, sort descending confidence, breaking ties by prediction ID. Match the highest-IoU eligible unmatched object, breaking ties by ground-truth ID. A duplicate, wrong class, wrong image or low-IoU prediction is a false positive; unmatched ground truth is a false negative. At each rank precision=TP/rank and recall=TP/number of ground truths. The teaching AP sums precision at TP ranks divided by total ground truths, including missed objects; this is non-interpolated AP. Compute per class, exclude classes without ground truth (report them), and average classes equally for mAP at each threshold. COCO uses a precision envelope sampled at 101 recall levels and evaluates thresholds 0.50 to 0.95 by 0.05, with additional area, max-detection and crowd/ignore rules; do not label this simplified evaluator COCO AP. NMS is a prediction postprocessing step: greedily keep highest-scoring boxes and suppress lower-scoring same-class boxes with IoU strictly greater than its threshold. NMS threshold and evaluation threshold are distinct. NMS can suppress nearby true objects; do not choose thresholds on test labels.
Ranked AP with a missed object
from fractions import Fractionflags=[1,0,1]; tp=0; total=Fraction(0)for rank,flag in enumerate(flags,1): tp+=flag if flag: total+=Fraction(tp,rank)print(total/3)Expected output (locally tested): 5/9. The missed third object remains in the denominator.
Common mistakes
Do not count two predictions of one ground truth as two true positives. Do not divide AP by number of detections instead of ground truths. Equal scores need a reproducible tie policy; native NMS may choose different tied boxes across devices.
Practice and next steps
For TP, FP, TP and three ground truths calculate AP=(1+2/3)/3. Compare with the case in which no ground truths are missed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Detection matching and average precision — knowledge check
Apply detection matching and average precision to small examples and debugging scenarios.
A second prediction matches an already claimed object. Count it as?
What is the AP denominator in the teaching protocol?
Why report AP at several IoU thresholds?
Does this teaching AP equal COCO AP?
Where choose NMS threshold?