So far the models classify a whole image. Real applications need to know what is present and where—the object detection task from Module 7.2. Early detectors ran a classifier over thousands of region proposals (slow). YOLO (“You Only Look Once,” Redmon et al., 2016) reframed detection as a single regression over a grid—fast enough for real-time video.
Learning Objectives
By the end of this lesson, students should be able to:
- Explain YOLO’s single-shot, grid-based detection formulation.
- Describe bounding-box regression, objectness, and class prediction per cell.
- Explain the role of non-maximum suppression (NMS) and IoU.
- Contrast one-stage (YOLO) vs two-stage (R-CNN) detectors.
- Run inference with a modern YOLO via the
ultralyticspackage. - Interpret detection metrics like mAP.
YOLO divides the input image into an S×S grid. Each cell predicts a fixed number of bounding boxes (x, y, w, h), an objectness score (probability a box contains an object), and class probabilities—all in a single forward pass. This “look once” design makes it a one-stage detector, trading a little accuracy for large speed gains.
How Single-Shot Detection Works
A CNN backbone (often a ResNet-style or custom CSPDarknet network) produces a feature grid. A detection head outputs, per cell, box coordinates + objectness + class scores. Because everything is one pass, YOLO reaches 30–150+ FPS. Overlapping duplicate boxes are then removed with non-maximum suppression using Intersection-over-Union (IoU).
| Aspect | Two-stage (Faster R-CNN) | One-stage (YOLO) |
|---|---|---|
| Region proposals | Separate proposal network | None—dense grid |
| Speed | Slower (~5–15 FPS) | Real-time (30–150+ FPS) |
| Accuracy | Often higher on small objects | Very competitive, improving |
| Best use | Offline, high-precision | Video, robotics, edge, live |
Running YOLO in PyTorch (Ultralytics)
Modern YOLO models ship in the ultralytics package, built on PyTorch. Inference and training are a few lines.
Reality: YOLO looks at the whole image once. It predicts every box in parallel from a single feature grid—that global context is why it makes fewer background false positives than sliding-window detectors, and why it is fast.
Raw YOLO output contains many overlapping boxes for the same object. Without non-maximum suppression you get duplicate detections; with too high a confidence threshold you miss real objects. Tune conf and iou thresholds per application rather than leaving defaults blindly.
Knowledge Check
- Short Answer: What does YOLO stand for? Answer: You Only Look Once.
- True/False: YOLO is a two-stage detector. Answer: False—it is one-stage.
- Multiple Choice: Each grid cell predicts: (a) only a class, (b) boxes + objectness + class, (c) only pixels, (d) a caption. Answer: (b).
- Short Answer: What technique removes duplicate overlapping boxes? Answer: Non-maximum suppression (NMS).
- True/False: YOLO can run in real time on video. Answer: True.
- Multiple Choice: IoU measures: (a) learning rate, (b) box overlap, (c) channel count, (d) FLOPs. Answer: (b).
- Short Answer: Name one advantage of one-stage over two-stage detectors. Answer: Much faster / real-time inference.
- True/False: Modern YOLO models are distributed via the ultralytics PyTorch package. Answer: True.
- Multiple Choice: The standard detection accuracy metric is: (a) BLEU, (b) mAP, (c) perplexity, (d) F0.5. Answer: (b).
- Short Answer: Which task from Module 7.2 does YOLO implement? Answer: Object detection.
Key Takeaways
- YOLO reframes detection as one regression over a grid—a single forward pass.
- Each cell predicts boxes, objectness, and class probabilities.
- NMS with IoU cleans up overlapping predictions.
- One-stage design trades slight accuracy for real-time speed.
- Next: Mask R-CNN adds pixel-level masks to detection.
Demo: Run yolov8n on a webcam feed live in class—students immediately grasp “real-time” detection.
Exercise: Have students sweep the confidence and IoU thresholds on one image and observe precision/recall trade-offs directly.