Finding objects as boxes

Image analysis

About this lecture

How does a network output “there are 12 cells, here”, when the number is not known in advance?

  • Boxes: the simplest object description; IoU of two boxes in closed form.
  • Where candidate boxes come from: a grid of guesses (in detail on the next day).
  • Objects of every size: a feature pyramid inside the network.
  • Every object gets many candidates: non-maximum suppression (NMS) keeps one, and what it gets wrong in a crowd.
  • How detections are scored: recalled from the evaluation day.

Slides titled The math give the formulas and proofs; the ideas are complete without them.

Boxes

A box = centre, width, height (4 numbers), plus a class and a score (how sure the detector is).

  • Enough for counting, tracking, cropping each object for a second network.
  • Poor for long diagonal objects: a box around a diagonal rod is mostly background (in the demo’s rod: the rod fills 16 % of its box).
  • A mask per object can be predicted inside each box afterwards.

The diagonal-rod scene after suppression: the kept box around the long diagonal rod (score 0.88) is mostly background; the rod fills 16 % of it. Round cells fill their boxes well.

IoU of two boxes

The evaluation day’s overlap measure, intersection over union: \mathrm{IoU} = |A \cap B| / |A \cup B|. For two axis-aligned boxes the intersection is a box again.

  • A = [0, 4] \times [0, 4], B = [2, 6] \times [2, 6]: intersection [2, 4] \times [2, 4], area 4; union 16 + 16 - 4 = 28; \mathrm{IoU} = 4/28 = 0.14.
  • The same 4 \times 4 box shifted by one pixel to the right: 12/20 = 0.6. Shifted by one pixel right and down: 9/23 = 0.39.
  • For small objects one pixel matters a lot.
Three pairs of 4 × 4 boxes on a pixel grid, the intersection shaded dark: A = [0,4]², B = [2,6]²: IoU 4/28 = 0.14; B shifted right by 1 from A: 12/20 = 0.60; shifted right and down by 1: 9/23 = 0.39.

The math: IoU of boxes in closed form

A = [a_0, a_1] \times [b_0, b_1], B = [c_0, c_1] \times [d_0, d_1]:

w = \max(0, \min(a_1, c_1) - \max(a_0, c_0)), \quad h = \max(0, \min(b_1, d_1) - \max(b_0, d_0)),

\mathrm{IoU}(A, B) = \frac{wh}{|A| + |B| - wh}.

  • Scale invariant: scaling both boxes by s multiplies every area by s^2; the ratio is unchanged.
  • 1 - \mathrm{IoU} as a training loss gives no gradient when the boxes do not overlap (w = 0 or h = 0 on a whole region): the loss is flat at 1 however far apart they are. Hence the regression losses on the box coordinates (next day).

1 − IoU of two 4 × 4 boxes against the horizontal shift of B: it rises from 0 to 1 at a shift of 4 pixels and stays exactly 1 for larger shifts: flat, no gradient.

Recall: scoring detections

From the evaluation day, unchanged:

  • Match predictions to true objects one to one at IoU \ge \tau (often \tau = 0.5): TP, FP, FN objects; F1 and matching accuracy TP/(TP + FP + FN).
  • Rank the scored detections, walk down the list: a precision–recall curve; its area is the average precision (AP). Detection benchmarks often average AP over \tau = 0.5, 0.55, \dots, 0.95.

This unit is about what happens before the evaluation: where the scored boxes come from, and how duplicates are removed.

Where do the candidates come from?

  • The network looks at a coarse grid over the image (e.g. every 16 pixels).
  • Every grid cell guesses a box and a score.
  • Older detectors guess relative to template boxes (anchors); objects that fit no template are never learned.
  • Others predict a heat map of centres; its peaks are the objects.

In detail, with a real network: Networks for cells and objects: single-shot detection.

Anchor grid (yellow dots, stride 16) with one 20 × 20 square anchor shape on the rod-and-small-cell scene: for each object the best anchor (dashed yellow) and its IoU: 0.67 for one round cell (green, learnable); 0.39, 0.28, 0.17 for the other cell, the large cell and the diagonal rod, and 0.10 for the small cell (red, never learned).

Objects of every size

  • One micrograph can hold spots of 3 px, nuclei of 15 px and clusters of 100 px.
  • A CNN’s early layers are fine grids (stride 4–8), but each cell sees a small region and knows little (edges, blobs).
  • Its deep layers see large regions and know what is there, but on a coarse grid (stride 32): a spot is 1/100 of a cell.
  • The old answer, from scale space: run the detector on an image pyramid (the image at ×2, ×1, ×½). It works, but the whole network runs once per level: 4 + 1 + \tfrac14 \approx 5 passes.

A synthetic fluorescence image with small spots, single nuclei and one large cluster of nuclei, twice. Left: the stride-8 grid of an early layer; one cell (yellow) on a spot and the 40-pixel region it sees (orange dashed): it sees the spot but not the cluster. Right: the stride-32 grid of a deep layer; one cell on the cluster and the 140-pixel region it sees: the whole cluster, but a spot is 1/100 of one cell.

A pyramid inside the network

  • The CNN already makes a pyramid: its maps at strides 4, 8, 16, 32 (bottom-up). Coarse maps know what is there; fine maps are sharp but know little.
  • Feature pyramid network (FPN; Lin et al., 2017), a top-down path: upsample the coarsest map ×2 and add the finer map, brought to the same channels by a 1×1 convolution (the lateral connection). Repeat to the finest level.
  • Every level is now sharp and knows what it sees. The same box head runs on every level: small objects on fine levels, large on coarse. Cost: one pass, plus a few cheap layers.
  • Coarse to fine, adding detail level by level: how a Laplacian pyramid is added back up. Like U-Net’s skip connections, but U-Net predicts on the finest level only, FPN on every level.

Diagram of a feature pyramid network. Left, bottom-up: the CNN's maps at strides 4, 8, 16 and 32, smaller and darker (knowing more) going up. Right, top-down: maps of the same sizes, all dark; each is the 1×1-converted map from the left (green arrows) plus the ×2-upsampled map from above (blue arrows, plus signs). Orange arrows: boxes are predicted from every level, small spots from stride 4, nuclei from stride 8, large clusters from stride 32.

The math: the top-down path, and which level an object goes to

Backbone maps C_2, \dots, C_5 (stride 2^k); W_k a 1×1 convolution (the same linear map of the channels at every pixel); \mathrm{up} = ×2 upsampling:

P_5 = W_5 C_5, \qquad P_k = W_k C_k + \mathrm{up}(P_{k+1}), \quad k = 4, 3, 2.

The Laplacian pyramid’s reconstruction is the same recursion, g_i = l_i + \mathrm{up}(g_{i+1}), with fixed details l_i; here the “details” W_k C_k are learned. A 3×3 convolution on each P_k smooths the blocky upsampling.

An object of size s = \sqrt{wh} goes to level k = \lfloor k_0 + \log_2(s / s_0) \rfloor, clamped to 2, \dots, 5; in the original paper s_0 = 224, k_0 = 4:

  • s = 224: k = 4 (stride 16); s = 448: k = 5; s = 112: k = 3. Twice the size, one level coarser: the object always covers about 224/16 = 14 cells.
  • A 20 px nucleus: \lfloor 4 + \log_2(20/224) \rfloor = \lfloor 0.51 \rfloor = 0, clamped to k = 2 (stride 4, 5 cells).

Many single-shot detectors instead let the training choose the level (next day).

Many candidates per object

Whatever proposes the boxes, every object gets several overlapping candidates: neighbouring grid cells, neighbouring pixels of a peak, jittered guesses.

Greedy non-maximum suppression: sort by score; keep the best box; delete every remaining box with IoU > t with it; repeat with the best remaining box.

Five boxes: b_1 = [0,10]^2 (0.9), b_2 = [1,11] \times [0,10] (0.8), b_3 = [8,18] \times [0,10] (0.7), b_4 = [9,19] \times [1,11] (0.6), b_5 = [20,26]^2 (0.3). Two true cells: under b_1 and under b_3.

t kept
0.3 b_1, b_3, b_5 duplicates gone
0.7 b_1, b_3, b_4, b_5 b_4 (IoU 0.68 with b_3) survives
0.1 b_1, b_4, b_5 b_3 (IoU 0.11 with b_1) deleted

The five boxes after greedy NMS at three thresholds; kept boxes solid and coloured, deleted ones grey dashed. t = 0.3: b1, b3, b5 kept. t = 0.7: b1, b3, b4, b5 (b4 survives next to b3). t = 0.1: b1, b4, b5 (b3 is deleted because it overlaps b1 slightly, and then b4 survives).

The math: greedy NMS

  1. Sort the candidates by score, highest first; K = \emptyset.
  2. Take the first remaining candidate b; add it to K.
  3. Remove every remaining candidate c with \mathrm{IoU}(b, c) > t.
  4. Repeat from 2 until no candidate remains; output K.

At most n rounds, each comparing with up to n boxes: O(n^2) IoUs. The result depends only on the order of the scores and on t. It is not an optimisation of any global criterion; it is a rule. The same rule with polygon IoU prunes star-convex polygons (next day, StarDist).

Try it: boxes and suppression

What NMS gets wrong

  • In a crowd of touching cells, the box of a true neighbour overlaps the kept box by more than t: it is deleted (a missed cell). The fix “raise t” lets duplicates of the same cell survive.
  • One threshold cannot do both when neighbours overlap as much as duplicates do. In the demo’s palisade (neighbour IoU about 0.3): t = 0.2 loses 4 of 12 cells; t = 0.75 keeps 34 duplicates; the best, t = 0.4, still has 3 false positives.
  • Hence for dense cells the map-based routes of the previous unit are usually preferred over boxes.

Two palisades of touching elongated cells after NMS (green: kept boxes with scores; blue dashed: true boxes). t = 0.2: 10 boxes, 4 cells have no box left. t = 0.75: 46 boxes, 34 of them duplicates (false positives).

Meanings of “non-maximum suppression”

Where What is suppressed Neighbourhood
Canny (the edges day) gradient values that are not the largest across the edge the two neighbours along the gradient direction
Hough accumulator (the Hough day) accumulator cells that are not a local peak a window in parameter space
Detection (here) boxes that overlap a better box by more than t IoU with the kept boxes
Polygons (next day, StarDist) polygons that overlap a better polygon by more than t polygon IoU with the kept polygons

A centre heat map’s peak picking (next day) is the Hough version; box NMS is a different rule with the same name, and polygon NMS is the box rule with polygon IoU.

Summary

  • A box is 4 numbers + score; IoU of boxes has a closed form; poor for diagonal or long objects.
  • Candidates come from a grid of guesses (anchors or centre peaks: next day).
  • Objects of every size: a feature pyramid (top-down path with lateral 1×1 connections) gives sharp maps that know what they see at every stride; boxes are predicted at every level, small objects on fine levels.
  • Every object gets many candidates; greedy NMS keeps the best and deletes overlaps above t.
  • In crowds, one t cannot both remove duplicates and keep touching neighbours.
  • Scoring (matching, F1, AP) comes from the evaluation day.