Home
Login

YOLOv5 is a state-of-the-art, real-time object detection model based on PyTorch, supporting object detection, image segmentation, and image classification tasks.

AGPL-3.0Python 54.3kultralyticsyolov5 Last Updated: 2025-06-21

YOLOv5 Project Detailed Introduction

Project Overview

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

Core Features

Multi-Task Support

YOLOv5 supports multiple computer vision tasks:

  • Object Detection: Identifying and locating multiple objects in an image.
  • Instance Segmentation: Pixel-level object segmentation.
  • Image Classification: Classifying single images into categories.

Technical Advantages

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.

Model Architecture and Performance

Model Specifications

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 Metrics

Performance on the COCO dataset:

  • YOLOv5s: mAP@0.5:0.95 = 37.4%, Speed 6.4ms (V100)
  • YOLOv5m: mAP@0.5:0.95 = 45.4%, Speed 8.2ms (V100)
  • YOLOv5l: mAP@0.5:0.95 = 49.0%, Speed 10.1ms (V100)
  • YOLOv5x: mAP@0.5:0.95 = 50.7%, Speed 12.1ms (V100)

Installation and Quick Start

Environment Requirements

  • Python >= 3.8.0
  • PyTorch >= 1.8

Installation Steps

# Clone the repository
git clone https://github.com/ultralytics/yolov5
cd yolov5

# Install dependencies
pip install -r requirements.txt

Inference with PyTorch Hub

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

Command-Line Inference

# 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'

Training Custom Models

Training Command Examples

# 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

Multi-GPU Training

# 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

Instance Segmentation Functionality

YOLOv5 v7.0 introduced instance segmentation functionality, achieving state-of-the-art performance on the COCO dataset.

Segmentation Model Performance

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

Segmentation Training Example

# 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

Image Classification Functionality

YOLOv5 v6.2 added image classification functionality, with results from training on the ImageNet dataset for 90 epochs:

Classification Model Performance

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

Classification Training Example

# 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

Model Export and Deployment

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

Advanced Features

Test Time Augmentation (TTA)

Improve prediction accuracy through reflection and scale augmentation:

python val.py --data coco.yaml --img 1536 --iou 0.7 --augment

Model Ensembling

Combine multiple models for better performance:

python val.py --data coco.yaml --weights yolov5s.pt yolov5m.pt yolov5l.pt yolov5x.pt

Hyperparameter Evolution

Automatically find the best training hyperparameters:

python train.py --data coco.yaml --evolve 300

Integration and Ecosystem

YOLOv5 is deeply integrated with several mainstream AI platforms:

  • Weights & Biases: Experiment tracking and visualization
  • Comet ML: Experiment management and monitoring
  • Roboflow: Data annotation and management
  • Intel OpenVINO: Model optimization and deployment
  • Neural Magic: Model sparsification and acceleration
  • Ultralytics HUB: One-stop training and deployment platform

Community and Support

Summary

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.

Star History Chart