🌾
Real-world scenario
Samuel at AgriSense (Kisumu, Kenya): detect crop diseases and pests in field photos using object detection

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.

500
Field photos
88%
mAP (YOLOv8)
<10ms
Inference time

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).

COCO128 sample images. Objects are labeled with bounding boxes and class names. YOLOv8 learns to predict bounding boxes and classes for unseen images.
Grid of COCO128 sample images with bounding boxes: person, car, dog, backpack, sports ball, etc.

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.

YOLO architecture. Input → Backbone (feature extraction) → Neck (multi-scale fusion) → Head (box + class prediction) → NMS (post-processing) → Output detections.
YOLO pipeline diagram showing backbone (ResNet), neck (FPN), and head producing bounding boxes at multiple scales

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.

install_yolo.py Install ultralytics and dependencies
Output
PyTorch version: 2.0.1 GPU available: True GPU device: NVIDIA A100 Model loaded: Model parameters: 3,161,216

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.

yolo_inference.py Run YOLOv8 inference on an image
Output
Detected 3 objects: - person: conf=0.95, bbox=(100.5, 50.2, 300.8, 500.1) - car: conf=0.87, bbox=(350.0, 200.3, 600.5, 450.2) - dog: conf=0.92, bbox=(650.1, 150.5, 750.9, 400.2) Annotated image saved to output.jpg

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
YOLOv8 speed vs accuracy. Nano is 8x faster but 16% less accurate than Extra. Choose based on latency requirements.
Scatter plot showing YOLOv8 model variants from nano (fast, low accuracy) to extra (slow, high 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.

finetune_custom.py Fine-tune YOLOv8 on custom agricultural data
Output
Training on custom dataset (500 images, 3 classes)... Epoch 1/50: loss=1.23, mAP=0.45 Epoch 10/50: loss=0.52, mAP=0.78 Epoch 30/50: loss=0.21, mAP=0.88 Epoch 50/50: loss=0.15, mAP=0.88 Best mAP: 0.88 Model saved: runs/detect/train/weights/best.pt

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.

Bounding Box Regression Multi-Scale Detection Non-Maximum Suppression Real-Time Inference

Also used in...

Domain Spotlight

🌾
Agriculture: Crop Disease Detection
Samuel's use case. Detect rust, leaf spots, pests in crop photos. Mobile app scales to millions of small-holder farmers in Kenya, India, Nigeria.
🚗
Autonomous Driving: Obstacle Detection
Detect pedestrians, cyclists, cars, traffic signs. YOLOv8l/x for high accuracy. Runs at 30 FPS on edge GPUs in vehicles.
🏭
Manufacturing: Quality Control
Detect defects in assembled products. YOLOv8n on factory cameras. Real-time <10ms inference, integrate with conveyor belts.
🎯 Mini Challenge: Deploy to mobile

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

YOLO = You Only Look Once. Main innovation: single forward pass predicts all bounding boxes and class labels. Old methods (R-CNN) ran 2000 region proposal networks sequentially—slow. YOLO is 30 FPS vs R-CNN's 1 FPS.
mAP = mean Average Precision. Measures detection accuracy across all classes and IoU thresholds (0.5 to 0.95). Higher mAP = better detections. YOLOv8n: 37.3% mAP, YOLOv8x: 53.9% mAP on COCO. Standard metric for comparing models.
NMS removes overlapping bounding boxes, keeping the one with highest confidence. Problem: YOLO predicts 10,647 boxes per image (20x20x27 anchors = multi-scale), most are noise. NMS filters to ~100 high-confidence boxes, reducing false positives significantly.
YOLOv8n: 3.2M parameters, 0.6ms inference, fits in 12MB. YOLOv8x: 68M parameters, 5ms inference, 200MB. Mobile phones have <100MB for the entire app. Nano inference + display + OS = feasible. Extra would drain battery in seconds. Trade 16% mAP for 8x faster inference.
Use stratified train/val split (400/100) or cross-validation. Track mAP on validation set across epochs. Collect separate test set from different farms/fields (distribution shift). If val mAP > test mAP by >5%, model overfits. Use regularization (dropout, augmentation, early stopping) to improve generalization.

Quiz: 5 questions to test your knowledge

Question 1 of 5
What is the main advantage of YOLO over R-CNN?
Question 2 of 5
How many parameters does YOLOv8n have?
Question 3 of 5
COCO dataset has how many object classes?
Question 4 of 5
What does mAP stand for?
Question 5 of 5
For mobile deployment (limited battery, <100MB), which YOLOv8 model is best?
0
/5
Excellent! You understand YOLO and object detection.

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

Mistake 1: Not using NMS, getting duplicate detections
YOLO predicts 10K+ boxes. NMS filters to ~100. If you skip NMS, you'll see 10 overlapping boxes for one object. Always apply NMS post-inference: results = model.predict(conf=0.5, iou=0.45)
Mistake 2: Fine-tuning on too few images (50 images)
Need minimum 100-200 images per class to avoid overfitting. Samuel used 500 (good for 3 classes). Use augmentation (rotation, flip, brightness) to artificially expand dataset.
Mistake 3: Using YOLOv8x for mobile (too large, too slow)
Mobile needs YOLOv8n or quantized YOLOv8s. YOLOv8x is 200MB+ and infers in 5ms—drains battery, doesn't fit. Trade 16% mAP for 8x faster inference.
Mistake 4: Ignoring class imbalance in training data
If 400/500 images are "healthy" and 50 are "rust", model learns to predict "healthy" (high accuracy, low recall on rust). Use weighted loss or augment "rust" images to balance.

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.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →