Ultralytics YOLOv5 🚀 is a cutting-edge, state-of-the-art (SOTA) computer vision model developed by Ultralytics. Built on the PyTorch framework, YOLOv5 is known for its ease of use, speed, and accuracy. It incorporates insights and best practices from extensive research and development, making it a popular choice for various visual AI tasks.
Project Address: https://github.com/ultralytics/yolov5
YOLOv5 supports multiple computer vision tasks:
Key features include real-time inference, support for various training techniques such as test-time augmentation (TTA) and model ensembling, and compatibility with export formats like TFLite, ONNX, CoreML, and TensorRT.
YOLOv5 offers various model sizes to meet different needs:
Model | Parameters (M) | Speed | Accuracy | Use Cases |
---|---|---|---|---|
YOLOv5n | 1.9 | Fastest | Lower | Mobile devices, edge computing |
YOLOv5s | 7.2 | Fast | Medium | General applications |
YOLOv5m | 21.2 | Medium | Higher | Balanced performance |
YOLOv5l | 46.5 | Slower | High | High-precision requirements |
YOLOv5x | 86.7 | Slowest | Highest | Highest-precision requirements |
Performance on the COCO dataset:
# Clone the repository
git clone https://github.com/ultralytics/yolov5
cd yolov5
# Install dependencies
pip install -r requirements.txt
import torch
# Load the YOLOv5 model
model = torch.hub.load("ultralytics/yolov5", "yolov5s")
# Define the input image source
img = "https://ultralytics.com/images/zidane.jpg"
# Perform inference
results = model(img)
# Process the results
results.print() # Print results to the console
results.show() # Display results in a window
results.save() # Save results to runs/detect/exp
# Inference using a webcam
python detect.py --weights yolov5s.pt --source 0
# Inference on a local image file
python detect.py --weights yolov5s.pt --source img.jpg
# Inference on a video file
python detect.py --weights yolov5s.pt --source vid.mp4
# Inference on a directory of images
python detect.py --weights yolov5s.pt --source path/to/images/
# Inference on a YouTube video
python detect.py --weights yolov5s.pt --source 'https://youtu.be/LNwODJXcvt4'
# Train YOLOv5s on the COCO dataset for 300 epochs
python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5s.yaml --batch-size 64
# Train YOLOv5m
python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5m.yaml --batch-size 40
# Train YOLOv5l
python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5l.yaml --batch-size 24
# Use multi-GPU distributed training
python -m torch.distributed.run --nproc_per_node 4 --master_port 1 train.py --data coco.yaml --weights '' --cfg yolov5s.yaml --batch-size 64 --device 0,1,2,3
YOLOv5 v7.0 introduced instance segmentation functionality, achieving state-of-the-art performance on the COCO dataset.
Model | mAP^box | mAP^mask | Parameters (M) | Speed (ms) |
---|---|---|---|---|
YOLOv5n-seg | 27.6 | 23.4 | 2.0 | 59.2 |
YOLOv5s-seg | 37.6 | 32.0 | 7.6 | 63.7 |
YOLOv5m-seg | 45.0 | 37.1 | 22.0 | 89.5 |
YOLOv5l-seg | 49.0 | 39.9 | 47.8 | 168.4 |
YOLOv5x-seg | 50.7 | 41.4 | 88.8 | 265.7 |
# Train a segmentation model
python segment/train.py --data coco128-seg.yaml --weights yolov5s-seg.pt --img 640
# Validate a segmentation model
python segment/val.py --weights yolov5s-seg.pt --data coco.yaml --img 640
# Use a segmentation model for prediction
python segment/predict.py --weights yolov5m-seg.pt --source data/images/bus.jpg
YOLOv5 v6.2 added image classification functionality, with results from training on the ImageNet dataset for 90 epochs:
Model | Top1 Accuracy | Top5 Accuracy | Parameters (M) | Speed (ms) |
---|---|---|---|---|
YOLOv5s-cls | 71.5% | 90.2% | 5.5 | 1.7 |
YOLOv5m-cls | 75.9% | 92.9% | 13.0 | 2.8 |
YOLOv5l-cls | 78.0% | 94.0% | 27.4 | 4.9 |
YOLOv5x-cls | 79.0% | 94.4% | 55.4 | 8.7 |
# Train on the CIFAR-100 dataset
python classify/train.py --model yolov5s-cls.pt --data cifar100 --epochs 5 --img 224 --batch 128
# Validate the ImageNet model
python classify/val.py --weights yolov5m-cls.pt --data ../datasets/imagenet --img 224
# Image classification prediction
python classify/predict.py --weights yolov5s-cls.pt --source data/images/bus.jpg
YOLOv5 supports various deployment formats:
# Export to ONNX format
python export.py --weights yolov5s.pt --include onnx
# Export to TensorRT format
python export.py --weights yolov5s.pt --include engine
# Export to CoreML format (for iOS)
python export.py --weights yolov5s.pt --include coreml
# Export to TFLite format (for mobile devices)
python export.py --weights yolov5s.pt --include tflite
Improve prediction accuracy through reflection and scale augmentation:
python val.py --data coco.yaml --img 1536 --iou 0.7 --augment
Combine multiple models for better performance:
python val.py --data coco.yaml --weights yolov5s.pt yolov5m.pt yolov5l.pt yolov5x.pt
Automatically find the best training hyperparameters:
python train.py --data coco.yaml --evolve 300
YOLOv5 is deeply integrated with several mainstream AI platforms:
YOLOv5, as a significant milestone in the field of object detection, has become the preferred framework for academic research and industrial applications due to its excellent performance, ease of use, and rich functionality. Whether you are a beginner or a professional developer, you can find a suitable solution in the YOLOv5 ecosystem.