Quiz 2
Registry Synced

Object Detection: R-CNN, YOLO, and SSD

930 words
5 min read

Reading compass

Now · 🎯 Learning Objectives

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:
  1. Localization: Predict bounding box coordinates (x, y, w, h)
  2. Classification: Predict class for each box
  3. Multiple objects: Handle variable number of objects

1.2 Intersection over Union (IoU)

IoU=Area of OverlapArea of Union\text{IoU} = \frac{\text{Area of Overlap}}{\text{Area of Union}}
  • 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:
  1. Sort detections by confidence score
  2. Pick highest confidence detection
  3. Remove all other detections with IoU > threshold (e.g., 0.5)
  4. Repeat until no detections left

1.4 R-CNN Family

(Diagram)
ModelSpeedmAP (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:
L=λcoordi=0S2j=0B1ijobj[(xix^i)2+(yiy^i)2+(wiw^i)2+(hih^i)2]\mathcal{L} = \lambda_{coord} \sum_{i=0}^{S^2} \sum_{j=0}^B \mathbb{1}_{ij}^{obj} [(x_i - \hat{x}_i)^2 + (y_i - \hat{y}_i)^2 + (\sqrt{w_i} - \sqrt{\hat{w}_i})^2 + (\sqrt{h_i} - \sqrt{\hat{h}_i})^2] +i=0S2j=0B1ijobj(CiC^i)2+λnoobji=0S2j=0B1ijnoobj(CiC^i)2+ \sum_{i=0}^{S^2} \sum_{j=0}^B \mathbb{1}_{ij}^{obj} (C_i - \hat{C}_i)^2 + \lambda_{noobj} \sum_{i=0}^{S^2} \sum_{j=0}^B \mathbb{1}_{ij}^{noobj} (C_i - \hat{C}_i)^2 +i=0S21iobjcclasses(pi(c)p^i(c))2+ \sum_{i=0}^{S^2} \mathbb{1}_{i}^{obj} \sum_{c \in classes} (p_i(c) - \hat{p}_i(c))^2

1.6 Anchor Boxes

Anchors are pre-defined boxes of different shapes and sizes at each grid cell:
ScaleAspect RatioCommon Use
Small1:1Faces, close objects
Medium2:1Standing people, cars
Large3:1Wide objects, lying poses
Small1:2Tall, narrow objects
Medium1:3Very tall objects
Faster R-CNN: 9 anchors per spatial location (3 scales × 3 aspect ratios)

1.7 Mean Average Precision (mAP)

  1. Sort all detections by confidence
  2. Compute precision-recall curve
  3. Average Precision (AP) = Area under PR curve
  4. mAP = Mean of AP across all classes
AP=k=1nP(k)ΔR(k)AP = \sum_{k=1}^n P(k) \cdot \Delta R(k)

📝 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 = 625
Overlap: x_min=15, y_min=15, x_max=30, y_max=30 Overlap area = 15×15 = 225
Union = 400 + 625 - 225 = 800
IoU = 225/800 = 0.281
This 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 values
This 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?
  1. Pick A (highest score: 0.9)
  2. Remove C (IoU with A = 0.7 > 0.5 threshold)
  3. B remains (IoU with A = 0.4 < 0.5)
  4. Next iteration: Pick B (next highest: 0.8)
  5. 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.
AspectOne-StageTwo-Stage
SpeedFast (real-time)Slower (but improved)
AccuracyGood, struggles on small objectsBetter, especially small objects
ArchitectureSingle networkRPN + Detection network
TrainingDirect regressionTwo-stage training
Use caseReal-time video, mobileHigh accuracy, offline
Both approaches have converged in recent years — modern detectors like YOLOv8, EfficientDet, and DETR blur the boundaries.
</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)
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.