Course contents 08 / 14

08 / 14 · Convolutional Networks

Object detection

How a convolutional network finds every object in an image and draws a box around it: intersection over union, the two-stage R-CNN family with anchors, one-stage YOLO, SSD and RetinaNet with the focal loss, non-maximum suppression, mean average precision and DETR.

On this page 9

The previous note built backbones that turn an image into one label. A photograph of a street holds many objects, and a self-driving car, a shop-shelf counter or a photo search engine needs all of them: which objects there are, how many, and where each one is. Object detection answers with a list of boxes, each with a class and a confidence. The list has a different length for every image, which a classifier with a fixed output cannot produce. Detectors keep the backbones and change the head. This note shows how they reduce the problem to many small classification and regression problems over a fixed set of candidate boxes, how they remove the duplicates this creates, and how the result is scored.

Notation. A box is b=(x1,y1,x2,y2)b = (x_1, y_1, x_2, y_2), its top-left and bottom-right corners in pixels, with yy pointing down as in an image; ∣b∣\lvert b \rvert is its area. Anchors and proposals are written by centre and size, a=(xa,ya,wa,ha)a = (x_a, y_a, w_a, h_a). There are KK object classes and a background class 0. A detector outputs boxes b^\hat b with class scores ss; ptp_t is the probability a classifier gives the true class of an example, and τ\tau is an IoU threshold.

The task: boxes and classes#

A ground-truth annotation of an image is a set {(bj,cj)}j=1m\{(b_j, c_j)\}_{j=1}^{m} of boxes with classes, where mm varies from image to image and is often zero for a class. A detector returns its own set {(b^i,c^i,si)}\{(\hat b_i, \hat c_i, s_i)\}. Three things make this harder than classification:

  • The output is a set of unknown size. A network has outputs of fixed shape, so it must predict many candidates and decide which to keep.
  • Localization is continuous. A box can be almost right, so “correct” needs a tolerance, which the next sections build from the overlap of two boxes.
  • Scale and crowding. The same class appears at 20 pixels and at 500, objects overlap, and most of an image is background.

The standard benchmarks are PASCAL VOC, with 20 classes (Everingham et al., 2010), and MS COCO, with more classes, more and smaller objects per image, and a stricter metric (Lin et al., 2014).

Baseline: a classifier slid over windows#

The direct approach runs an image classifier on every window of the image: every position, several sizes, several aspect ratios, plus a “background” class. It is correct in principle and hopeless in cost. For a 1000×6001000 \times 600 image, windows every 8 pixels at 3 sizes and 3 aspect ratios give 125⋅75⋅9≈84,000125 \cdot 75 \cdot 9 \approx 84{,}000 crops, each a full forward pass of a network such as the ones of the previous note. Two observations rescue the idea. Most windows are obviously background, so a cheap step can propose a few thousand plausible regions instead. And neighbouring windows share almost all their pixels, so their convolutional features can be computed once for the whole image and cropped afterwards. The history of detectors is largely the story of these two observations.

Intersection over union#

Whether a predicted box matches a true one is measured by their intersection over union,

IoU⁡(b,b′)=∣b∩b′∣∣b∪b′∣=∣b∩b′∣∣b∣+∣b′∣−∣b∩b′∣,\operatorname{IoU}(b, b') = \frac{\lvert b \cap b' \rvert}{\lvert b \cup b' \rvert} = \frac{\lvert b \cap b' \rvert}{\lvert b \rvert + \lvert b' \rvert - \lvert b \cap b' \rvert} ,

where the intersection of two axis-aligned boxes is again a box, with corners (max⁡(x1,x1′),max⁡(y1,y1′),min⁡(x2,x2′),min⁡(y2,y2′))\big(\max(x_1, x_1'), \max(y_1, y_1'), \min(x_2, x_2'), \min(y_2, y_2')\big), and has area zero when that box is empty. IoU is 1 for identical boxes and 0 for disjoint ones, and it does not change when both boxes are scaled together, so a large and a small object are judged by the same standard. A detection is usually called correct when its IoU with an unmatched object of the same class is at least 0.5.

Worked example. A=(1,1,5,5)A = (1, 1, 5, 5) and B=(3,2,7,6)B = (3, 2, 7, 6) are both 4×44 \times 4, area 16. Their intersection runs from x=3x = 3 to 55 and from y=2y = 2 to 55, a 2×32 \times 3 box of area 6. The union is 16+16−6=2616 + 16 - 6 = 26, and IoU⁡=6/26=0.231\operatorname{IoU} = 6/26 = 0.231: well below 0.5, although the boxes share a corner region.

How far can a box of the right size be off? Shifted by a fraction uu of its side along one axis, the intersection is (1−u)(1 - u) of each box and the union (1+u)(1 + u), so

IoU⁡=1−u1+u,IoU⁡≥τ  ⟺  u≤1−τ1+τ.\begin{aligned} \operatorname{IoU} &= \frac{1 - u}{1 + u}, \\ \operatorname{IoU} &\ge \tau \iff u \le \frac{1 - \tau}{1 + \tau} . \end{aligned}

The threshold 0.5 tolerates a shift of a third of the side, the threshold 0.75 only a seventh. The same 2-pixel error leaves a 10×1010 \times 10 box at IoU 8/12=0.6678/12 = 0.667 and a 100×100100 \times 100 box at 98/102=0.96198/102 = 0.961: small objects are much harder to localize to a given IoU.

Figure

Intersection over union, and how far a box may be off

Intersection over union, and how far a box may be offLeft: two 4 by 4 boxes on a grid, A at (1, 1) and B at (3, 2); their 2 by 3 intersection is shaded, and the IoU is 6/26 = 0.231. Right: the IoU of a box of the right size as a function of its shift u, as a fraction of its side. Shifted along one axis the IoU is (1 − u)/(1 + u): 0.5 at u = 1/3 and 0.75 at u = 1/7. Shifted along both axes the curve is lower.02468246A: 4 × 4 = 16B: 4 × 4 = 166A ∩ B: 2 × 3 = 6IoU = 6 / (16 + 16 − 6) = 0.23100.20.40.600.50.751shift / box side uIoUu = 1/3u = 1/7shift along one axisalong both axesIntersection over union, and how far a box may be offLeft: two 4 by 4 boxes on a grid, A at (1, 1) and B at (3, 2); their 2 by 3 intersection is shaded, and the IoU is 6/26 = 0.231. Right: the IoU of a box of the right size as a function of its shift u, as a fraction of its side. Shifted along one axis the IoU is (1 − u)/(1 + u): 0.5 at u = 1/3 and 0.75 at u = 1/7. Shifted along both axes the curve is lower.02468246A: 4 × 4 = 16B: 4 × 4 = 166A ∩ B: 2 × 3 = 6IoU = 6 / (16 + 16 − 6) = 0.23100.20.40.600.50.751shift / box side uIoUu = 1/3u = 1/7shift along one axisalong both axes
Left: the worked example of the text, intersection 6, union 26, IoU = 0.231. Right: how far a box of the right size may be off. Shifted by a fraction u of its side along one axis, its IoU is (1 − u)/(1 + u), so the threshold 0.5 tolerates a shift of a third of the side and the threshold 0.75 only a seventh. A 2-pixel shift costs a 10 × 10 box a third of its IoU (0.667) and a 100 × 100 box four percent (0.961): IoU measures the error relative to the size of the object.

Two-stage detectors#

The R-CNN line follows the two observations of the baseline one at a time: first propose regions and classify them, then share the convolutions, then learn the proposals too.

R-CNN: classify proposed regions#

R-CNN (Girshick et al., 2014) takes about 2000 region proposals per image from selective search, a hand-designed algorithm that greedily merges similar neighbouring superpixels into candidate regions (Uijlings et al., 2013). Each proposal is cropped with a little context, warped to 227×227227 \times 227 pixels, and passed through a convolutional network pretrained on ImageNet and fine-tuned on warped regions. Its features feed one linear SVM per class, and a linear regressor refines the box. Duplicates are removed per class by non-maximum suppression. With this pipeline R-CNN reached 53.7% mAP on VOC 2010, against 35.1% for the UVA system, which used the same selective-search proposals with hand-engineered features, and 40.4% for the strongest baseline of the comparison, SegDPM (section 2.4 and Table 1).

Its weakness is cost: every proposal is a separate forward pass, about 13 seconds per image on a GPU (section 2.2), and training happens in three separate stages (network, SVMs, regressors).

Box regression#

Both R-CNN and its successors predict a box as a correction to a reference box a=(xa,ya,wa,ha)a = (x_a, y_a, w_a, h_a), a proposal or an anchor. The target for a true box (x,y,w,h)(x, y, w, h) in centre-size form is

tx=x−xawa,ty=y−yaha,tw=log⁡wwa,th=log⁡hha\boxed{ \begin{aligned} t_x &= \frac{x - x_a}{w_a}, \\ t_y &= \frac{y - y_a}{h_a}, \qquad t_w = \log \frac{w}{w_a}, \qquad t_h = \log \frac{h}{h_a} \end{aligned} }

(Girshick et al., 2014, Appendix C). The shifts are measured in units of the reference size, so the same tt means the same relative correction for a small and a large box. The logarithm makes the size targets symmetric (doubling and halving are ±0.693\pm 0.693) and keeps the decoded width w=waetww = w_a e^{t_w} positive whatever the network predicts. Fast R-CNN trains the four outputs with the smooth L1 loss, 12t2\tfrac12 t^2 for ∣t∣<1\lvert t \rvert < 1 and ∣t∣−12\lvert t \rvert - \tfrac12 beyond, per coordinate (Girshick, 2015, eq. 3): the Huber loss with δ=1\delta = 1, which keeps a badly matched box from producing a huge gradient.

Worked example. The anchor (50,50,40,40)(50, 50, 40, 40) and the true box (56,47,60,32)(56, 47, 60, 32) give t=(6/40,−3/40,log⁡1.5,log⁡0.8)=(0.15,−0.075,0.405,−0.223)t = (6/40, -3/40, \log 1.5, \log 0.8) = (0.15, -0.075, 0.405, -0.223). Decoding reverses it: x=50+0.15⋅40=56x = 50 + 0.15 \cdot 40 = 56 and w=40 e0.405=60w = 40 \, e^{0.405} = 60.

Fast R-CNN: share the convolutions#

Fast R-CNN (Girshick, 2015) runs the backbone once on the whole image and crops each proposal from the feature map instead of from the pixels. RoI pooling turns a crop of any size into a fixed grid: the region of h×wh \times w feature cells is divided into an H×WH \times W grid of sub-windows of about h/H×w/Wh/H \times w/W cells, and each sub-window is max-pooled, giving H×W=7×7H \times W = 7 \times 7 for VGG-16 (section 2.1). The pooled grid feeds fully connected layers with two outputs, a softmax over K+1K + 1 classes and box offsets tut^u for each class uu, trained together with one multi-task loss

L(p,u,tu,v)=−log⁡pu+λ [u≥1]∑i∈{x,y,w,h}smooth⁡L1 ⁣(tiu−vi),L(p, u, t^u, v) = -\log p_u + \lambda\, [u \ge 1] \sum_{i \in \{x, y, w, h\}} \operatorname{smooth}_{L_1}\!\big(t^u_i - v_i\big) ,

where uu is the true class of the proposal, vv its regression target, and the indicator [u≥1][u \ge 1] turns the box term off for background proposals, which have no box to regress to; λ=1\lambda = 1. Each mini-batch takes 64 proposals from each of 2 images: 25% with IoU at least 0.5 with a true box, labelled with its class, and the rest with IoU in [0.1,0.5)[0.1, 0.5), labelled background (section 2.3).

With VGG-16 on VOC 2007, the shared computation cut the test time from 47 s to 0.32 s per image, proposals excluded, and the training time from 84 to 9.5 hours, while mAP rose from 66.0% to 66.9% (section 4.4 and Table 4). The proposals, still from selective search on a CPU, now took longer than the detector.

Faster R-CNN: anchors and the region proposal network#

Faster R-CNN (Ren et al., 2015) learns the proposals with a region proposal network (RPN) that shares the backbone. A 3×33 \times 3 convolution runs over the feature map, and at each position a small head looks at kk anchors, reference boxes centred there: 3 areas, 1282128^2, 2562256^2 and 5122512^2 pixels, times 3 aspect ratios, 1:1, 1:2 and 2:1, so k=9k = 9 (sections 3.1.1 and 3.3). For each anchor the head outputs an objectness score and four offsets tt in the parametrization above. The backbone has stride 16, so a 1000×6001000 \times 600 image gives roughly 60×4060 \times 40 positions and about 20,000 anchors.

Anchors are labelled by IoU with the true boxes: an anchor is positive if its IoU with some true box exceeds 0.7, or if it is the anchor with the highest IoU for some true box (so that every object gets at least one); it is negative if its IoU is below 0.3 for every true box; anchors in between are ignored (section 3.1.2). Each image contributes 256 sampled anchors, up to half of them positive, to the loss

L=1Ncls∑iℓcls(pi,pi∗)+λ 1Nreg∑ipi∗ smooth⁡L1 ⁣(ti−ti∗),L = \frac{1}{N_{\text{cls}}} \sum_i \ell_{\text{cls}}(p_i, p_i^*) + \lambda\, \frac{1}{N_{\text{reg}}} \sum_i p_i^*\, \operatorname{smooth}_{L_1}\!\big(t_i - t_i^*\big) ,

with pi∗∈{0,1}p_i^* \in \{0, 1\} the label of anchor ii, ℓcls\ell_{\text{cls}} the binary cross-entropy, Ncls=256N_{\text{cls}} = 256, Nreg≈2400N_{\text{reg}} \approx 2400 the number of anchor positions, and λ=10\lambda = 10 so that the two terms weigh about the same. Worked example. An anchor of area 1282128^2 with height twice its width has w=128/2=90.5w = 128/\sqrt2 = 90.5 and h=1282=181h = 128\sqrt2 = 181. A true box that this anchor overlaps with IoU 0.82 makes it a positive with regression targets toward that box; one with IoU 0.45 leaves it ignored; with every IoU at 0.12 or less it is a negative.

The RPN’s boxes are proposals for the second stage, which is Fast R-CNN on the same feature map.

Figure

Faster R-CNN: one feature map, proposals and a head

Faster R-CNN: one feature map, proposals and a headA diagram of Faster R-CNN. The image passes through a convolutional backbone, giving a feature map with stride 16. The region proposal network (RPN) scores 9 anchors at every position of the map; NMS at threshold 0.7 leaves 300 proposals. RoI pooling cuts a 7 by 7 feature grid for each proposal from the same map, the head classifies it and refines the box, and per-class NMS gives the final detections.imagebackboneCNNfeaturesstride 161RPN9 anchors / position2NMS 0.7300 proposals3RoI pooling7 × 7 per proposal4headK + 1 classes, boxes5NMSper classdetections6inputstateweights, gatesoutputFaster R-CNN: one feature map, proposals and a headA diagram of Faster R-CNN. The image passes through a convolutional backbone, giving a feature map with stride 16. The region proposal network (RPN) scores 9 anchors at every position of the map; NMS at threshold 0.7 leaves 300 proposals. RoI pooling cuts a 7 by 7 feature grid for each proposal from the same map, the head classifies it and refines the box, and per-class NMS gives the final detections.imagebackboneCNNfeaturesstride 161RPN9 anchors / position2NMS 0.7300 proposals3RoI pooling7 × 7 per proposal4headK + 1 classes, boxes5NMSper classdetections6inputstateweights, gatesoutput
A two-stage detector as one network. The expensive part, the backbone, runs once per image; the RPN is a small convolutional head on the same map: for a 1000 × 600 image at stride 16 that is roughly 60 × 40 positions with 9 anchors each, about 20 thousand. The second stage sees only the proposals that survive NMS. Both stages are trained with the same kind of loss, classification plus box regression, with different IoU thresholds for labelling.
  1. The backbone computes one feature map for the whole image, with stride 16.
  2. At every position the RPN scores 9 anchors for “object or not” and regresses their boxes.
  3. NMS at IoU 0.7 removes overlapping proposals, leaving about 2000 for training; 300 are used at test time.
  4. RoI pooling cuts a 7×77 \times 7 grid from the shared map for each proposal.
  5. The head assigns each proposal one of KK classes or background and refines its box once more.
  6. Per-class NMS removes duplicate detections.

With VGG-16, the whole system ran in 198 ms per image on a GPU, about 5 frames per second, against 1830 ms for selective search with Fast R-CNN, and reached 73.2% mAP on VOC 2007 when trained on VOC 2007 and 2012 (section 4.1, Tables 3 and 5). Adding a feature pyramid, which gives the RPN and the head feature maps at several strides with top-down connections between them, improved small objects in particular and became the standard backbone of later two-stage detectors (Lin et al., 2017a).

One-stage detectors#

A two-stage detector classifies each proposal twice, coarsely and finely. A one-stage detector predicts the final classes and boxes directly from the feature map, for every location at once, trading some accuracy for speed and simplicity.

YOLO: one grid, one pass#

YOLO (Redmon et al., 2016) resizes the image to 448×448448 \times 448 and divides it into an S×SS \times S grid. Each cell predicts BB boxes, each as (x,y,w,h)(x, y, w, h) and a confidence that should equal P(object)⋅IoU⁡P(\text{object}) \cdot \operatorname{IoU} with the true box, plus one set of CC class probabilities for the cell; the cell that contains an object’s centre is responsible for it. For VOC, S=7S = 7, B=2B = 2 and C=20C = 20, so the network ends in a 7×7×(2⋅5+20)=7×7×307 \times 7 \times (2 \cdot 5 + 20) = 7 \times 7 \times 30 tensor: 1470 numbers and 98 boxes per image (section 2). The loss is a sum of squared errors, with weights λcoord=5\lambda_{\text{coord}} = 5 on box coordinates and λnoobj=0.5\lambda_{\text{noobj}} = 0.5 on the confidence of boxes without objects, and with w\sqrt w and h\sqrt h in place of ww and hh so that an error of a few pixels counts more on a small box than on a large one (section 2.2).

YOLO ran at 45 frames per second with 63.4% mAP on VOC 2007, and a smaller version at 155 frames per second with 52.7%, trained on VOC 2007 and 2012 (Table 1). Its errors were mostly localization errors, and a cell that holds the centres of several small objects can report only one class (sections 2.4 and 4.2).

SSD: default boxes at several scales#

SSD (Liu et al., 2016) keeps the single pass but predicts from several feature maps of decreasing resolution, each with default boxes (anchors) of a few aspect ratios at every position, and a 3×33 \times 3 convolution outputs class scores and offsets for each. Fine maps handle small objects and coarse maps large ones. For a 300×300300 \times 300 input the maps are 382,192,102,52,3238^2, 19^2, 10^2, 5^2, 3^2 and 121^2 with 4, 6, 6, 6, 4 and 4 default boxes per position, which makes 5776+2166+600+150+36+4=87325776 + 2166 + 600 + 150 + 36 + 4 = 8732 boxes per class. A default box is positive when its IoU with a true box exceeds 0.5, and since almost all are negative, only the negatives with the highest loss are kept, at most three per positive (hard negative mining, section 2.2). SSD300 reached 74.3% mAP on VOC 2007 at 59 frames per second on a Titan X (abstract and Table 7).

Focal loss and RetinaNet#

Hard negative mining hints at the real obstacle of one-stage detectors. They score 10410^4 to 10510^5 candidate locations per image, and only a handful hold objects (Lin et al., 2017b, section 1). The easy negatives, clear background that the classifier already gets right with ptp_t close to 1, each contribute a small loss, but there are so many of them that together they dominate the gradient. Two-stage detectors avoid this with the proposal stage and with fixed foreground-to-background ratios in sampling.

The focal loss (Lin et al., 2017b) fixes it inside the loss. With ptp_t the probability of the true class (that is pp for a positive and 1−p1 - p for a negative of a sigmoid output pp),

  FL⁡(pt)=−αt (1−pt)γlog⁡pt  \boxed{\; \operatorname{FL}(p_t) = -\alpha_t\, (1 - p_t)^{\gamma} \log p_t \;}

The factor (1−pt)γ(1 - p_t)^\gamma is close to 1 for a misclassified example and close to 0 for an easy one; γ=0\gamma = 0 gives the cross-entropy, and the paper uses γ=2\gamma = 2. The weight αt\alpha_t is α\alpha for positives and 1−α1 - \alpha for negatives, with α=0.25\alpha = 0.25 working best together with γ=2\gamma = 2 (sections 3.1–3.2 and 5.1).

Worked example. At pt=0.9p_t = 0.9 the cross-entropy is −log⁡0.9=0.105-\log 0.9 = 0.105 and the focal loss with γ=2\gamma = 2 is 0.01⋅0.105=0.001050.01 \cdot 0.105 = 0.00105, 100 times smaller; at pt=0.968p_t = 0.968 the ratio is 1/0.0322≈10001/0.032^2 \approx 1000; at pt=0.5p_t = 0.5 it is only 4, and at pt=0.1p_t = 0.1 the factor is 0.810.81. Take 100,000 easy negatives at pt=0.99p_t = 0.99 and 10 positives at pt=0.5p_t = 0.5. Under cross-entropy the negatives contribute 105⋅0.01005=100510^5 \cdot 0.01005 = 1005 and the positives 10⋅0.693=6.910 \cdot 0.693 = 6.9: the objects make up 0.7% of the loss. Under the focal loss with γ=2\gamma = 2 the negatives contribute 105⋅10−4⋅0.01005=0.1010^5 \cdot 10^{-4} \cdot 0.01005 = 0.10 and the positives 10⋅0.25⋅0.693=1.7310 \cdot 0.25 \cdot 0.693 = 1.73: 94.5%. With α=0.25\alpha = 0.25 as well, the share is 85.2%.

Figure

The focal loss turns down easy examples

The focal loss turns down easy examplesThe loss against the probability of the true class from 0 to 1, for γ = 0, 0.5, 1, 2 and 5. At γ = 0 it is cross-entropy. As γ grows, the loss on examples with probability above 0.5 almost vanishes: at pₜ = 0.9 and γ = 2 it is 100 times smaller than cross-entropy.well classified00.20.40.60.8101234probability of the true class pₜlosspₜ = 0.9, γ = 2: 100× smallerγ = 0 (cross-entropy)γ = 0.5γ = 1γ = 2γ = 5The focal loss turns down easy examplesThe loss against the probability of the true class from 0 to 1, for γ = 0, 0.5, 1, 2 and 5. At γ = 0 it is cross-entropy. As γ grows, the loss on examples with probability above 0.5 almost vanishes: at pₜ = 0.9 and γ = 2 it is 100 times smaller than cross-entropy.well classified00.20.40.60.8101234probability of the true class pₜlosspₜ = 0.9, γ = 2: 100× smallerγ = 0 (cross-entropy)γ = 0.5γ = 1γ = 2γ = 5
FL(pₜ) = −(1 − pₜ)^γ log pₜ. The factor (1 − pₜ)^γ barely touches hard examples (0.81 at pₜ = 0.1 for γ = 2) and shrinks the loss of easy ones: 4 times at pₜ = 0.5, 100 times at pₜ = 0.9, about 1000 times at pₜ = 0.968. A hundred thousand easy background anchors no longer drown out a few dozen objects.

RetinaNet is the one-stage detector built to test the loss: a ResNet with a feature pyramid, 9 anchors per position at each pyramid level, positives at IoU at least 0.5 and negatives below 0.4, and two small convolutional subnetworks for classes and boxes. The focal loss is applied to all of roughly 100,000 anchors per image, with no sampling, and normalized by the number of positive anchors. The bias of the final classification layer is initialized so that every anchor starts at foreground probability π=0.01\pi = 0.01, so that the many background anchors do not produce a large, destabilizing loss in the first iterations (sections 3.3 and 4.1). With a ResNet-101 pyramid, RetinaNet reached 39.1 AP on COCO test-dev, above the two-stage detectors of its time (Table 2).

Non-maximum suppression#

Every detector above predicts many overlapping boxes for one object: neighbouring anchors, overlapping proposals, adjacent grid cells. Non-maximum suppression (NMS) keeps the best of each cluster. For each class separately:

  1. Sort the boxes by score.
  2. Take the highest-scoring box and keep it.
  3. Remove every remaining box whose IoU with it exceeds the threshold τ\tau.
  4. Repeat from step 2 with the boxes that are left.

A box is compared only with boxes that have been kept, never with ones already removed. The algorithm is greedy and costs O(n2)O(n^2) IoU computations in the worst case for nn boxes; detectors apply it to a few hundred or thousand boxes per class after discarding low scores.

Worked example. Six boxes of one class: A=(10,10,50,50)A = (10, 10, 50, 50) with score 0.95, B=(14,12,54,52)B = (14, 12, 54, 52) with 0.80, C=(8,14,46,54)C = (8, 14, 46, 54) with 0.60, D=(60,20,100,60)D = (60, 20, 100, 60) with 0.90, E=(64,24,104,64)E = (64, 24, 104, 64) with 0.55 and F=(40,30,80,70)F = (40, 30, 80, 70) with 0.50, and τ=0.5\tau = 0.5. Sorted: A,D,B,C,E,FA, D, B, C, E, F. AA is kept, and BB and CC go: their IoUs with AA are 0.75 and 0.71. DD has IoU 0 with AA and is kept; EE goes, with IoU 0.68 with DD. FF overlaps AA by 0.07 and DD by 0.23, so it stays. The output is AA, DD, FF.

Figure

Non-maximum suppression, step by step

Non-maximum suppression, step by stepSix boxes of one class: three heavily overlapping ones around the first object (A 0.95, B 0.80, C 0.60), two around the second (D 0.90, E 0.55), and a box F 0.50 between them. Greedy suppression at threshold 0.5 keeps A, D and F and removes B, C and E.A 0.95B 0.80C 0.60D 0.90E 0.55F 0.50by decreasing scoreA 0.95D 0.90B 0.80C 0.60E 0.55F 0.501keptremoved: IoU with A 0.75removed: IoU with A 0.712keptremoved: IoU with D 0.683kept: IoU 0.07 with A, 0.23 with D4Non-maximum suppression, step by stepSix boxes of one class: three heavily overlapping ones around the first object (A 0.95, B 0.80, C 0.60), two around the second (D 0.90, E 0.55), and a box F 0.50 between them. Greedy suppression at threshold 0.5 keeps A, D and F and removes B, C and E.A 0.95B 0.80C 0.60D 0.90E 0.55F 0.50by decreasing scoreA 0.95D 0.90B 0.80C 0.60E 0.55F 0.501keptremoved: IoU with A 0.75removed: IoU with A 0.712keptremoved: IoU with D 0.683kept: IoU 0.07 with A, 0.23 with D4
The worked example of the text, IoU threshold 0.5. Each step takes the best remaining box and removes everything that overlaps it by more than the threshold; boxes are compared only with kept ones. F survives although it touches A and D: its overlaps are small. The threshold sets the trade-off: at 0.7, E (IoU 0.68 with D) would also survive as a duplicate, and at 0.2, F (0.23 with D), which might be a separate object, would go.

The threshold is a trade-off between duplicates and crowds. At τ=0.7\tau = 0.7 in this example EE also survives, a duplicate of DD; at τ=0.2\tau = 0.2, FF disappears as well, and if FF was a real object partly hidden behind the other two, it is lost. No single threshold handles both a crowd of overlapping people and a cluster of duplicates of one person, which is one motivation for detectors without NMS (below).

Evaluation: precision, recall and mAP#

A detector is scored per class by the precision–recall curve of its detections, ranked by score across the whole test set. The classification metrics note defines the curve and average precision for ranked binary predictions; detection adds one step, deciding which detections are true positives.

  1. Go through the detections of a class in order of decreasing score.
  2. A detection is a true positive if its IoU with a true box of that class in the same image is at least τ\tau and that box has not been matched by a higher-scoring detection; otherwise it is a false positive. A second detection of an already matched object is a false positive: duplicates are penalized.
  3. After the kk-th detection, precision is the fraction of the kk that are true positives, and recall the fraction of all n+n_+ true boxes that have been matched. True boxes never matched are the misses.
  4. Replace each precision by the interpolated precision, the best precision at the same or higher recall, pinterp(r)=max⁡r′≥rp(r′)p_{\text{interp}}(r) = \max_{r' \ge r} p(r'), which removes the zigzag and makes the curve non-increasing. Average precision (AP) is the area under the interpolated curve.

The mean average precision (mAP) averages AP over the classes. Conventions differ in the threshold and the sampling of the curve. PASCAL VOC uses τ=0.5\tau = 0.5; its 2007 edition averaged the interpolated precision at the 11 recalls 0,0.1,…,10, 0.1, \dots, 1 (Everingham et al., 2010), and later editions use the area under the whole interpolated curve. COCO averages over ten thresholds, τ=0.50,0.55,…,0.95\tau = 0.50, 0.55, \dots, 0.95, samples the interpolated curve at 101 recalls 0,0.01,…,10, 0.01, \dots, 1, and calls the result simply AP; AP50 and AP75 are the single-threshold versions, and APS\mathrm{AP}_S, APM\mathrm{AP}_M and APL\mathrm{AP}_L restrict to objects smaller than 32232^2 pixels, between 32232^2 and 96296^2, and larger (Lin et al., 2014; the official COCO evaluation code). Averaging over thresholds rewards tight boxes: a detector whose boxes are all at IoU 0.6 scores well at AP50 and poorly at AP.

Worked example. One class, four true 40×4040 \times 40 boxes, seven detections. Shifting a 40×4040 \times 40 box sideways by dd pixels gives IoU (40−d)/(40+d)(40 - d)/(40 + d), and the detections are such shifted copies:

rankscorebest match, IoUat τ=0.5\tau = 0.5precisionrecallat τ=0.75\tau = 0.75
10.95object 1, 0.905TP1.0000.25TP
20.90object 2, 0.633TP1.0000.50FP
30.80object 3, 0.333FP0.6670.50FP
40.70object 1, 0.667FP (duplicate)0.5000.50FP
50.60object 3, 0.818TP0.6000.75TP
60.50object 4, 0.538TP0.6671.00FP
70.40noneFP0.5711.00FP

At τ=0.5\tau = 0.5 the true positives come at ranks 1, 2, 5 and 6, each adding 1/41/4 of recall. Their interpolated precisions are 1, 1, 0.667 and 0.667 (at rank 5 the precision is 0.600, but rank 6 reaches 0.667 at a higher recall), so

AP0.5=14(1+1+0.667+0.667)=0.833.\mathrm{AP}_{0.5} = \tfrac14 (1 + 1 + 0.667 + 0.667) = 0.833 .

Without interpolation the sum would use 0.600 at rank 5 and give 0.817. At τ=0.75\tau = 0.75 only ranks 1 and 5 are true positives, with precisions 1 and 2/52/5, so AP0.75=14(1+0.4)=0.350\mathrm{AP}_{0.75} = \tfrac14(1 + 0.4) = 0.350: the ranking is the same, but two of the boxes are not tight enough. Over the ten COCO thresholds the AP is 0.833, 0.65, 0.65, 0.35, 0.35, 0.35, 0.35, 0.25, 0.25 and 0, with mean 0.403.

Figure

Average precision for the worked example

Average precision for the worked examplePrecision–recall curves for seven detections and four objects. The dots are precision and recall after each rank. The staircase envelopes are the interpolated precision; the area under each is the AP: 0.833 at IoU threshold 0.5 and 0.350 at 0.75.00.250.50.75100.250.50.751recallprecisionprecision at each rankIoU 0.5: AP = 0.833IoU 0.75: AP = 0.350Average precision for the worked examplePrecision–recall curves for seven detections and four objects. The dots are precision and recall after each rank. The staircase envelopes are the interpolated precision; the area under each is the AP: 0.833 at IoU threshold 0.5 and 0.350 at 0.75.00.250.50.75100.250.50.751recallprecisionprecision at each rankIoU 0.5: AP = 0.833IoU 0.75: AP = 0.350
The detections come in order of decreasing score; each true positive raises recall by 1/4, each false positive lowers precision. The interpolated precision at a recall is the best precision at that recall or beyond, hence the staircase; AP is the area under it. At the threshold 0.75 two of the four true positives become false positives because their boxes are not tight enough, and AP falls from 0.833 to 0.350 with the same ranking.

Results across papers are comparable only under the same protocol. VOC mAP at 0.5 and COCO AP over ten thresholds are different numbers on different data: the 73.2% of Faster R-CNN on VOC 2007 and the 39.1 AP of RetinaNet on COCO do not rank the two methods.

DETR: detection as set prediction#

Optional depth: a detector built on the Transformer, which this course reaches in Vision Transformers. The rest of the note does not depend on it.

Anchors, IoU-based labelling and NMS are hand-designed parts that every detector above needs. DETR (Carion et al., 2020) removes them by treating detection as the prediction of a set. A CNN backbone produces a feature map, which is flattened into a sequence of HWHW feature vectors with positional encodings and passed through a Transformer encoder, whose self-attention lets every location see the whole image. A decoder takes N=100N = 100 learned object queries, vectors that each attend to the encoded image and are each turned into one prediction: a class among KK plus “no object” ∅\varnothing, and a box.

The loss compares the set of NN predictions with the set of true objects, padded with ∅\varnothing to size NN. First, the Hungarian algorithm finds the one-to-one assignment σ^\hat\sigma of predictions to objects with the lowest total matching cost, which combines the predicted probability of the right class with the box error. Then each prediction is trained toward its assigned object: cross-entropy on the class, and for real objects an L1 loss on the box coordinates (weight 5) plus the generalized IoU loss (weight 2) (Rezatofighi et al., 2019), which stays informative when two boxes do not overlap (section 3.1 and Appendix A.4). Because each object is matched to exactly one prediction, a second prediction of the same object is trained toward “no object”: the network learns to suppress its own duplicates, and NMS is not needed.

With a ResNet-50 backbone and 500 training epochs, DETR reached 42.0 AP on COCO val, the same as a well-tuned Faster R-CNN with a feature pyramid; it was better on large objects (APL\mathrm{AP}_L 61.1 against 53.4) and worse on small ones (APS\mathrm{AP}_S 20.5 against 26.6) (section 4.1, Table 1). It also trains much longer than anchor-based detectors. Later variants address both, and the set-prediction view returns in Vision Transformers.

What changed#

Classifier slid over windowsModern detector
Candidatesevery window, scale, ratioproposals or anchors, about 10310^3 to 10510^5
Featuresone network pass per windowone backbone pass per image, cropped by RoI pooling or read at each position
Boxesthe windows themselvesregressed offsets (tx,ty,tw,th)(t_x, t_y, t_w, t_h) from each reference box
Backgroundas frequent as it issampled at fixed ratios, mined, or turned down by the focal loss
Duplicatesmany per objectremoved by NMS, or never produced (DETR)
ScoreaccuracyAP per class at an IoU threshold, averaged into mAP

Limitations#

  • Hand-tuned thresholds everywhere. Anchor IoU thresholds, NMS thresholds and anchor shapes are chosen per dataset; bad anchors for unusual aspect ratios mean missed objects.
  • NMS cannot tell crowds from duplicates. Heavily overlapping objects of the same class suppress each other.
  • Small objects. At stride 16 or 32 a 10-pixel object covers less than one feature cell, and IoU punishes a few pixels of error severely.
  • Boxes are a coarse description. A diagonal pole or a person with outstretched arms fills a small part of its box; the shape itself is what segmentation predicts.
  • The metric shapes the method. AP at 0.5 ignores box tightness, AP over ten thresholds rewards it, and neither measures the cost of a missed object against a false alarm in a given application.

What you should remember#

  • IoU⁡=∣b∩b′∣/∣b∪b′∣\operatorname{IoU} = \lvert b \cap b' \rvert / \lvert b \cup b' \rvert; a box of the right size shifted by a fraction uu along one axis has IoU (1−u)/(1+u)(1 - u)/(1 + u), so the threshold 0.5 allows a third of a side and 0.75 a seventh.
  • Boxes are regressed as offsets from a reference box: tx=(x−xa)/wat_x = (x - x_a)/w_a, tw=log⁡(w/wa)t_w = \log(w/w_a), trained with smooth L1.
  • R-CNN classifies about 2000 warped proposals; Fast R-CNN shares the backbone and pools each proposal to 7×77 \times 7; Faster R-CNN learns the proposals with an RPN over 9 anchors per position, positives at IoU above 0.7 and negatives below 0.3.
  • One-stage detectors (YOLO, SSD, RetinaNet) predict classes and boxes at every location; the focal loss −αt(1−pt)γlog⁡pt-\alpha_t (1 - p_t)^\gamma \log p_t with γ=2\gamma = 2 cuts the loss of an example at pt=0.9p_t = 0.9 a hundredfold, so that background no longer dominates.
  • NMS keeps the best box and removes those overlapping it above τ\tau, then repeats; the threshold trades duplicates against crowds.
  • AP is the area under the interpolated precision–recall curve of a class, with true positives defined by IoU at least τ\tau and one match per object; mAP averages over classes, COCO AP also over τ=0.5,…,0.95\tau = 0.5, \dots, 0.95.
  • DETR predicts a set with object queries and a Hungarian-matched loss, and needs neither anchors nor NMS.

Code#

IoU, box encoding, the focal loss, NMS and average precision on synthetic boxes, with the worked examples of the note. NMS is compared with torchvision.ops.nms when torchvision is installed and otherwise checked against its defining property: kept boxes overlap each other by at most τ\tau, and every removed box overlaps a higher-scoring kept one by more than τ\tau, which by induction over the score order determines the result uniquely. The last part simulates a detector whose boxes are true boxes with jittered corners and measures what the jitter does to AP at different thresholds. Exact numbers can differ slightly across platforms and library versions; the script runs in about a second.

import math, torch

def box_iou(a, b):
    """Pairwise IoU of boxes a (N, 4) and b (M, 4), each row (x1, y1, x2, y2)."""
    area_a = (a[:, 2] - a[:, 0]) * (a[:, 3] - a[:, 1])
    area_b = (b[:, 2] - b[:, 0]) * (b[:, 3] - b[:, 1])
    lt = torch.max(a[:, None, :2], b[None, :, :2])   # (N, M, 2): top-left corner of each intersection
    rb = torch.min(a[:, None, 2:], b[None, :, 2:])   # bottom-right corner
    wh = (rb - lt).clamp(min=0)                      # empty intersections get width or height 0
    inter = wh[..., 0] * wh[..., 1]
    return inter / (area_a[:, None] + area_b[None, :] - inter)

# 1. IoU: the worked example, and what a 2-pixel shift costs a small and a large box
A, B = torch.tensor([[1., 1, 5, 5]]), torch.tensor([[3., 2, 7, 6]])
print(round(box_iou(A, B).item(), 4))                                          # 0.2308 = 6 / 26
print([round(box_iou(torch.tensor([[0., 0, s, s]]), torch.tensor([[2., 0, s + 2, s]])).item(), 3) for s in (10, 100)])
# [0.667, 0.961]

# 2. regression targets of a box relative to an anchor, both as (centre x, centre y, width, height)
def encode(anchor, box):
    xa, ya, wa, ha = anchor
    x, y, w, h = box
    return [(x - xa) / wa, (y - ya) / ha, math.log(w / wa), math.log(h / ha)]
print([round(t, 3) for t in encode((50, 50, 40, 40), (56, 47, 60, 32))])     # [0.15, -0.075, 0.405, -0.223]

# 3. focal loss against cross-entropy, as a function of the probability p_t of the true class
for pt in (0.1, 0.5, 0.9, 0.968):
    ce = -math.log(pt)
    print(pt, round(ce, 4), round((1 - pt) ** 2 * ce, 6), round(1 / (1 - pt) ** 2, 1))
# 0.1 2.3026 1.865094 1.2 | 0.5 0.6931 0.173287 4.0 | 0.9 0.1054 0.001054 100.0 | 0.968 0.0325 3.3e-05 976.6
# 100,000 easy negatives (p_t = 0.99) against 10 positives (p_t = 0.5): the positives' share of the loss
for gamma, alpha in ((0, 0.5), (2, 0.5), (2, 0.25)):
    neg = 1e5 * (1 - alpha) * 0.01 ** gamma * -math.log(0.99)
    pos = 10 * alpha * 0.5 ** gamma * -math.log(0.5)
    print(gamma, alpha, round(pos / (pos + neg), 4))
# 0 0.5 0.0068 | 2 0.5 0.9452 | 2 0.25 0.8518

# 4. greedy non-maximum suppression
def nms(boxes, scores, thr):
    order = scores.argsort(descending=True)
    keep = []
    while order.numel() > 0:
        i = order[0]
        keep.append(i.item())
        ious = box_iou(boxes[i][None], boxes[order[1:]])[0]
        order = order[1:][ious <= thr]               # drop every box that overlaps the kept one by more than thr
    return torch.tensor(keep)

names = 'ABCDEF'
boxes = torch.tensor([[10., 10, 50, 50], [14, 12, 54, 52], [8, 14, 46, 54],
                      [60, 20, 100, 60], [64, 24, 104, 64], [40, 30, 80, 70]])
scores = torch.tensor([0.95, 0.80, 0.60, 0.90, 0.55, 0.50])
print(box_iou(boxes, boxes).numpy().round(3))
# [[1.    0.747 0.711 0.    0.    0.067]
#  [0.747 1.    0.639 0.    0.    0.107]
#  [0.711 0.639 1.    0.    0.    0.048]
#  [0.    0.    0.    1.    0.681 0.231]
#  [0.    0.    0.    0.681 1.    0.205]
#  [0.067 0.107 0.048 0.231 0.205 1.   ]]
for thr in (0.5, 0.7, 0.2):
    print(thr, ''.join(names[i] for i in nms(boxes, scores, thr)))
# 0.5 ADF | 0.7 ADEF | 0.2 AD

# check on 300 random boxes: against torchvision if it is installed, otherwise by the defining property
torch.manual_seed(0)
xy = torch.rand(300, 2) * 200
rnd = torch.cat([xy, xy + 10 + torch.rand(300, 2) * 40], 1)
s = torch.rand(300)
mine = nms(rnd, s, 0.5)
try:
    import torchvision
    print('matches torchvision:', torch.equal(mine, torchvision.ops.nms(rnd, s, 0.5)))
except ImportError:
    kept = torch.zeros(300, dtype=torch.bool); kept[mine] = True
    iou = box_iou(rnd, rnd)
    higher = s[None, :] > s[:, None]                 # higher[i, j]: box j scores above box i
    ok_kept = (iou[kept][:, kept] - torch.eye(int(kept.sum()))).max() <= 0.5
    ok_dropped = ((iou[~kept] > 0.5) & higher[~kept] & kept[None, :]).any(1).all()
    print(len(mine), 'kept; kept boxes overlap at most 0.5:', bool(ok_kept),
          '| each dropped box overlaps a higher-scoring kept one:', bool(ok_dropped))
# 245 kept; kept boxes overlap at most 0.5: True | each dropped box overlaps a higher-scoring kept one: True

# 5. average precision with all-point interpolation
def average_precision(det_boxes, det_scores, gt_boxes, thr):
    order = det_scores.argsort(descending=True)
    iou = box_iou(det_boxes[order], gt_boxes)
    taken = torch.zeros(len(gt_boxes), dtype=torch.bool)
    tp = torch.zeros(len(order))
    for k in range(len(order)):                      # each detection, best score first, takes the best free match
        cand = iou[k].clone()
        cand[taken] = -1
        j = cand.argmax()
        if cand[j] >= thr:
            taken[j] = True
            tp[k] = 1
    hits = tp.cumsum(0)
    precision = hits / torch.arange(1, len(tp) + 1)
    recall = hits / len(gt_boxes)
    envelope = precision.flip(0).cummax(0).values.flip(0)   # best precision at this recall or beyond
    return (envelope * tp).sum().item() / len(gt_boxes), tp, precision

# the worked example: four 40 x 40 objects; detections shifted sideways by d pixels have IoU (40 - d) / (40 + d)
gt = torch.tensor([[x, 0., x + 40, 40] for x in (0, 100, 200, 300)])
shift = lambda g, d: [g[0] + d, g[1], g[2] + d, g[3]]
dets = torch.tensor([shift(gt[0], 2), shift(gt[1], 9), shift(gt[2], 20), shift(gt[0], 8),
                     shift(gt[2], 4), shift(gt[3], 12), [0, 200, 40, 240]])
conf = torch.tensor([0.95, 0.90, 0.80, 0.70, 0.60, 0.50, 0.40])
print(box_iou(dets, gt).max(1).values.numpy().round(3))       # [0.905 0.633 0.333 0.667 0.818 0.538 0.   ]
ap50, tp, prec = average_precision(dets, conf, gt, 0.5)
print(tp.tolist(), prec.numpy().round(3), round(ap50, 4))
# [1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0] [1.    1.    0.667 0.5   0.6   0.667 0.571] 0.8333
thresholds = [0.5 + 0.05 * i for i in range(10)]
aps = [average_precision(dets, conf, gt, t)[0] for t in thresholds]
print([round(a, 3) for a in aps], round(sum(aps) / 10, 4))
# [0.833, 0.65, 0.65, 0.35, 0.35, 0.35, 0.35, 0.25, 0.25, 0.0] 0.4033

# 6. synthetic detector: 200 images with 1 to 4 objects, corners jittered by sigma times the box size,
#    10% of objects missed, one low-scoring false positive per image on average
def synthetic(sigma, seed=0):
    g = torch.Generator().manual_seed(seed)
    gts, dets, confs, img_ids = [], [], [], []
    for img in range(200):
        n = int(torch.randint(1, 5, (1,), generator=g))
        xy = torch.rand(n, 2, generator=g) * 300
        wh = 20 + torch.rand(n, 2, generator=g) * 80
        box = torch.cat([xy, xy + wh], 1)
        gts.append(box)
        found = torch.rand(n, generator=g) > 0.1
        noisy = box + sigma * wh.repeat(1, 2) * torch.randn(n, 4, generator=g)
        dets.append(noisy[found]); confs.append(0.5 + 0.5 * torch.rand(int(found.sum()), generator=g))
        m = int(torch.poisson(torch.tensor([1.0]), generator=g))
        fxy = torch.rand(m, 2, generator=g) * 300
        dets.append(torch.cat([fxy, fxy + 20 + torch.rand(m, 2, generator=g) * 80], 1))
        confs.append(0.7 * torch.rand(m, generator=g))
        img_ids += [img] * (int(found.sum()) + m)
    # boxes of different images must never match: shift each image far apart before pooling
    off = lambda b, i: b + 1000 * i
    G = torch.cat([off(b, i) for i, b in enumerate(gts)])
    D = torch.cat([off(b, i) for i, b in zip(img_ids, torch.cat(dets).split(1))])
    return [average_precision(D, torch.cat(confs), G, t)[0] for t in thresholds]

for sigma in (0.02, 0.05, 0.1):
    a = synthetic(sigma)
    print(sigma, round(a[0], 3), round(a[5], 3), round(sum(a) / 10, 3))   # sigma, AP50, AP75, AP over 0.5:0.95
# 0.02 0.837 0.837 0.757
# 0.05 0.837 0.806 0.593
# 0.1 0.824 0.154 0.362

In the simulation, 10% of the objects are never detected and some false positives score above true detections, so even perfect boxes would not reach AP 1. Corners jittered by 2% of the box size cost nothing at AP50 and AP75 and lower the ten-threshold AP to 0.757; at 10% jitter AP50 is almost unchanged, 0.824, while AP75 collapses to 0.154 and the ten-threshold AP to 0.362. This is the sense in which COCO’s AP measures localization quality and VOC’s mAP at 0.5 mostly does not.

Next#

A box says where an object is but not which pixels belong to it. The next note labels every pixel: it needs a network whose output has the resolution of its input, which the encoder–decoder with skip connections provides, and its own losses and metrics, which reuse the IoU of this note at the level of pixels. Mask R-CNN, at its end, adds exactly this to the Faster R-CNN of this note.

References#