Object Detection
Where things are, not just what they are
Object detection locates every instance of known classes inside an image — boxes plus labels. Two families dominate: two-stage (R-CNN) and one-stage (SSD/YOLO), trading accuracy for latency.
What you will learn
- Explain the detection output: boxes + classes, IoU, NMS
- Compare one-stage vs two-stage detector trade-offs
- Read NMS and anchor side-effects when boxes duplicate
What is this?
Object detection outputs a set of bounding boxes, each with a class and confidence. Two-stage detectors (Faster R-CNN) first propose candidate regions then classify each; one-stage detectors (SSD, YOLO) predict boxes and classes directly from the feature grid — faster but historically less accurate at small objects. Evaluation uses IoU against ground truth and mean Average Precision (mAP).
Why do you need it?
- Classification answers 'what'; detection answers 'what AND where' — the core of surveillance, X-rays, satellites and self-driving.
- The speed/accuracy split is a real deployment decision: one-stage for real-time, two-stage for accuracy-on-small.
- Chasing detection quality means debugging losses, anchors and NMS — three separate systems.
- It's the foundation that segmentation and tracking build on.
Mental model
A zookeeper scanning an enclosure
Object detection
The keeper must count and locate every animal (boxes) while judging each species (class). Hasty keepers (one-stage) miss tiny monkeys in the trees; meticulous keepers (two-stage) inspect candidate regions more carefully at the cost of time.
How it works
- Anchors: predefined boxes at each feature-grid cell (aspect ratios/sizes) — the 'candidate boxes' the head refines.
- Two-stage: RPN proposes ~2k regions, crops them, RegionCNN head classifies each — slow but precise.
- One-stage: the head regresses deltas to anchors and class probabilities directly on the dense grid — fast.
- IoU: overlap between predicted and ground-truth box; matches ordered by confidence.
- NMS: suppresses lower-confidence boxes that heavily overlap a kept one (duplicates).
From features to final boxes.
In production
- YOLO-family serves real-time video analytics (traffic, retail, security) on cheap GPUs/edge.
- Faster R-CNN models audit documents/medical images where small-object recall beats latency.
- Custom heads on detector frameworks (Detectron2, MMDetection) dominate industry adapts.
Best practices
- Start with a pretrained detector and fine-tune; training from scratch needs enormous data.
- Match your two metrics (mAP@.5 and mAP@.5:.95) to the deployment's tolerance to localization error.
- Tune anchor sizes to YOUR typical object scale — the #1 small-object win.
- Set NMS IoU threshold deliberately: too high = duplicates, too low = missed instances.
Common mistakes
- Reusing classification augmentations (flips can break 'left/right' detector semantics in traffic).
- Evaluating with raw confidence instead of AP thresholds — mAP tells a different story.
- Feeding un-resized images to a fixed-input detector and silently losing aspect ratio.
- Expecting one-stage accuracy without its anchor/scale tuning.
Common issues
Two boxes on one object at inferenceNMS IoU threshold too strict (no suppression)
Lower NMS IoU threshold (e.g. 0.45→0.5 is common; tune negative) or add score cutoff.
Small objects never detectedAnchors too large or backbone stride dropping them to one feature cell
Add small anchors/P-2 features or a separate small-object head.
High false positives on backgroundScore threshold too low or class imbalance in data
Raise score threshold, add hard negatives, and balance the class weights.
Debugging tips
- Qualitative-first: print grid of detections (boxes,NMS artefacts) — easier than reading mAP.
- Diagnose by failure category: missed = recall/anchors; duplicated = NMS; misplaced = localization loss weight.
- Check per-class AP, not just mAP — a top-2 class can hide the bottom-10.
- Visualize anchors on a few images to confirm they respect the object scale distribution.
Interview checkpoint
Hands-on lab
- Implement IoU from two bounding boxes in numpy
- Implement a minimal NMS routine and verify on crafted cases
- Understand threshold effects on duplicates and misses
Compute IoU and NMS by Hand
30 min · advanced
Quick quiz
What does NMS (non-maximum suppression) do?
mAP is computed only at a single IoU threshold.