YOLO, Faster R-CNN, and Modern Object Detection
Module: Computer Vision | Difficulty: Premium
Intersection over Union
Anchor Box Regression
Classification Loss (Focal Loss)
where is the model's estimated probability for the ground-truth class.
Non-Maximum Suppression
| Detector | Stage | Speed | mAP | Paradigm |
|---|---|---|---|---|
| R-CNN | Two | Slow | 58.5% | Selective Search |
| Faster R-CNN | Two | 5 fps | 73.2% | RPN |
| SSD | One | 59 fps | 76.8% | Multi-scale anchors |
| YOLOv8 | One | 140 fps | 78.4% | Anchor-free |
| DETR | One | 28 fps | 78.9% | Set prediction |
import torch
def compute_iou(box1, box2):
x1 = torch.max(box1[:, None, 0], box2[None, :, 0])
y1 = torch.max(box1[:, None, 1], box2[None, :, 1])
x2 = torch.min(box1[:, None, 2], box2[None, :, 2])
y2 = torch.min(box1[:, None, 3], box2[None, :, 3])
inter = (x2 - x1).clamp(0) * (y2 - y1).clamp(0)
area1 = (box1[:, 2] - box1[:, 0]) * (box1[:, 3] - box1[:, 1])
area2 = (box2[:, 2] - box2[:, 0]) * (box2[:, 3] - box2[:, 1])
union = area1[:, None] + area2[None, :] - inter
return inter / (union + 1e-6)
def nms(boxes, scores, iou_threshold=0.5):
order = scores.argsort(descending=True)
keep = []
while order.numel() > 0:
i = order[0].item()
keep.append(i)
if order.numel() == 1:
break
ious = compute_iou(boxes[i:i+1], boxes[order[1:]])[0]
mask = ious < iou_threshold
order = order[1:][mask]
return keep
Research Insight: DETR (Detection Transformer) reframed object detection as a set prediction problem, eliminating hand-crafted components like anchors and NMS. Its bipartite matching loss assigns predictions to ground-truth via the Hungarian algorithm, producing a permutation-invariant training objective. Despite slower convergence, DETR and its successors (Deformable DETR, DINO) have become the dominant paradigm for open-vocabulary and zero-shot detection.