Train Your First Model
One Python call, sensible defaults, and a checkpoint at the end.
Now we train. Thanks to transfer learning from the pretrained weights, there's a real chance your first run beats the pretrained baseline by a wide margin — your dataset is narrower, the classes are familiar, and the Ultralytics YOLO default configuration is well-tuned. Don't overthink the config the first time. Run, look at the curves, then iterate.
Train a model on your dataset, end-to-end, and save the best.pt checkpoint.
model = YOLO('yolo26n.pt').model.train(data='my_dataset/data.yaml', epochs=100, imgsz=640).Watch the loss curves in the console.
Open
runs/detect/train/. Your weights are inweights/best.pt.
Hands-on
A first training run

from ultralytics import YOLO
model = YOLO("yolo26n.pt") # start from a pretrained checkpoint
results = model.train(
data="my_dataset/data.yaml",
epochs=100,
imgsz=640,
batch=16,
name="forklift_v1",
)
# Path to the best checkpoint
print(results.save_dir)That writes a directory like runs/detect/forklift_v1/ with:
weights/best.pt— best validation mAP across epochs.weights/last.pt— last epoch.results.csv— per-epoch metrics.- A bunch of plots — confusion matrix, PR curves, F1 curves, sample predictions.
Open the directory in a file explorer and scroll the plots before running anything else.
The arguments that matter
Defaults are sane. Three knobs you'll touch first:
| Arg | Default | When to change |
|---|---|---|
epochs | 100 | Lower for tiny datasets to avoid overfitting; higher (200–300) for large ones |
imgsz | 640 | Increase to 1024 / 1280 for small objects |
batch | 16 | Batch size — match GPU memory; -1 picks the largest that fits |
model.train(data="my_dataset/data.yaml", epochs=200, imgsz=1024, batch=-1)Always pass yolo26n.pt (or another size) as the model — never train from scratch unless you're researching the backbone. Pretrained weights cut training time by 5–10× and reach much higher final accuracy on most datasets.
Reading the live log
The console log per epoch looks like this:
Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size
1/100 2.43G 1.234 1.456 0.987 42 640: 100% ...
Class Images Instances Box(P R mAP50 mAP50-95)
all 50 102 0.612 0.534 0.561 0.342What to watch:
box_loss,cls_loss,dfl_losstrend down — these are the components of YOLO's loss function and should fall steadily for the first many epochs. A loss that plateaus immediately means data or config trouble.P(precision) /R(recall) trend up — and balance. Precision >> recall means the model is too cautious; recall >> precision means it's hallucinating.mAP50andmAP50-95trend up — the actual quality numbers; the performance metrics guide explains them in depth.
If anything looks off in the first 5 epochs, kill the run with Ctrl-C and investigate before burning hours.
Multi-GPU
model.train(data=..., device=[0, 1, 2, 3]) # use 4 GPUsUltralytics YOLO uses DDP automatically and trains in mixed precision by default. Effective batch size = batch × number of GPUs. When you're ready to push past sensible defaults, the hyperparameter tuning guide covers learning rate sweeps, augmentation knobs, and the genetic optimizer.
CLI version
The same training, on the command line:
yolo detect train data=my_dataset/data.yaml model=yolo26n.pt epochs=100 imgsz=640 name=forklift_v1CLI and Python are equivalent — pick whichever fits your workflow.
Run model.train(data=…, epochs=100). While it trains, scan the console output every few epochs. After it finishes, open runs/detect/.../results.png and look at the curves. Note where mAP plateaus.
git add -A && git commit -m "feat(model): trained first forklift detector"Training completed without crashing.
runs/detect/<name>/weights/best.ptexists.You've looked at the loss/mAP curves and have an opinion on whether they look healthy.
Show solution
from ultralytics import YOLO
model = YOLO("yolo26n.pt")
model.train(
data="my_dataset/data.yaml",
epochs=100,
imgsz=640,
batch=-1, # auto-batch
name="forklift_v1",
patience=20, # early-stop if mAP doesn't improve for 20 epochs
)We have a model. Next: validate it honestly and figure out what to fix.