Object Detection

GATE Relevance: Medium

🎯 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.

Definition — Object Detection: Given an input image, produce a set of bounding boxes {(x, y, w, h, class, confidence)} where (x, y) is the top-left corner, (w, h) is width and height, class is the object category, and confidence is the detection score.
TaskOutputExample
ClassificationSingle label“cat”
LocalisationSingle box + labelOne cat at (50, 30, 200, 180)
DetectionMultiple boxes + labels2 cats + 1 dog with boxes
SegmentationPixel-level masksExact shape of each object

2. Intersection over Union (IoU)

Definition — IoU: A measure of overlap between two bounding boxes A and B:
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.

IoU Thresholds: A detection is a true positive if IoU ≥ threshold (commonly 0.5). COCO benchmarks also report at IoU = 0.75 and averaged over [0.5, 0.95].

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

  1. Region proposals: Use Selective Search to generate ~2000 proposals
  2. Feature extraction: Warp each proposal to 227×227, pass through a CNN (AlexNet/VGG)
  3. Classification: SVM classifier per class on CNN features
  4. 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.

RoI Pooling: Given a region proposal mapped onto the feature map, divide it into a fixed H×W grid and apply max pooling in each cell. This produces a fixed-size output regardless of the proposal size. Enables end-to-end training of the classifier and regressor.

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.

Region Proposal Network (RPN): A small network sliding over the feature map. At each spatial location, it predicts k anchor boxes of various scales and aspect ratios. For each anchor, it outputs:
  • 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

  1. Sort all detections by confidence score (descending)
  2. Select the box with highest score → add to final list
  3. Remove all remaining boxes with IoU > threshold (e.g., 0.5) with the selected box
  4. Repeat steps 2–3 until no boxes remain
⚠ Limitation: Standard NMS can fail when two objects of the same class overlap significantly (e.g., a crowd of people). Soft-NMS decays scores instead of hard removal, partially addressing this issue.

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.

YOLO Output Tensor: The network outputs an S × S × (B × 5 + C) tensor, where each bounding box has 5 values: (x, y, w, h, confidence). Confidence = P(Object) × IoUpredtruth.

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.

YOLO Versions: YOLOv1 (2015) → YOLOv2/YOLO9000 (2016, anchor boxes, batch norm) → YOLOv3 (2018, multi-scale, FPN) → YOLOv4 (2020, CSPDarknet, mosaic augmentation) → YOLOv5–v8+ (incremental improvements, each by different teams).

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 Key Idea: Attach detection heads (classification + regression) to multiple layers of the backbone network, not just the final layer. Default boxes of various aspect ratios anchor predictions at each spatial location.

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.

Precision = TP / (TP + FP) — of all detections, how many are correct?
Recall = TP / (TP + FN) — of all ground truth objects, how many are found?

Computing mAP

  1. For each class, sort detections by confidence (descending)
  2. Compute precision and recall at each detection threshold
  3. Plot the Precision-Recall curve
  4. Compute AP (Average Precision) = area under the P-R curve (using interpolation)
  5. mAP = mean of AP across all classes

Example — AP Calculation (simplified)

5 detections for class “dog” (sorted by confidence), 3 ground-truth dogs:

#TP/FPPrecisionRecall
1TP1/1 = 1.001/3 = 0.33
2FP1/2 = 0.501/3 = 0.33
3TP2/3 = 0.672/3 = 0.67
4FP2/4 = 0.502/3 = 0.67
5TP3/5 = 0.603/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

PropertyTwo-Stage (R-CNN family)One-Stage (YOLO, SSD)
PipelineRegion proposal → classificationSingle pass detection
SpeedSlower (~5–15 FPS)Faster (~30–155 FPS)
AccuracyGenerally higher mAPCompetitive, slightly lower
Small objectsBetter (RPN + RoI pooling)Can struggle (YOLO v1)
TrainingMore complex, multi-stageSimpler, end-to-end
Use caseHigh-accuracy tasksReal-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.