Ch.15
Object Detection: R-CNN Family vs YOLO (Bounding Boxes)
Object detection at a glance
R-CNN is like opening many candidate crates in a warehouse; YOLO is like every zone manager reporting at the same time.
On the same photo we ask 'where is the dog?' R-CNN first draws many suspect regions (proposal boxes), then checks them one by one. YOLO splits the image into a grid; each cell at once reports where and what. Finally, NMS (Non-Maximum Suppression) — cleanup that removes duplicate boxes on the same object — tidies overlaps, and mAP (mean Average Precision) — the detector report card — scores how well we found objects.
R-CNN: pick candidates, then verify · YOLO: every grid cell at once
First draw many dashed candidate boxes ('something might be here'), then keep one solid box after checking.
R-CNN · 2-stageR-CNNFirst draw many dashed candidate boxes ('something might be here'), then keep one solid box after checking.
YOLOSplit the photo into a grid; each cell reports location and class in one pass.
What happens to one photo
- Start with an RGB photo. Unlike classification, the goal is where each object is, not just what the whole image is.
- A CNN backbone turns the photo into feature maps. Later steps read these features to draw boxes.
- R-CNN proposes many candidate regions that might contain objects. YOLO divides the image into an S×S grid.
- For each candidate or grid cell, predict what it is (class) and where it is (box) together.
- Duplicate cleanup (NMS) keeps one box per object, then report card (mAP) and overlap score (IoU) show how accurate detection was.
Classification asks "Is there a cat in this photo?" Object detection goes one step further: "Where exactly is the cat (X, Y), and how big (W, H)?" — drawing bounding boxes around objects. It powers self-driving, defect inspection, and robot vision.
This chapter meets two ideas that split the field: the careful 2-stage R-CNN family (find suspect regions first, then verify) and lightning-fast 1-stage YOLO (split the image like a grid and scan once). We compare their design and easy-to-read metrics for box quality.
Reading the formulas (object detection)
1. IoU — core formula
- Intersection: overlapping area
- Union: combined area
- Closer to 1 → better match
2. Bounding box coordinates — how do we write the rectangle?
We record where the box around an object sits using one of two common styles:
- Corner style: top-left and bottom-right (x_min, y_min, x_max, y_max) — pin two corners of the rectangle
- Center style: middle (cx, cy) plus width and height (w, h) — center + size
w, h are the box width and height. Area is roughly w×h.
YOLO often scales values to 0~1 relative to the image or grid cell. (e.g. half the image width → 0.5)
3. NMS (Non-Maximum Suppression) — five boxes on one person? Keep one
If a detector draws five boxes around one person, the screen gets messy. NMS is the cleanup step for overlapping boxes.
1. Sort by confidence (highest first).
2. Keep the top box.
3. Drop boxes that overlap it too much (IoU above a threshold). Repeat with what is left.
In one line: one object → one most-confident box.
4. mAP (mean Average Precision) — the detector report card
mAP is an overall score for how well the model finds objects.
- Compute AP (area under the precision–recall curve) per class.
- Average AP across classes → mAP.
- A prediction counts as correct only if it overlaps the ground truth by at least half (IoU ≥ 0.5).
Higher mAP → fewer false boxes and fewer missed objects.
Object Detection: R-CNN vs YOLO
1. Classification vs detection: from 'what' to 'what and where'
Image classification asks "Is there a cat in this room?" Object detection asks "Where is the cat?" You draw boxes around multiple objects and name each one — a richer task.
2. R-CNN family (2-stage): a careful detective
Two steps:
① "Something might be here" — pull out many region proposals (RoI).
② Look at each closely: "It's a cat! The box should be this size!" Very thorough and accurate, but slower because of two stages.
3. YOLO (1-stage): a sharp guard who sees at a glance
YOLO (You Only Look Once) lives up to its name — the image is seen just once. Split it into an S×S grid; each cell shouts "There's a dog in my zone!" and draws a box at the same time. No heavy proposal step, so it is blazing fast for real-time use.
4. Quality tools: IoU, NMS, mAP
- IoU (intersection ÷ union): how much predicted and ground-truth boxes overlap (0~1). Core idea: intersection ÷ union; full formula in the formula guide below.
- NMS (Non-Maximum Suppression): when many boxes pile on the same dog, cleanup keeps the most confident one and removes the rest.
- mAP (mean Average Precision): the detector report card — how well the model finds objects and draws boxes overall.
Why it matters
The 'eyes' of AI that must act in the real world
Self-driving needs exact pedestrian and car positions to brake. Robot arms need to know where to grasp. Detection gives AI spatial understanding beyond one label for the whole image.
Speed or accuracy? Pick the right tool
For CCTV or self-driving where milliseconds matter, use YOLO (1-stage). For tiny lesions in medical scans where accuracy beats speed, R-CNN (2-stage) fits better. Knowing both sides helps you choose.
You can't say 'good' without metrics
Even perfect class names fail if boxes land in the wrong place. IoU and mAP prove objectively whether the model is smart and give targets for improvement.
Stepping stone to segmentation
Bounding boxes are rough rectangles — not pixel-perfect outlines. Master detection and you naturally grow into segmentation, coloring each pixel inside an object.
How it is used
Step 1: Prepare data and box coordinates
Label where each object sits. Usually unify on top-left / bottom-right (x_min, y_min, x_max, y_max) or center + size (cx, cy, w, h).
Step 2: Pick a network for the job
Need real-time streaming? Choose a YOLO-style grid model. Need tiny, crowded objects with high accuracy? Choose Faster R-CNN with an RPN backbone.
Step 3: Train — match boxes and shrink loss
Compare prediction and ground truth with IoU. Train classification error (what object?) and regression error (where and how big?) together.
Step 4: Duplicate cleanup (NMS) and final report card (mAP)
In production, drop low-confidence boxes, run NMS (Non-Maximum Suppression) — cleanup for overlapping boxes — then measure mAP (mean Average Precision) — the report card — on validation data to see how good the model is.
Summary
One-liner: Object detection finds each object's class and box at once — split into careful 2-stage R-CNN and fast 1-stage YOLO.
Key tools: IoU scores overlap, NMS (duplicate cleanup) removes extra boxes, mAP (report card) summarizes overall performance.
Next: move from boxes to segmentation, tracing pixel-level outlines.
Notes for problem solving
Start by reading the question this way
- First sort the axis: classification vs detection / R-CNN 2-stage vs YOLO 1-stage / IoU·NMS·mAP
- For calculations, lock the pattern: YOLO grid S×S → total cells S² (e.g. S=7 → 7×7=49)
- For IoU and union: union = A + B − intersection; overlap 4×4 → area 16
Example (concept)
"Which goal is closest to object detection?"
① One class for the whole image
② Bounding box + class per object
③ Pixel segmentation only
④ Tune learning rate only
Answer 2
Why? Detection finds what + where. Classification gives one label for the entire image.
Example (T/F)
"YOLO always uses 2-stage only"
Answer 0 (false)
Why? YOLO predicts boxes and classes in one 1-stage pass over an S×S grid.
Example (calculation)
"YOLO grid S=7 — how many cells?" → 7×7=49
Examples by problem type + why the answer fits
Example (scenario)
"After inference, five boxes overlap one person. First step?"
① NMS to remove duplicates
② Shuffle labels
③ Zero-layer backbone
④ Drop mAP
Answer 1
Why? Overlapping duplicates are cleaned with NMS.
Example (multiple-choice calc)
"Box A and B each area 32, intersection 16 — union area?"
Answer 48 (32+32-16)
Why? Use union = A + B − intersection.
Example (grid calc)
"YOLO grid S=9 — total cells?"
Answer 81 (9×9)
Why? An S×S grid squares the cells per side.
Example (reasoning)
"CCTV needs real-time inference. Which architecture?"
① Selective Search only
② Original R-CNN only
③ YOLO-like 1-stage
④ Stop augmentation
Answer 3
Why? When speed matters, try 1-stage YOLO first.