YOLO (You Only Look Once)

Overview

  • YOLO stands for You Only Look Once
  • Created for fast computer vision tasks
  • Ultralytics created YOLO v5, v8, and v11
  • Real-time object detection framework

YOLO Capabilities

YOLO can handle 5 types of tasks:

Task Code Description
Detection detect Bounding boxes around objects
Instance Segmentation seg Pixel-level object masks
Pose/Keypoints pose Human pose estimation
Oriented Detection obb Rotated bounding boxes
Classification cls Image classification

YOLO v11 Model Variants

Model Parameters (M) Size (MB) Speed T4 GPU (ms) FLOPS (B) Best For
YOLOv11n 2.6 5.35 1.5 6.5 Edge devices, mobile
YOLOv11s 9.4 18.4 2.5 21.5 Balanced speed/accuracy
YOLOv11m 20.1 38.8 4.7 68 General purpose
YOLOv11l 25.3 49 6.2 86.9 High accuracy
YOLOv11x 56.9 109 11.3 194.9 Maximum accuracy

Choosing a model: - Nano (n) - Fast inference, lower accuracy (mobile apps, edge devices) - Small (s) - Good balance (most common choice) - Medium (m) - Better accuracy, slower - Large (l) - High accuracy applications - Extra Large (x) - Best accuracy, slowest

Understanding Model Weights

Why does model size stay the same after training?

# Before training
yolov11n.pt  # 5.35 MB - pretrained on COCO dataset

# After training
best.pt      # 5.35 MB - trained on your custom dataset

Explanation: - YOLOv11n has a fixed architecture with ~2.6 million parameters - Both files store the same number of weights - Only the values of those weights change (not the structure) - Same format, different data

Think of it like two Excel files with the same template but different data - file size stays similar

Training YOLO Models

Basic Training Command

yolo task=detect mode=train \
  data=data.yaml \
  model=yolov11s.pt \
  epochs=100 \
  batch=16 \
  imgsz=640 \
  device=0 \
  project=my_detection \
  name=run1

Core Training Parameters

Parameter Value Description
epochs 100 Number of complete passes through dataset
batch 16 Images processed together (helps pattern recognition)
imgsz 640 Input image size (640×640 pixels)
val True Enable validation during training
patience 15 Stop if no improvement for 15 epochs
device 0 GPU device (0 for first GPU, 'cpu' for CPU)
cache True Cache images in RAM for faster training
plots True Generate training visualization plots

Data Augmentation Parameters

Data augmentation creates variations of training images to improve model generalization.

# Augmentation settings
auto_augment = 'randaugment'  # Smart automatic transformations
mosaic = 1.0                  # Combine 4 images (better small object detection)
translate = 0.1               # Random position shift (±10%)
scale = 0.5                   # Random zoom in/out
fliplr = 0.5                  # 50% chance horizontal flip
erasing = 0.4                 # Random cutout (improves robustness)

Augmentation Examples: - Mosaic: Combines 4 training images into one (helps detect small objects) - Translate: Randomly shifts objects within image - Scale: Zooms in/out on objects - Flip: Mirrors image horizontally - Erasing: Randomly removes image patches

Learning Parameters

lr0 = 0.01              # Initial learning rate (how fast model learns)
momentum = 0.937        # Smooths learning over time
weight_decay = 0.0005   # Prevents overfitting
warmup_epochs = 3.0     # Gradual learning rate increase (first 3 epochs)

Using Ultralytics Package

Installation

pip install ultralytics

Basic Usage

from ultralytics import YOLO

# Load pretrained model
model = YOLO("yolov11n.pt")

# Run inference
results = model.predict(source="image.png", save=True, conf=0.5)

# Process results
for result in results:
    # Detection boxes (x1, y1, x2, y2)
    boxes = result.boxes.xyxy

    # Segmentation masks (for seg models)
    masks = result.masks.xy

    # Keypoints (for pose models)
    keypoints = result.keypoints

    # Class names
    names = result.names

Available Modes

Mode Command Description
Train train Train model on custom data
Predict predict Run inference on images/videos
Validate val Evaluate model performance
Export export Export to ONNX, TensorRT, etc.
Track track Multi-object tracking
Benchmark benchmark Speed/accuracy benchmarking

Training on Custom Data

from ultralytics import YOLO

# Load a model
model = YOLO("yolov11n.pt")

# Train the model
results = model.train(
    data="data.yaml",
    epochs=100,
    imgsz=640,
    batch=16,
    device=0
)

# Validate
metrics = model.val()

# Predict
results = model.predict("test_image.jpg")

Data Annotation

Label Studio Setup

# Install
pip install label-studio

# Start annotation tool
label-studio start

Auto-Annotation for Segmentation

Segmentation annotation is time-consuming (drawing polygons around objects). Use auto-annotation:

from ultralytics.data.annotator import auto_annotate

# Auto-annotate using detection + SAM model
auto_annotate(
    data="path/to/images",
    det_model="yolov11n.pt",          # Detection model
    sam_model="sam_b.pt"              # Segment Anything Model
)

How it works: 1. Detection model finds objects (bounding boxes) 2. SAM model creates precise polygons around each detected object 3. Much faster than manual polygon annotation

Workflow: 1. Create detection labels in Label Studio (quick bounding boxes) 2. Export detection labels 3. Use auto_annotate with detection model + SAM 4. Get segmentation labels automatically

Data Format

YOLO Dataset Structure

dataset/
├── images/
│   ├── train/
│   │   ├── img1.jpg
│   │   └── img2.jpg
│   └── val/
│       ├── img3.jpg
│       └── img4.jpg
├── labels/
│   ├── train/
│   │   ├── img1.txt
│   │   └── img2.txt
│   └── val/
│       ├── img3.txt
│       └── img4.txt
└── data.yaml

data.yaml Configuration

# Dataset paths
path: /path/to/dataset
train: images/train
val: images/val

# Classes
names:
  0: person
  1: car
  2: bicycle

# Number of classes
nc: 3

Label Format (detection)

Each line in .txt file:

class_id center_x center_y width height

Example:

0 0.5 0.5 0.3 0.4
1 0.2 0.3 0.1 0.15

All values are normalized (0-1 range)

Common Use Cases

Application Model Type Example
License Plate Detection Detection Traffic monitoring
Helmet Detection Detection Safety compliance
ID Card Detection Detection Document processing
Person Counting Detection Crowd monitoring
Face Mask Detection Detection COVID compliance
Pose Estimation Pose Fitness apps, sports analysis

Tips & Best Practices

  1. Start small: Begin with YOLOv11n or s, only use larger models if needed
  2. Data quality > quantity: 100 well-labeled images better than 1000 poor ones
  3. Use augmentation: Especially with small datasets (<500 images)
  4. Monitor training: Watch for overfitting (val loss increases while train loss decreases)
  5. Test different image sizes: 640 is default, but 1280 can improve accuracy
  6. Use pretrained weights: Transfer learning is faster than training from scratch

Quick Reference Commands

# Train
yolo detect train data=data.yaml model=yolov11n.pt epochs=100

# Predict single image
yolo detect predict model=best.pt source=image.jpg

# Predict video
yolo detect predict model=best.pt source=video.mp4

# Validate model
yolo detect val model=best.pt data=data.yaml

# Export to ONNX
yolo export model=best.pt format=onnx