Sub topic 2
Two-stage detection: from R-CNN to Faster R-CNN
This chapter assumes you know Python, NumPy, how convolutional feature maps work, gradient-descent training, and IoU (intersection over union) for boxes.
Two-stage detectors first propose regions that may contain objects, then classify and refine each proposal. R-CNN (2014) ran a CNN on about 2,000 proposals per image, so each image needed thousands of forward passes. Fast R-CNN shared one backbone pass across all proposals. Faster R-CNN replaced the external proposal step with a Region Proposal Network (RPN), which predicts an objectness score and box offsets for each anchor.
Deciding which anchors are positives
Anchors are reference boxes placed at every feature-map location. An anchor is a positive when its IoU with a ground-truth box is at least 0.7, or when it has the highest IoU with some ground-truth box. Anchors with IoU below 0.3 are negatives. Anchors in between are ignored during training.
Classification and box losses, and focal loss
Both the RPN and the detection head use a classification loss and a box regression loss on positive anchors. Negatives far outnumber positives, so easy negatives can dominate training. Focal loss fixes this by down-weighting well-classified examples. It multiplies cross-entropy by (1 - p_t) ** gamma, where p_t is the predicted probability of the true class. With gamma = 2, a confident correct prediction contributes almost nothing.
Removing duplicates with non-maximum suppression
Many anchors fire on the same object. Non-maximum suppression (NMS) keeps the highest-scoring box, removes every remaining box whose IoU with it exceeds a threshold (commonly 0.5), and repeats on what is left.
def iou(a, b):
ix1, iy1 = max(a[0], b[0]), max(a[1], b[1])
ix2, iy2 = min(a[2], b[2]), min(a[3], b[3])
inter = max(0.0, ix2 - ix1) * max(0.0, iy2 - iy1)
union = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter
return inter / union if union > 0 else 0.0
def nms(boxes, scores, threshold=0.5):
order = sorted(range(len(boxes)), key=lambda i: scores[i], reverse=True)
keep = []
while order:
best = order.pop(0)
keep.append(best)
order = [j for j in order if iou(boxes[best], boxes[j]) <= threshold]
return keep
print(nms([(0, 0, 10, 10), (1, 1, 11, 11), (50, 50, 60, 60)], [0.9, 0.8, 0.7])) # [0, 2]Single-stage detectors: SSD, RetinaNet and YOLO
Single-stage detectors skip the proposal step and predict classes and boxes directly from feature maps. SSD predicts from several feature maps at different resolutions. YOLO divides the image into a grid, and each cell predicts boxes. RetinaNet pairs a feature pyramid with focal loss, which closed much of the accuracy gap with two-stage detectors.
Feature pyramids for objects of different sizes
Deep feature maps are coarse but semantically strong. Shallow maps are detailed but weak in meaning. A feature pyramid network (FPN) adds top-down connections so every level has both. Detection then runs on each level, with anchors of different sizes per level.
::: callout note The thresholds 0.7, 0.3 and 0.5 are common defaults, not fixed rules. Check the configuration of the detector you use. :::
Exercise
Two boxes have IoU 0.6 with each other. Their scores are 0.9 and 0.8, and the NMS threshold is 0.5. Which box is kept?
Show answer
The 0.9 box is kept. The 0.8 box is removed because 0.6 is greater than 0.5.