Part X — Computer Vision: CNNs, ResNets, Object Detection & Segmentation · Chapter 7

Object detection fundamentals (Bounding Boxes & IoU)

Hook

Every classifier in this course has answered "what is this?" A detector has to answer "what, and exactly where?" — and "where" needs its own metric entirely, one that a confusion matrix alone was never built to measure.

Intuition
0.00.00.00.00.00.00.01.01.01.00.00.00.01.01.01.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0
Ground truth (light) vs predicted (dark) box overlap

A ground-truth box and a predicted box on a small pixel grid. Shift the prediction and watch how much they overlap — and how sharply that overlap score drops once the boxes stop lining up.

Formalize

Intersection over Union (IoU) scores how well two boxes overlap, independent of their size or position:

IoU=area of overlaparea of union\text{IoU} = \frac{\text{area of overlap}}{\text{area of union}}
  1. A detection only counts past a threshold

    A detection typically only "counts" if IoU clears a threshold — commonly 0.50.5.

  2. Segmentation asks the same question per pixel

    Segmentation asks the same overlap question at the level of individual pixels instead of a single box, scored with the same TP (True Positive)/FP (False Positive)/FN (False Negative) machinery as any confusion matrix: every pixel is either correctly inside the mask, correctly outside it, or wrong one way or the other.

Play
0.00.00.00.00.00.00.00.51.01.0-0.50.00.00.51.01.0-0.50.00.00.00.00.00.00.00.00.00.00.00.00.0
Ground truth vs predicted box, shifted by 1

The exact same two boxes, scored two ways: IoU treats the whole box as one geometric shape, while precision, recall, and Dice treat every pixel as its own tiny classification. Both agree the boxes don't fully align — the pixel view just says which pixels are the problem.

Worked example

Ground truth: a 2×32\times3 box. Predicted box, same size, shifted right by 0, 1, or 2 columns:

  1. Perfect alignment: IoU=1

    Shift 00: the boxes coincide exactly. Intersection =6=6, union =6=6, IoU=1\text{IoU}=1.

  2. Right at the standard threshold

    Shift 11: intersection drops to 44 pixels, union rises to 88. IoU=4/8=0.5\text{IoU}=4/8=0.5 — sitting exactly on the boundary most detection benchmarks use to call something a match at all.

  3. Pixel metrics agree, and go further

    At the same shift, the overlap (intersection) is 44 pixels, out of 66 in each box:

    • TP (in both boxes) == intersection =4=4
    • FP (in predicted only) =64=2=6-4=2
    • FN (in ground truth only) =64=2=6-4=2

    So precision == recall =4/(4+2)=2/3=4/(4+2)=2/3, and Dice =2(4)/(24+2+2)=8/12=2/3=2(4)/(2\cdot4+2+2)=8/12=2/3 — consistent with the box IoU of 0.50.5 via Dice=2IoU/(1+IoU)\text{Dice}=2\,\text{IoU}/(1+\text{IoU}), but reporting exactly which pixels were false alarms versus which were missed.

Checkpoint

Find the shift that puts the detection right at the edge of counting as a match (IoU ≥ 0.5) — not a perfect one.

Pick a shift to try it
Summary
IoU=ABAB,Dice=2ABA+B=2IoU1+IoU\text{IoU} = \frac{|A \cap B|}{|A \cup B|}, \qquad \text{Dice} = \frac{2|A\cap B|}{|A|+|B|} = \frac{2\,\text{IoU}}{1+\text{IoU}}

Detection and segmentation both reduce to the same question — how much do two regions overlap — just measured at different granularities: one box versus the other, or one pixel versus the other. Every evaluation idea from Part IX still applies here; IoU and Dice are just a confusion matrix wearing a geometric hat. The capstone that closes this part puts every architecture built so far — attention, residual connections, transfer learning — to work fine-tuning one real pretrained model.