Ultralytics YOLO Foundations: Part 3

Custom Data & Training

2026-03-13

4. Training & ML Fundamentals

Teaching the model your custom data.

Key Training Concepts

Before we dive in, let’s lock down the terminology:

  • Model: The mathematical structure with learned weights (e.g., yolo26n.pt).
  • Epoch: One complete pass through your entire training dataset.
  • mAP (Mean Average Precision): The ultimate grading score (0-100%). It grades how well the model finds the object AND correctly identifies it!

Train vs. Validation

To train a robust AI, split your data:

  • Train Set (~80%): Data the model actively learns from.
  • Validation Set (~20%): Unseen data evaluated during training to measure performance (mAP).

Crucial: Never train on validation data! This causes a Data Leak (model memorizes answers but fails in the real world).

Split your data into train and validation sets using Ultralytics Data Split or using thier Platform

Transfer Learning

  • .pt (Pretrained weights): Starts with general features (edges, shapes) from large datasets. Fast training, needs little data. (Always recommended!)
  • .yaml (Architecture Definition): Blank blueprint. Random weights, meaning you train entirely from scratch.

Use a .pt model for all custom dataset training to leverage Transfer Learning.

Understanding the Data Setup

Different formats for different tasks:

  • Classification: Relies entirely on folder structure (e.g., train/cat/, train/dog/). No label files needed!
  • Detection & Segmentation: Uses standard YOLO format, pairing images with .txt label files.
my_dataset/
├── images/
│   ├── train/
│   └── val/
└── labels/
    ├── train/
    └── val/

Inside the Labels

  • Object Detection (.txt Bounding Box): <class_index> <x_center> <y_center> <width> <height>

  • Instance Segmentation (.txt Polygon): <class_index> <x1> <y1> <x2> <y2> ... <xn> <yn>

  • Pose Estimation (.txt Keypoints): <class_index> <x_center> <y_center> <width> <height> <px1> <py1> <p1_vis> ...

⚠ Note: Coordinates must always be normalized (0.0 to 1.0) to keep annotations independent of image resolution!

The data.yaml File

Linking indices to names.

# data.yaml
path: ../my_dataset
train: images/train
val: images/val

names:
  0: my_custom_object
  1: another_object

5. Custom Data Labeling

The Ultralytics Platform

Labeling Your Own Data

Typing coordinates manually is impossible. Use Ultralytics HUB, Roboflow, or CVAT.

  1. Upload your images.
  2. Draw bounding boxes or polygons visually.
  3. Export in YOLO format for a ready-to-train dataset structure!

Docs Reference: Ultralytics HUB

AutoLabeling with SAM

Speed up the labeling process using foundational models:

  1. Zero-Shot Labeling: Bypass manual labeling using SAM (Segment Anything Model) or FastSAM.
  2. Run the model on your raw dataset to automatically generate masks and bounding boxes.
  3. These models understand objects generically without needing custom training!
  4. Simply review and correct the auto-generated labels—drastically faster than drawing every box or polygon manually!

Docs Reference: Segment Anything Models (SAM)

6. Kickoff Training

Hyperparameters and Configs

Training

We can start training directly from the command line or Python API:

yolo train model=yolo26n.pt data=data.yaml epochs=100 imgsz=640 batch=-1
# Python Equivalent
from ultralytics import YOLO

model = YOLO("yolo26n.pt")
results = model.train(data="data.yaml", epochs=100, imgsz=640, batch=-1)

Resuming Training

If your training is interrupted (e.g., power outage or Colab timeout), you can easily resume it from the last saved weights without losing progress!

# Resume training from the last saved checkpoint
yolo train resume model=path/to/runs/train/exp/weights/last.pt
# Python Equivalent
from ultralytics import YOLO

# Load the last checkpoint
model = YOLO("path/to/runs/train/exp/weights/last.pt")

# Resume training
results = model.train(resume=True)

Deep Dive into Hyperparameters

  • epochs (Default: 100): Pass through the ENTIRE dataset. More = learns more, but risks overfitting (memorizing data).
  • patience (Default: 50): If metrics don’t improve for this many epochs, training stops early to save time!
  • batch (Default: 16): Images processed at once.
    • Higher = faster, stable, takes more GPU memory.
    • Smaller = adds noise, longer training.
    • Pro-tip: Use batch: -1 for AutoBatch to maximize your GPU memory without crashing!

Hyperparameters: Optimizer

  • optimizer (Default: ‘auto’): Algorithm guiding learning (SGD, AdamW, etc.). auto works 99% of the time.
  • lr0 (Default: 0.01): Initial Learning Rate (step size).
    • Too high: Loss explodes, model diverges.
    • Too small: Converges slowly, might get stuck.

Hyperparameters: Image Size and Hardware

  • imgsz (Default: 640): Target image size. Larger sizes (e.g., 1024) capture more detail (great for tiny objects!) but use more RAM and are slower.
    • 💡 Tip for Tiny Objects: Need to detect tiny objects in huge 4K images? Check out SAHI (Slicing Aided Hyper Inference)! It slices large images into overlapping tiles, runs YOLO on each, and merges the results automatically.
  • device (Default: ’’): Specify GPUs (e.g., device=0,1) to trigger multi-GPU training!

Using Config Files (Best Practice)

For reproducibility, put arguments into a experiment.yaml file:

# experiment.yaml
task: detect
mode: train
model: yolo26s.pt
data: data.yaml    
epochs: 100
batch: 16
imgsz: 640

Run with:

yolo cfg=experiment.yaml

Using Config Files

# Python Equivalent
from ultralytics import YOLO

model = YOLO("yolo26s.pt")
model.train(cfg="experiment.yaml")

Experiment Tracking

Ultralytics seamlessly integrates with popular MLOps tools to log metrics (mAP, loss) and matrices (confusion matrix) during training.

Supported Integrations:

  • TensorBoard (Built-in)
  • Weights & Biases (W&B)
  • Comet
  • ClearML
  • MLflow
# Enable TensorBoard logging
yolo settings tensorboard=True
# In terminal
tensorboard --logdir runs/train
# or in colab
%load_ext tensorboard
%tensorboard --logdir path/to/runs

Docs Reference: Integrations

Data Augmentation (Python)

YOLO natively supports Albumentations to improve model robustness, applying default augmentations automatically if installed.

pip install albumentations ultralytics
import albumentations as A
from ultralytics import YOLO

model = YOLO("yolo26n.pt")

# Define custom Albumentations transforms
custom_transforms = [
    A.Blur(blur_limit=7, p=0.5),
    A.CLAHE(clip_limit=4.0, p=0.5),
    A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.5),
]

# Train the model with custom transforms
model.train(data="data.yaml", epochs=100, augmentations=custom_transforms)

Docs Reference: Albumentations Integration

Conclusion

Wrapping Up Custom Training

Summary

  • Data Setup: Formatting datasets correctly for YOLO tasks and avoiding data leaks.
  • Labeling tools: Using Ultralytics HUB and auto-labeling with SAM.
  • Training: Initiating and resuming training runs efficiently.
  • Hyperparameters: Understanding epochs, batch sizes, optimizers, and tracking experiments.

Next Steps

Next up: Evaluation & Deployment

  • How to measure your model’s real-world accuracy (mAP, Precision, Recall).
  • Deploying your trained model to run blazingly fast in production.

Q&A

Thank You!

Any questions?