Intermediate level · Module 6: Object Detection with YOLO · Lesson 2 of 3
Training a YOLO Detector on a Custom Dataset
Learners fine-tune a small pretrained Ultralytics YOLO model on their labelled data, on a CPU with small images or on a free notebook GPU, and use it to detect objects in new images.
In this lesson you will
- Prepare a YOLO dataset configuration
- Fine-tune and run a YOLO detector
Key concepts
data.yaml (paths and class names)pretrained nano modelmodel.train(epochs, imgsz, device)model.predicttraining outputs and curves
The lesson
Ultralytics expects images and label files in train and val folders and a data.yaml file listing the paths and class names. Starting from a small pretrained model ('n' or nano size) and fine-tuning it on your classes needs far less data and time than training from scratch.
from ultralytics import YOLO; model = YOLO(<pretrained nano weights>); model.train(data='data.yaml', epochs=30, imgsz=416, device='cpu'). On a CPU, use small image sizes and few epochs for learning; a free notebook GPU allows larger settings. Training saves curves, metrics and the best weights.
model.predict('image.jpg', conf=0.25) returns detections with boxes, classes and scores, and can save annotated images. Always check predictions visually on images the model has not seen.
Step by step
- Organise the labelled images into train and val folders and write data.yaml.
- Fine-tune a nano model on CPU with imgsz=416 for a few epochs.
- Inspect the training curves and results files.
- Run predictions on ten new images and display them.
Practice activity
Fine-tune a YOLO detector for the level project and demonstrate predictions on new images.
Expected result: A trained detector, its training curves and annotated predictions on unseen images.
Check your understanding
-
What does data.yaml contain?
Show answer
Dataset paths and class names.
-
Why start from a pretrained model?
Show answer
It needs much less data and training time than training from scratch.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.