Ultralytics YOLO
Ultralytics YOLO is a cutting-edge deep learning library for state-of-the-art computer vision tasks including object detection, instance segmentation, pose estimation, and image classification, built around the YOLO architecture. It maintains a rapid release cadence, frequently delivering stability improvements and new features, with the current stable version being 8.4.36.
Warnings
- breaking The v8 release introduced a complete API overhaul. The primary interaction shifted from direct script execution or specific task imports (e.g., `detect.py`) to a unified `YOLO` class interface. Code written for v7 or earlier versions is not directly compatible.
- gotcha Training resume functionality has historically been prone to issues (e.g., not restoring optimizer state, incorrect argument loading). While recent patch releases (v8.4.29-v8.4.36) include numerous fixes, users should verify resume behavior, especially after major dependency updates or unexpected interruptions.
- gotcha Optimal performance requires a correctly configured GPU environment (CUDA, cuDNN, PyTorch with CUDA support). Without it, Ultralytics will silently fall back to CPU, leading to significantly slower training and inference.
- gotcha Data preparation (dataset YAMLs, annotation formats) is crucial. Incorrect paths, missing files, or malformed annotation files (especially COCO JSON or YOLO TXT) are common sources of errors during training or validation.
- gotcha Training can occasionally encounter 'NaN' losses, especially with aggressive learning rates, mixed precision (FP16), or problematic data. Recent versions include improvements for NaN recovery but it remains a potential issue.
Install
-
pip install ultralytics -
pip install ultralytics[all]
Imports
- YOLO
from ultralytics import YOLO
Quickstart
from ultralytics import YOLO
import os
# Load a pretrained YOLOv8n model
model = YOLO('yolov8n.pt')
# Use the model for training (example with dummy data for brevity)
# For a real run, ensure 'coco128.yaml' or your custom data path is valid
# You might need to download a dataset like coco128 first.
# For a quick local test, you can uncomment and try to train on a tiny dataset:
# try:
# results = model.train(data='coco128.yaml', epochs=1, imgsz=640)
# except Exception as e:
# print(f"Training failed (might be missing dataset or GPU): {e}")
# Use the model for prediction on an image
image_path = 'https://ultralytics.com/images/bus.jpg'
results = model(image_path)
# Process results
for r in results:
print(f"Detected {len(r.boxes)} objects.")
# r.show() # Uncomment to display the image with detections
# Export the model to ONNX format
model.export(format='onnx')