Object Detection: R-CNN, YOLO, and SSD
930 words
5 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Object Detection: R-CNN, YOLO, and SSD ## 🎯 Learning Objectives - Distinguish object detection from classification - Explain the R-CNN family evolution (R-CNN → Fast R-CNN → Faster R-CNN) - Implement YOLO's grid-based detection approach - Understand anchor boxes, IoU, NMS, and mAP ## 📋 Prerequisites - CNN fundam...

Object Detection: R-CNN, YOLO, and SSD
🎯 Learning Objectives
- Distinguish object detection from classification
- Explain the R-CNN family evolution (R-CNN → Fast R-CNN → Faster R-CNN)
- Implement YOLO's grid-based detection approach
- Understand anchor boxes, IoU, NMS, and mAP
📋 Prerequisites
- CNN fundamentals
- Bounding box representation
- Classification vs regression
1. 📖 Core Content
1.1 The Detection Problem
Classification: "Is there a cat in this image?" → One label per image Detection: "Where is the cat and where is the dog?" → Multiple bounding boxes + labels per image
Detection requires:
- Localization: Predict bounding box coordinates (x, y, w, h)
- Classification: Predict class for each box
- Multiple objects: Handle variable number of objects
1.2 Intersection over Union (IoU)
IoU=Area of UnionArea of Overlap- IoU > 0.5: Typically considered "good" detection
- IoU > 0.7: Tight/accurate detection
- IoU = 0: No overlap
1.3 Non-Maximum Suppression (NMS)
NMS removes duplicate detections:
- Sort detections by confidence score
- Pick highest confidence detection
- Remove all other detections with IoU > threshold (e.g., 0.5)
- Repeat until no detections left
1.4 R-CNN Family
(Diagram)
| Model | Speed | mAP (COCO) | Key Innovation |
|---|---|---|---|
| R-CNN | ~50s/image | ~42% | First DL detection |
| Fast R-CNN | ~2s/image | ~46% | Shared conv, RoI pooling |
| Faster R-CNN | ~0.2s/image | ~48% | Learnable RPN |
1.5 YOLO (You Only Look Once)
YOLO frames detection as a single regression problem:
python# runnable import numpy as np def yolo_encoding(image_grid_size, num_boxes, num_classes): """ YOLO output encoding Each grid cell predicts: - For each box: (x, y, w, h, confidence) - Class probabilities: (num_classes,) Total output: grid × grid × (num_boxes × 5 + num_classes) """ S = image_grid_size # e.g., 7 for YOLOv1 B = num_boxes # e.g., 2 C = num_classes # e.g., 20 (PASCAL VOC) output_shape = (S, S, B * 5 + C) print(f"YOLO output shape: {output_shape}") print(f"Total predictions: {S * S * B} boxes") print(f"Output vector per cell: {B * 5 + C} values") return output_shape # Example shape = yolo_encoding(7, 2, 20)
YOLO loss function:
1.6 Anchor Boxes
Anchors are pre-defined boxes of different shapes and sizes at each grid cell:
| Scale | Aspect Ratio | Common Use |
|---|---|---|
| Small | 1:1 | Faces, close objects |
| Medium | 2:1 | Standing people, cars |
| Large | 3:1 | Wide objects, lying poses |
| Small | 1:2 | Tall, narrow objects |
| Medium | 1:3 | Very tall objects |
Faster R-CNN: 9 anchors per spatial location (3 scales × 3 aspect ratios)
1.7 Mean Average Precision (mAP)
- Sort all detections by confidence
- Compute precision-recall curve
- Average Precision (AP) = Area under PR curve
- mAP = Mean of AP across all classes
📝 Practice Questions
Q1<strong>Q1</strong>: For two boxes A=(10,10,20,20) and B=(15,15,25,25), compute IoU.A: (10,10) to (30,30), area = 20×20 = 400 B: (15,15) to (40,40), area = 25×25 = 625Overlap: x_min=15, y_min=15, x_max=30, y_max=30 Overlap area = 15×15 = 225Union = 400 + 625 - 225 = 800IoU = 225/800 = 0.281This is a fairly low IoU — the boxes overlap but one is much larger. Q2<strong>Q2<strong>Q2</strong>: YOLOv1 uses 7×7 grid, 2 boxes, 20 classes. What's the output tensor size per image?Output = 7 × 7 × (2 × 5 + 20) = 49 × 30 = 1470 valuesThis is the total number of values in the output tensor. For each of the 49 grid cells, the model predicts 30 values (10 for box coordinates/confidence + 20 class probabilities). Q3<strong>Q3<strong>Q3</strong>: Why does YOLO use √w and √h in its loss instead of w and h?Using w and h directly would penalize large boxes and small boxes unequally. The same absolute error in width means more for a small box than a large one.By using √w and √h, the error is proportional to the box size:
- Small box (w=10): Δw=2 → √12 - √10 = 3.46 - 3.16 = 0.30
- Large box (w=100): Δw=2 → √102 - √100 = 10.10 - 10.00 = 0.10
The square root transformation makes the loss more sensitive to small box errors. Q4<strong>Q4</strong>: NMS threshold is 0.5. Detection A (score 0.9), B (0.8), C (0.7). IoU(A,B)=0.4, IoU(A,C)=0.7. Which are kept?
- Pick A (highest score: 0.9)
- Remove C (IoU with A = 0.7 > 0.5 threshold)
- B remains (IoU with A = 0.4 < 0.5)
- Next iteration: Pick B (next highest: 0.8)
- No more detections to compare
Kept: A and B. C is suppressed because it overlaps too much with A. Q5<strong>Q5<strong>Q5<strong>Q5</strong>: Compare one-stage (YOLO, SSD) vs two-stage (Faster R-CNN) detectors.
| Aspect | One-Stage | Two-Stage |
|---|---|---|
| Speed | Fast (real-time) | Slower (but improved) |
| Accuracy | Good, struggles on small objects | Better, especially small objects |
| Architecture | Single network | RPN + Detection network |
| Training | Direct regression | Two-stage training |
| Use case | Real-time video, mobile | High accuracy, offline |
</details> * * * ## 🔗 Cross-References - **Next**: [Segmentation](/notes/04-degree-electives-bsda5006-dl-cv-week05-05-segmentation) - **Video**: BSDA5006 Week 6 transcripts [Join Discord](https://discord.gg/gE2m4Qrdqv) [Previous**Transfer Learning**](/notes/04-degree-electives-bsda5006-dl-cv-week03-03-transfer-learning)[Next**Segmentation**](/notes/04-degree-electives-bsda5006-dl-cv-week05-05-segmentation)Both approaches have converged in recent years — modern detectors like YOLOv8, EfficientDet, and DETR blur the boundaries.