Inspect any ONNX model in your browser
← All answersAccuracy & training

How do I train YOLO on my own custom dataset?

Short answer

To train YOLO on a custom dataset: label your images in YOLO format, organise them into train/val folders, write a data.yaml describing the paths and class names, then call model.train() starting from a pretrained checkpoint. Validate and export when the metrics look right.

On this pageRunnable code

1. Label your images

Draw boxes and assign classes using a tool like Label Studio, Roboflow, or CVAT, and export in YOLO format. Each image gets a .txt file with one line per object: class x_center y_center width height, all normalised 0-1.

2. Organise the folders

datasets/mydata/
  images/
    train/
    val/
  labels/
    train/
    val/

Keep each image paired with its matching label file (same stem, .txt extension). Split by source video, camera, or capture session where possible, not by randomly scattering nearly identical adjacent frames across train and validation. This makes validation a more realistic test of generalisation.

3. Write data.yaml

path: ../datasets/mydata
train: images/train
val: images/val
names:
  0: person
  1: forklift

4. Train from pretrained weights

from ultralytics import YOLO

model = YOLO("yolo11n.pt")          # start from pretrained, not scratch
model.train(data="data.yaml", epochs=100, imgsz=640, batch=16)

metrics = model.val()              # check mAP before trusting it
model.export(format="onnx")        # deploy-ready format

Arguments worth knowing

  • imgsz: raise to 1280 for small objects; keep 640 for speed.
  • epochs: 100 is a reasonable start; use early results to decide if more helps.
  • batch: as large as your GPU memory allows; -1 lets Ultralytics auto-pick.
  • model size: yolo11n/s for speed, yolo11m/l/x for accuracy.

If accuracy disappoints after training, the fix is almost always in the data, not the training command.