Object detection (R-CNN, YOLO, SSD)
Overview
Object detection is the task of simultaneously locating (where?) and classifying (what?) multiple objects within an image. Unlike image classification (which only answers "what?") or semantic segmentation (which labels every pixel but doesn't distinguish instances), object detection outputs bounding boxes with class labels for each detected object.

The fundamental challenge: turning a classification network into a localization + classification system that works for variable numbers of objects.
The Evolution: Three Paradigms
1. R-CNN Family (Region-based Methods)
WHY this architecture?
- Separates "where to look" (region proposals) from "what is it" (classification)
- Leverages CNNs' strength at fixed-size image classification
- Reduces search space from all possible boxes (millions) to promising candidates
HOW it works:
Each region is:
- Warped to a fixed size (227×227 for AlexNet)
- Passed through a CNN to extract a feature vector
- Classified with an SVM
- Refined with a bounding box regressor
WHY these formulas?
- Translation (): Offset as fraction of box width/height (scale-invariant)
- Scale (): Exponential ensures positive width/height, makes learning easier (log-space is more linear)
The network learns by minimizing:
where are the true transformation targets:
Fast R-CNN improvement: Instead of running CNN2000 times, extract features once for the whole image, then use RoI (Region of Interest) pooling to extract fixed-size features for each proposal.
Faster R-CNN improvement: Replace Selective Search with a learned Region Proposal Network (RPN).
For each anchor:
- Objectness score: (is there an object?)
- Box refinement: as above
Loss function combines both:
WHY anchor boxes?
- Handle multiple scales/aspect ratios without resizing
- Provide reference coordinates for box regression
- Typical: 3scales × 3 ratios = 9 anchors per position
After NMS (Non-Maximum Suppression), keep top 300. For each:
- Extract 7×7 RoI pooled features
- Two fully connected layers
- Output: 21 classes (20 objects + background) + 4×21 box offsets
Why this step? The 7×7 pooling standardizes features regardless of proposal size. The 4×21 offsets allow class-specific box refinement (a "car" box might need different adjustments than a "person" box).
2. YOLO (You Only Look Once)
WHY single-stage?
- Speed: One forward pass instead of thousands
- Global context: Sees entire image, reducing background false positives
- Trade-off: Slightly lower accuracy on small objects
HOW YOLO works:
Divide image into grid (e.g., 7×7). Each grid cell predicts:
- bounding boxes (each with5 values: )
- class probabilities
Output tensor:
For YOLO v1:
where:
- : Box center relative to grid cell (values0–1)
- : Box size relative to image (values 0–1)
WHY this confidence formula?
- If no object: , confidence should be 0
- If object exists: Confidence reflects how well the box fits (IoU)
- During training, this is supervised. At test time, it's predicted.
Each grid cell also predicts:
Final class-specific confidence:
WHY each term?
-
Localization loss (x, y): MSE on box centers. Only penalize the box "responsible" for the object ( if box in cell has highest IoU).
-
Size loss (w, h): Use because small deviations in large boxes matter less than in small boxes. Square root makes errors more comparable across scales.
-
Confidence loss (has object): Penalize when exists but confidence is low.
-
Confidence loss (no object): Weighted lower () because most boxes contain no object — prevents overwhelming the gradient.
-
Classification loss: Standard MSE on class probabilities for cells containing objects.
increases importance of localization.
Cell (3, 4) contains a dog's center:
- Box 1predicts: (relative to cell and image)
- Cell predicts:
- Final: confidence for "dog"
Why this step? Cell (3, 4) is responsible because the object's center is in that cell. The box with highest IoU (box 1) is the one penalized during training. The 0.782 final score combines "is there an object?" with "what is it?".
YOLO v2/v3 improvements:
- Anchor boxes (like Faster R-CNN): Predict offsets from anchors instead of absolute coordinates
- Multi-scale predictions: Detect at3 different feature map resolutions (for small, medium, large objects)
- Better backbone: Darknet-53 (ResNet-like with skip connections)
3. SSD (Single Shot MultiBox Detector)
WHY multi-scale?
- Early layers (high resolution): Detect small objects
- Late layers (low resolution): Detect large objects
- Each layer specializes in a scale range
HOW it works:
Take 6 different feature maps from the CNN at different stages (e.g., 38×38, 19×19, 10×10, 5×5, 3×3, 1×1). For each location in each map, predict:
where = number of classes, 4 = box offsets.
Localization:
Same as Faster R-CNN box regression: predict offsets relative to default box .
Classification:
for each class (including background).
where:
- = number of matched default boxes
- (weight balance)
Localization loss (Smooth L1):
Confidence loss (Softmax):
where
WHY Smooth L1?
- When error is small (|x| < 1): Behaves like L2 (gradients don't vanish)
- When error is large (|x| ≥ 1): Behaves like L1 (less sensitive to outliers, more stable)
Feature map 5×5 (late layer):
- Receptive field: ~300 pixels
- Default boxes: 6 boxes per location (scales 0.6-0.9, more aspect ratios)
- Good for detecting: cars, buses, large objects
Why this step? A small object (e.g., 30×30 pixels) would be barely visible in the 5×5 map (just 0.5 cells), but well-represented in the 38×38 map (4-5 cells). Multi-scale matching ensures each object is predicted where it's most salient.
Key Techniques Across All Methods
Non-Maximum Suppression (NMS)
All detectors output many overlapping boxes. NMS keeps only the best box per object.
WHY this works? Boxes with high IoU are detecting the same object. Keep the most confident one, discard redundant detections.
Derivation of IoU (Intersection over Union):
Given two boxes and :
WHY this metric?
- IoU = 1: Perfect overlap (same box)
- IoU = 0: No overlap
- IoU ∈ [0, 1]: Measures box similarity geometrically
- Standard: IoU > 0.5 considered a "match" for evaluation
Hard Negative Mining
Most default boxes/anchors contain background, creating class imbalance.
Solution: After matching, sort boxes by confidence loss and keep top-K negatives (e.g., 3:1 ratio negative:positive).
WHY? Without this, the network learns to predict "background" everywhere and never detects objects. Hard negatives are the most confusing backgrounds (high loss) — focusing on these improves discrimination.
Comparison: When to Use Each
| Method | Speed (FPS) | Accuracy (mAP) | Best For | Trade-off | |--------|-------------|----------|--------| | Faster R-CNN | ~76% | High-accuracy applications (medical, autonomous vehicles) | Slow, two-stage | | YOLO v3 | ~30 | ~55% | Real-time video (surveillance, sports) | Mises small objects | | SSD | ~25 | ~68% | Balanced speed/accuracy (mobile apps) | Middle ground |
WHY these trade-offs?
- Two-stage (R-CNN): Region proposals → separate classification = more computation, better localization
- Single-stage (YOLO/SSD): Direct prediction = faster, but harder to learn precise localization
- Multi-scale (SSD): Better than YOLO on small objects, but slower than YOLO
Common Mistakes
Why it feels right: Both are between 0 and 1, both relate to detection quality.
The fix:
- Objectness/Confidence: "Is there any object here?" (binary)
- Classification: "What object is it?" (multi-class)
- Final score = objectness × classification
In YOLO: Each grid cell predicts one set of class probabilities (shared by all boxes) and per-box confidences. You multiply them for class-specific scores.
Steel-man: The confusion arises because in Faster R-CNN, these are combined in the RPN (objectness) and R-CNN head (classification), making the separation less obvious.
Why it feels right: MSE is the standard regression loss.
The fix: Consider two errors:
- Box1: 10×10 predicted as 15×15 (error = 5)
- Box 2: 100×100 predicted as 105×105 (error = 5)
With MSE on w, h: Both have loss = 25.
But Box 2's error is only 5% relative error, while Box 1's is 50%!
With MSE on :
- Box 1: , , error ≈ 0.71, loss ≈ 0.5
- Box 2: , , error ≈ 0.25, loss ≈ 0.06
Now Box 1's loss is 8× larger, correctly reflecting the larger relative error.
Steel-man: The intuition "absolute error should matter equally" works for quantities of similar scale, but bounding boxes span orders of magnitude. Square root compresses the range, making loss more proportional to relative error.
Why it feels right: More anchors → more coverage → better chance of matching ground truth.
The fix:
- Computational cost: Linear in number of anchors (slower training, inference)
- Label assignment ambiguity: With too many anchors, multiple anchors match the same object equally well → inconsistent gradients
- Overfitting: More parameters to learn anchor-specific patterns
Sweet spot: 3 scales × 3 aspect ratios = 9 anchors captures most object variations. Empirically validated in papers.
Steel-man: The intuition comes from "more capacity = more learning power," which works for model depth/width. But anchors are hand-designed priors, not learned features — more isn't always better, smart selection is.
Active Recall
Recall Explain object detection to a 12-year-old
Imagine you're playing "Where's Waldo?" but the computer is doing it.
Old way (R-CNN): The computer says, "Let me check if Waldo is in this box... nope. How about this box? Nope. This one? Maybe..." It checks2000 boxes one by one! Super slow.
New way (YOLO): The computer divides the picture into a grid, like a checkerboard. Each square shouts out, "I see a person here! Confidence: 85%!" and "I see a dog here! 92%!" All at once! Super fast.
Even better way (SSD): Like YOLO, but uses multiple grids—a coarse one for big things (cars) and a fine one for tiny things (faces). Like using a magnifying glass for small details and binoculars for distant objects.
The computer learns by showing it thousands of labeled pictures: "This is where the cat is, draw a box around it." Gradually, it learns cat-shapes, car-shapes, person-shapes. When it gets one wrong, we tell it, "The box should be a bit to the left" or "That's not a cat, it's a dog!" and it adjusts.
The magic: It not only says what (classification), but also where (localization)—drawing boxes around every object!
Or: Refined, Yappy, Scalable
Connections
- Convolutional Neural Networks: The backbone feature extractors (VGG, ResNet, Darknet)
- Image Classification: Object detection extends classification with localization
- Semantic Segmentation: Pixel-level labeling vs. box-level in detection
- Transfer Learning: Detectors often start with ImageNet-pretrained backbones
- Anchor Boxes: Core technique in Faster R-CNN, YOLO v2+, SSD
- Non-Maximum Suppression: Post-processing to remove duplicate detections
- Intersection over Union (IoU): Evaluation metric and matching criterion
- Batch Normalization: Stabilizes training in deep detector backbones
- Data Augmentation: Critical for detection (random crops, flips, color jitter)
- Instance Segmentation: Next step beyond detection (Mask R-CNN)
Flashcards
#flashcards/ai-ml
What is the fundamental difference between image classification and object detection? :: Image classification outputs a single label for the whole image. Object detection outputs multiple bounding boxes with class labels, solving both "what" (classification) and "where" (localization).
What are the two stages in R-CNN?
Why does YOLO use square roots in the size loss?
What is the purpose of anchor boxes in Faster R-CNN?
Derive the bounding box center offset formula in Faster R-CNN.
What is the confidence score in YOLO?
Why does SSD use multiple feature maps?
What is Non-Maximum Suppression and why is it needed?
What is Hard Negative Mining in SD?
Derive IoU (Intersection over Union).
Why is Smooth L1 loss used in localization?
What is the trade-off between one-stage and two-stage detectors?
How does YOLO divide the detection task across grid cells?
What is the Region Proposal Network (RPN) in Faster R-CNN?
Why does YOLO multiply objectness and class probability?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Chalo, object detection ko simple tarike se samajhte hain. Jab tum ek normal image classification network banate ho, to woh sirf ek sawaal ka jawab deta hai — "yeh photo mein kya hai?" Lekin real duniya mein ek image ke andar bohot saari cheezein ek saath hoti hain — ek car, do log, teen kutte. Object detection ka core intuition yeh hai ki humein do cheezein ek saath karni hain: locate karna (object kahaan hai — bounding box) aur classify karna (yeh kya hai — label). Problem yeh hai ki humein pehle se pata nahi hota ki kitne objects hain, woh kahaan hain, aur kis scale pe hain. Isiliye yeh classification se kaafi mushkil task hai.
Ab is problem ko solve karne ke liye teen approaches evolve hui. R-CNN family (R-CNN, Fast, Faster R-CNN) ek two-stage idea use karti hai — pehle "kahaan dekhna hai" (region proposals) nikaalo, phir "yeh kya hai" (classification) karo. Shuruaat mein Selective Search se 2000 candidate boxes bante the aur har box pe alag se CNN chalti thi, jo bohot slow tha. Fir Fast R-CNN ne feature ek hi baar poori image se nikaal ke RoI pooling se speed badhaayi, aur Faster R-CNN ne Selective Search ko hi ek learned network (RPN) se replace kar diya. Ismein anchor boxes ka concept important hai — yeh pre-defined shapes hote hain jo multiple scales aur aspect ratios ko handle karte hain, aur box regression ke liye reference dete hain. Box regression mein hum offset ko width/height ke fraction mein aur size ko log-space mein predict karte hain taaki learning smooth aur scale-invariant rahe.
Yeh sab isliye matter karta hai kyunki object detection real-world AI ka backbone hai — self-driving cars, face detection, medical imaging, security cameras, sabme yahi technology use hoti hai. R-CNN accurate hai par slow, jabki YOLO aur SSD jaise single-stage detectors real-time speed dete hain. Toh jab tum yeh evolution samajhte ho — Selective Search se RPN tak, alag-alag CNN calls se shared features tak — tum actually seekh rahe ho ki engineers accuracy aur speed ke beech trade-off kaise optimize karte hain. Yeh intuition tumhe har modern detection system samajhne mein madad karega.