Skip to content
ml-recall

Topic notes

Object Detection

Finding and classifying objects with bounding boxes; box formats and IoU, anchors and offsets, non-maximum suppression, two-stage vs one-stage detectors, focal loss, and AP / mAP evaluation.

5-min read AI-checked

Practise 47 questions Take a test

Key ideas

Formulas

IoU(A,B)=∣A∩B∣∣A∣+∣B∣−∣A∩B∣,tx=x−xawa, ty=y−yaha, tw=log⁡wwa, th=log⁡hha\mathrm{IoU}(A, B) = \frac{\lvert A \cap B \rvert}{\lvert A \rvert + \lvert B \rvert - \lvert A \cap B \rvert},\qquad t_x = \frac{x - x_a}{w_a},\ t_y = \frac{y - y_a}{h_a},\ t_w = \log\frac{w}{w_a},\ t_h = \log\frac{h}{h_a}

Faster R-CNN regresses these offsets directly4; d2l divides tx,tyt_x, t_y by 0.1 and tw,tht_w, t_h by 0.21.

FL(pt)=−αt(1−pt)γlog⁡pt(γ=2 works well),b=−log⁡1−ππ\mathrm{FL}(p_t) = -\alpha_t (1 - p_t)^{\gamma}\log p_t \quad (\gamma = 2 \text{ works well}),\qquad b = -\log\frac{1 - \pi}{\pi}

Here ptp_t is the predicted probability of the true class and bb the prior bias6. Smooth L1 with parameter σ\sigma is (σx)2/2(\sigma x)^2/2 for ∣x∣<1/σ2\lvert x \rvert < 1/\sigma^2 and ∣x∣−0.5/σ2\lvert x \rvert - 0.5/\sigma^2 otherwise2.

precision=TPTP+FP,recall=TPTP+FN,AP≈1#GT∑ranks of TPsprecision@rank\text{precision} = \frac{TP}{TP + FP},\qquad \text{recall} = \frac{TP}{TP + FN},\qquad \text{AP} \approx \frac{1}{\#\text{GT}}\sum_{\text{ranks of TPs}} \text{precision@rank}

Worked example

Boxes A=(0,0,10,10)A = (0, 0, 10, 10) score 0.9, B=(5,0,15,10)B = (5, 0, 15, 10) score 0.8, C=(1,1,11,11)C = (1, 1, 11, 11) score 0.7, D=(20,20,30,30)D = (20, 20, 30, 30) score 0.6.

In code

import torch
from torchvision.ops import batched_nms, box_iou, nms
boxes = torch.tensor([[0., 0., 10., 10.],    # A  (x1, y1, x2, y2)
                      [5., 0., 15., 10.],    # B
                      [1., 1., 11., 11.],    # C
                      [20., 20., 30., 30.]]) # D
scores = torch.tensor([0.9, 0.8, 0.7, 0.6])
print(box_iou(boxes, boxes)[0])               # tensor([1.0000, 0.3333, 0.6807, 0.0000])
print(nms(boxes, scores, iou_threshold=0.5))  # tensor([0, 1, 3])
labels = torch.tensor([0, 0, 1, 0])           # now C belongs to another class
print(batched_nms(boxes, scores, labels, 0.5))  # tensor([0, 1, 2, 3])  C survives

Common pitfalls

References

  1. Dive into Deep Learning — Sec. 14.4.1 Generating Multiple Anchor Boxes; 14.4.2 Intersection over Union (IoU); 14.4.3 Labeling Anchor Boxes in Training Data; 14.4.4 Predicting Bounding Boxes with Non-Maximum Suppression
  2. Dive into Deep Learning — Sec. 14.7.1 Model (14.7.1.1 Class Prediction Layer; 14.7.1.2 Bounding Box Prediction Layer); 14.7.2.2 Defining Loss and Evaluation Functions; 14.7.5 Exercises (smooth L1 loss)
  3. Dive into Deep Learning — Sec. 14.8.1 R-CNNs; 14.8.2 Fast R-CNN; 14.8.3 Faster R-CNN
  4. Ren et al. (2015), Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks — Sec. 3.1.1 Anchors; 3.1.2 Loss Function (Eq. 2); 3.1.3 Training RPNs; 4.2 Experiments on MS COCO (Table 11)
  5. Redmon et al. (2015), You Only Look Once: Unified, Real-Time Object Detection — Sec. 1 Introduction; 2 Unified Detection; 2.4 Limitations of YOLO
  6. Lin et al. (2017), Focal Loss for Dense Object Detection — Sec. 2 Related Work (Class Imbalance); 3.2 Focal Loss Definition; 3.3 Class Imbalance and Model Initialization; 3.4 Class Imbalance and Two-stage Detectors; 4.1 Inference and Training (Initialization)
  7. Lin et al. (2016), Feature Pyramid Networks for Object Detection — Sec. 3 Feature Pyramid Networks (Top-down pathway and lateral connections); 4.1 Feature Pyramid Networks for RPN
  8. Hosang et al. (2017), Learning non-maximum suppression — Sec. 1 Introduction; 4 Doing NMS with a convnet (4.1 Loss, matching of detections to objects)