Samuel is an agricultural AI engineer at AgriSense, a startup in Kisumu, Kenya. Farmers photograph diseased crop leaves (wheat, maize, sorghum) and upload to a mobile app. Samuel's task: automatically detect and localize the disease or pest in each image (bounding box), then classify the disease type. Classification alone won't work—the farmer needs to know where on the leaf the disease is. This is object detection. He uses YOLOv8 (You Only Look Once, version 8): trains on 500 labeled field photos, achieves 88% mAP (mean Average Precision), runs inference in <10ms on a phone (with iOS/Android). The economics: KSh 50 per SMS consultation is too expensive; a free automated app using YOLOv8 scales to millions of farmers.
Classification vs object detection: the key difference
Image Classification: "Is there a disease in this image?" Binary or multi-class output. One label per image.
Object Detection: "Where are the diseases? What type?" Multiple bounding boxes (x, y, w, h), confidence score per box, class label per box. One image can have 0, 1, or many objects.
Why it matters: A farmer sees a photo with 3 diseased spots on the same leaf. Classification says "powdery mildew detected." Object detection says "powdery mildew at (100, 200, 50, 40), (200, 150, 60, 45), (180, 300, 55, 50)." The farmer can immediately see which parts to treat with fungicide.
The output: Bounding box = (x_center, y_center, width, height), confidence score ∈ [0,1], and class (e.g., "rust", "leaf_spot"). YOLOv8 predicts all three simultaneously in a single forward pass.
Meet COCO128: the standard detection benchmark
COCO (Common Objects in Context) is the standard object detection dataset. COCO128 is a subset: 128 training images + 128 validation images with bounding box annotations. Objects: 80 classes (person, car, dog, banana, etc.). We'll use COCO128 for demonstrations, but production systems train on full COCO (118K images) or domain-specific datasets (like Samuel's 500 field photos).
YOLO pipeline: from image to detections in one pass
YOLO (You Only Look Once) revolutionized object detection by predicting all bounding boxes and class labels in a single forward pass. Old methods (R-CNN, Fast R-CNN) used region proposals—slow. YOLO is fast: 30 FPS on GPU, 10-50 FPS on mobile.
Pipeline:
1. Input image (e.g., 640×640)
2. Backbone: ResNet or EfficientNet extracts features
3. Neck: FPN (Feature Pyramid Network) combines multi-scale features
4. Head: predicts bounding boxes, confidence, class probabilities on a grid (e.g., 20×20, 40×40, 80×80)
5. NMS (Non-Maximum Suppression): removes overlapping boxes, keeps the best
6. Output: list of (x, y, w, h, confidence, class) per detected object
Speed advantage: Single pass = single forward prop. R-CNN does 2000 region proposals × forward pass each = 2000x slower.
Installing ultralytics: the industry standard YOLOv8 library
Ultralytics is the official YOLOv8 implementation. It's the easiest way to train, infer, and deploy YOLO models. One pip install, few lines of code to train on custom data.
Loading pretrained YOLOv8 and running inference
YOLOv8 is pretrained on COCO (80 classes). Load the model, pass an image, and get detections instantly. No training needed for quick prototyping. Fine-tune on custom data for production accuracy.
Understanding detection results: confidence and IoU
Confidence score: How sure is the model that an object is present? ∈ [0,1]. Threshold with conf=0.5 (default). High threshold = fewer false positives, but misses objects.
Bounding box format (YOLO): (x_center, y_center, width, height) in image coordinates. Convert to (x1, y1, x2, y2) for visualization.
IoU (Intersection over Union): Measures overlap between predicted and ground-truth box. mAP (mean Average Precision) is the standard metric: average precision across all classes.
Class probabilities: 80 softmax outputs (for COCO). Argmax is the predicted class.
YOLOv8 model variants: speed vs accuracy trade-off
YOLOv8 comes in 5 sizes. Nano is 8x faster than Extra but less accurate. Choose based on your constraint.
| Model | Parameters | Speed (GPU) | mAP (COCO) | Use Case |
|---|---|---|---|---|
| YOLOv8n | 3.2M | 0.6ms | 37.3% | Mobile, edge devices |
| YOLOv8s | 11M | 1.4ms | 44.9% | Balanced |
| YOLOv8m | 25M | 2.6ms | 50.2% | Cloud inference |
| YOLOv8l | 43M | 3.3ms | 52.9% | High accuracy |
| YOLOv8x | 68M | 5.0ms | 53.9% | Maximum accuracy |
Fine-tuning on custom data: the agricultural use case
Samuel has 500 labeled crop photos. He'll fine-tune YOLOv8n to detect "rust", "leaf_spot", and "healthy" leaf regions. Training takes <2 hours on a GPU.
How AI systems use this
Every real-time detection system relies on YOLO or similar architectures. Samuel's AgriSense app uses YOLOv8n (fast on phones). Autonomous vehicles use YOLOv8m/l (accuracy critical). Warehouse robots use YOLOv8s (balanced). The key insight: trading off 5% mAP for 8x faster inference unlocks mobile deployment. YOLO's single-pass design is revolutionary—every object detection system deployed in production today uses this paradigm.
Also used in...
Domain Spotlight
Task: Samuel wants to deploy YOLOv8 to iOS/Android phones. Which model should he choose (yolov8n, yolov8s, yolov8m) and why? What's the trade-off?
Hint 1: Mobile phones have limited RAM (2-4GB) and battery. Inference must be <100ms to feel responsive.
Hint 2: YOLOv8n: 3.2M params, 0.6ms on GPU, but 37.3% mAP (may miss small diseases). YOLOv8s: 11M, 1.4ms, 44.9% mAP.
Expected answer: YOLOv8n (or quantized YOLOv8s). Nano fits in <50MB, infers in <50ms on mid-range phone. Sacrifice 7% mAP for 8x faster inference.
Interview questions: test your understanding
Quiz: 5 questions to test your knowledge
Cheatsheet: YOLO object detection at a glance
| Concept | Definition | Example/Range |
|---|---|---|
| Bounding Box | (x_center, y_center, width, height) | Normalized to [0, 1] or pixel coords |
| Confidence | P(object) × IoU(pred, true) | [0, 1]; threshold at 0.5 |
| NMS | Remove overlapping boxes, keep highest conf | IoU threshold: 0.45 (typical) |
| mAP | Mean Average Precision across classes | YOLOv8n: 37.3%, YOLOv8x: 53.9% |
| IoU | Intersection / Union of boxes | [0, 1]; mAP computed at IoU=0.5:0.95 |
Common mistakes: what breaks and how to fix it
results = model.predict(conf=0.5, iou=0.45)Key takeaways
YOLO revolutionized object detection by predicting all boxes in a single forward pass. Samuel's AgriSense app demonstrates real-world impact: automatically detect crop diseases on millions of phones in Kenya, India, Nigeria. The economics work: free automated detection scales better than SMS consultations.
- YOLO innovation: Single forward pass = 30 FPS. R-CNN region proposals = 1 FPS. Game-changer.
- Bounding boxes: (x, y, w, h) + confidence + class label per object. Everything predicted simultaneously.
- NMS: Essential post-processing. Removes duplicate overlapping boxes.
- Model selection: YOLOv8n for mobile (<10ms), YOLOv8m for cloud (accuracy), YOLOv8x for research. Speed-accuracy trade-off.
- Fine-tuning: Pretrained COCO → custom data (e.g., 500 field photos). 88% mAP in <2 hours on GPU.
- Real-world impact: Samuel's app scales to millions of small-holder farmers. Free, automated, accessible.
You've mastered YOLO object detection.
Now build real-time vision systems that scale.
Join the Best AI Program: The AI-Era Agentic Engineer — 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Agentic Engineer →