Object Detection
🎯 Learning Objectives
- Understand the difference between image classification and object detection
- Trace the evolution from R-CNN to Faster R-CNN
- Calculate IoU (Intersection over Union) for bounding boxes
- Explain anchor boxes, non-maximum suppression, and region proposal networks
- Compare one-stage detectors (YOLO, SSD) with two-stage detectors (R-CNN family)
- Compute and interpret mAP (mean Average Precision)
1. What Is Object Detection?
Object detection goes beyond image classification: it must localise every object of interest in the image with a bounding box and classify each detected object.
| Task | Output | Example |
|---|---|---|
| Classification | Single label | “cat” |
| Localisation | Single box + label | One cat at (50, 30, 200, 180) |
| Detection | Multiple boxes + labels | 2 cats + 1 dog with boxes |
| Segmentation | Pixel-level masks | Exact shape of each object |
2. Intersection over Union (IoU)
IoU(A, B) = |A ∩ B| / |A ∪ B| = Area of Intersection / Area of Union
IoU ∈ [0, 1]. IoU = 1 means perfect overlap; IoU = 0 means no overlap.
Worked Example — IoU Calculation
Box A: top-left (10, 20), bottom-right (50, 60) ⇒ width = 40, height = 40, area = 1600
Box B: top-left (30, 30), bottom-right (70, 80) ⇒ width = 40, height = 50, area = 2000
Intersection:
- xleft = max(10, 30) = 30
- ytop = max(20, 30) = 30
- xright = min(50, 70) = 50
- ybottom = min(60, 80) = 60
- Intersection width = 50 − 30 = 20
- Intersection height = 60 − 30 = 30
- Intersection area = 20 × 30 = 600
Union: 1600 + 2000 − 600 = 3000
IoU = 600 / 3000 = 0.20
This IoU is below the typical threshold of 0.5, so this detection would be considered a false positive.
3. Early Approaches: Sliding Window & Selective Search
3.1 Sliding Window
Slide a fixed-size window across the image at multiple scales. Classify each window as object / not-object. Extremely slow because of the huge number of candidate windows — O(width × height × scales × aspect-ratios).
3.2 Selective Search
A smarter region proposal method that groups similar pixels using hierarchical segmentation. Produces ~2000 region proposals instead of millions of sliding windows. Used as the first stage in R-CNN.
4. R-CNN Family
4.1 R-CNN (2014)
R-CNN Pipeline
- Region proposals: Use Selective Search to generate ~2000 proposals
- Feature extraction: Warp each proposal to 227×227, pass through a CNN (AlexNet/VGG)
- Classification: SVM classifier per class on CNN features
- Bounding box regression: Refine proposal coordinates
Problem: Very slow (~47 seconds per image on GPU). The CNN runs separately for each of the ~2000 proposals — massive redundant computation.
4.2 Fast R-CNN (2015)
Key improvement: run the CNN once on the entire image to produce a feature map. Then use RoI Pooling (Region of Interest Pooling) to extract fixed-size features for each proposal from the shared feature map.
Speed: ~10× faster than R-CNN at test time. Still relies on external Selective Search for proposals.
4.3 Faster R-CNN (2015)
Replaces Selective Search with a Region Proposal Network (RPN) that shares convolutional features with the detection network. Fully end-to-end trainable.
- 2 scores: objectness (object vs background)
- 4 values: bounding box refinements (Δx, Δy, Δw, Δh)
5. Anchor Boxes
Pre-defined bounding box templates at each spatial location in the feature map. Typically defined by multiple scales (e.g., 128², 256², 512²) and aspect ratios (e.g., 1:1, 1:2, 2:1).
Example — Anchor Count
Feature map: 40 × 60. Scales: 3. Aspect ratios: 3.
Anchors per location: 3 × 3 = 9
Total anchors: 40 × 60 × 9 = 21,600
Each anchor produces classification + regression outputs, which are filtered by NMS.
6. Non-Maximum Suppression (NMS)
After detection, many overlapping boxes may point to the same object. NMS removes redundant detections.
NMS Algorithm
- Sort all detections by confidence score (descending)
- Select the box with highest score → add to final list
- Remove all remaining boxes with IoU > threshold (e.g., 0.5) with the selected box
- Repeat steps 2–3 until no boxes remain
7. YOLO (You Only Look Once)
A one-stage detector that treats detection as a single regression problem. The image is divided into an S × S grid. Each grid cell predicts B bounding boxes and C class probabilities simultaneously.
Example — YOLO v1 Output Size
S = 7, B = 2 boxes per cell, C = 20 classes (Pascal VOC):
Output = 7 × 7 × (2 × 5 + 20) = 7 × 7 × 30 = 1,470 values
Advantages: Extremely fast (45 FPS on GPU for v1, 155+ FPS for Fast YOLO). Sees the entire image during prediction so fewer background errors than R-CNN family.
Disadvantages: Struggles with small objects and multiple objects in the same grid cell. Lower localisation accuracy than two-stage detectors.
8. SSD (Single Shot MultiBox Detector)
Another one-stage detector that makes predictions at multiple feature map scales. Earlier (larger) feature maps detect small objects; later (smaller) feature maps detect large objects.
SSD-300 (input 300×300) uses 6 feature maps with scales from 38×38 down to 1×1, generating ~8,732 default boxes. Speed: ~59 FPS (faster than Faster R-CNN, competitive with YOLO).
9. mAP (mean Average Precision)
The standard evaluation metric for object detection.
Recall = TP / (TP + FN) — of all ground truth objects, how many are found?
Computing mAP
- For each class, sort detections by confidence (descending)
- Compute precision and recall at each detection threshold
- Plot the Precision-Recall curve
- Compute AP (Average Precision) = area under the P-R curve (using interpolation)
- mAP = mean of AP across all classes
Example — AP Calculation (simplified)
5 detections for class “dog” (sorted by confidence), 3 ground-truth dogs:
| # | TP/FP | Precision | Recall |
|---|---|---|---|
| 1 | TP | 1/1 = 1.00 | 1/3 = 0.33 |
| 2 | FP | 1/2 = 0.50 | 1/3 = 0.33 |
| 3 | TP | 2/3 = 0.67 | 2/3 = 0.67 |
| 4 | FP | 2/4 = 0.50 | 2/3 = 0.67 |
| 5 | TP | 3/5 = 0.60 | 3/3 = 1.00 |
Using 11-point interpolation at recall = {0, 0.1, …, 1.0}:
At each recall level, precision is the max precision at that recall or higher.
AP ≈ (max-p at r≥0 + max-p at r≥0.1 + … + max-p at r≥1.0) / 11
= (1.0 × 4 + 0.67 × 4 + 0.60 × 3) / 11 ≈ 0.77
10. One-Stage vs Two-Stage Detectors
| Property | Two-Stage (R-CNN family) | One-Stage (YOLO, SSD) |
|---|---|---|
| Pipeline | Region proposal → classification | Single pass detection |
| Speed | Slower (~5–15 FPS) | Faster (~30–155 FPS) |
| Accuracy | Generally higher mAP | Competitive, slightly lower |
| Small objects | Better (RPN + RoI pooling) | Can struggle (YOLO v1) |
| Training | More complex, multi-stage | Simpler, end-to-end |
| Use case | High-accuracy tasks | Real-time applications |
11. Summary
- IoU measures bounding box overlap; IoU ≥ 0.5 is a common TP threshold.
- R-CNN → Fast R-CNN (shared features + RoI pooling) → Faster R-CNN (+ RPN).
- Anchor boxes provide multi-scale, multi-aspect-ratio templates at each location.
- NMS eliminates duplicate detections by removing lower-confidence overlapping boxes.
- YOLO frames detection as a single regression, achieving real-time speed.
- SSD detects at multiple feature map scales for better small-object performance.
- mAP = mean of per-class Average Precision (area under P-R curve).
- Two-stage detectors are more accurate; one-stage detectors are faster.