Week 08 · Object detection, IoU, and average precision

Match boxes once and see what duplicates and missed objects do.

← Week 7 · Subject contents · Week 9 →

Match boxes once and see what duplicates and missed objects do.

Work through the problem

Intersection over union is intersection area divided by union area. Two area-four boxes with intersection two have IoU 2/(4+4−2)=1/3. A detector can count a ground-truth object only once. A second prediction of that object is a false positive.

Sort detections by score, then match each to the best unmatched ground truth above a threshold. This lab uses non-interpolated AP as the sum of precision at each true positive divided by the number of ground-truth objects. A missed object contributes no term. Other benchmarks use interpolation and average over thresholds.

Run the Python lab

def iou(a,b):
    def area(box):
        return max(0,box[2]-box[0])*max(0,box[3]-box[1])
    overlap = (max(a[0],b[0]),max(a[1],b[1]),min(a[2],b[2]),min(a[3],b[3]))
    return area(overlap)/(area(a)+area(b)-area(overlap))
truth = [(0,0,2,2),(4,0,6,2),(8,0,10,2)]
pred = [(0,0,2,2),(0,0,2,2),(4,0,6,2)]
used, total = set(),0.
for rank, box in enumerate(pred,1):
    choices = [(iou(box,t),j) for j,t in enumerate(truth) if j not in used]
    score,j = max(choices,default=(0,-1))
    if score>=.5:
        used.add(j)
        total+=len(used)/rank
    print(rank,len(used)/rank,len(used)/len(truth))
print("AP:",round(total/len(truth),3))
assert abs(total/len(truth)-5/9)<1e-12

Output from the lab

1 1.0 0.3333333333333333
2 0.5 0.3333333333333333
3 0.6666666666666666 0.6666666666666666
AP: 0.556

Practice

  1. How would swapping the duplicate and the second true detection change AP?

Check your reasoning

  1. The first two ranks would both be correct. AP would rise from 5/9 to 2/3.

← Week 7 · Subject contents · Week 9 →

Further reading · Szeliski · Computer Vision: Algorithms and Applications