█

AI/TLDR

Buy me a coffee
Buy me a coffee

YOLOv5

The script-driven PyTorch repo behind the YOLOv5 detection, segmentation and classification models

Vision & UnderstandingOpen source
Latest
v7.0
Updated
22 Nov 2022
Language
Python
License
AGPL-3.0
$git clone https://github.com/ultralytics/yolov5

What's new

v7.022 Nov 2022

Added instance-segmentation models (YOLOv5n-seg to YOLOv5x-seg) with segment/train, val and predict scripts.

IN SHORT

What is YOLOv5?

YOLOv5 is Ultralytics' PyTorch implementation of the YOLOv5 family of real-time vision models, published as a self-contained repository you clone and run. It covers three tasks: object detection, instance segmentation (added in release v7.0) and image classification (added in release v6.2). It sits in the Vision & Understanding category of the Multimodal Generation tools and is open-source, written in Python and licensed AGPL-3.0.

Updated Markdown version ↗

Overview

YOLOv5 is Ultralytics' PyTorch implementation of the YOLOv5 family of real-time vision models, published as a self-contained repository you clone and run. It covers three tasks: object detection, instance segmentation (added in release v7.0) and image classification (added in release v6.2). Pretrained checkpoints come in five sizes — nano, small, medium, large and extra-large (yolov5n through yolov5x) — plus larger-input P6 variants, and are downloaded automatically from the repo's GitHub releases the first time you use them.

YOLOv5 instance-segmentation output on a photo of two men in suits: each person is covered by a red mask with a bounding box labelled person 0.90 and person 0.89, and a tie is masked in green with the label tie 0.73
Instance segmentation from a YOLOv5-seg model: every detection gets a box, a class label with its confidence, and a pixel mask.YOLOv5 README ↗

It is a different thing from the `ultralytics` pip package that the same company maintains. YOLOv5 is driven by standalone scripts inside the cloned repo — `train.py`, `val.py`, `detect.py` and `export.py`, with train, val and predict counterparts under `segment/` and `classify/` — and by PyTorch Hub (`torch.hub.load("ultralytics/yolov5", ...)`). The `ultralytics` package is the maintainers' unified Python and `yolo` CLI home for newer Ultralytics YOLO architectures and adds tasks such as pose estimation and oriented bounding boxes. The YOLOv5 README itself points people who want the later YOLO generations or that unified interface to the package, and describes YOLOv5 as a mature, production-proven model for detection, segmentation and classification.

In practice people reach for this repo when an existing pipeline, paper or deployment is already built on YOLOv5 checkpoints and scripts, or when they want the plain, readable training scripts to modify. The repo also documents workflows around the models: training on custom data, multi-GPU training, test-time augmentation, model ensembling, pruning, hyperparameter evolution, transfer learning with frozen layers, and exporting to deployment formats such as ONNX, TensorRT, CoreML and TFLite. It is dual-licensed: AGPL-3.0 for open-source use, with an Ultralytics Enterprise License for commercial products that cannot meet the AGPL's terms.

What it does

  • Detection, instance-segmentation and classification models in five sizes (n/s/m/l/x), with P6 variants trained at 1280-pixel input
Line chart of COCO AP val against GPU speed in milliseconds per image for YOLOv5n6, s6, m6, l6 and x6, with EfficientDet plotted as a grey comparison line further right and lower
The README's speed-versus-accuracy chart: the five YOLOv5 P6 sizes trade GPU latency for COCO AP, plotted against EfficientDet.YOLOv5 README ↗
  • Standalone `train.py`, `val.py`, `detect.py` and `export.py` scripts, with `train.py` / `val.py` / `predict.py` counterparts under `segment/` and `classify/`
  • One-line PyTorch Hub loading, with results you can print, show, save, crop or turn into a pandas table
  • `detect.py` accepts webcams, images, videos, directories, glob patterns, screen capture, YouTube URLs and RTSP/RTMP/HTTP streams
  • Export to deployment formats including ONNX, TensorRT, CoreML and TFLite through `export.py`
  • Multi-GPU DDP training, AutoBatch (`--batch-size -1`), hyperparameter evolution and test-time augmentation built into the scripts

Getting started

YOLOv5 runs from a clone of the repository in a Python 3.8+ environment with PyTorch 1.8 or newer. Models and example datasets download automatically on first use.

Clone the repo and install requirements

bashbash
git clone https://github.com/ultralytics/yolov5
cd yolov5
pip install -r requirements.txt

Run inference through PyTorch Hub

`torch.hub.load` fetches the model code and the checkpoint. The input can be a URL, a local file, a PIL image, an OpenCV frame, a numpy array or a list of them.

pythonpython
import torch

# options: yolov5n, yolov5s, yolov5m, yolov5l, yolov5x
model = torch.hub.load("ultralytics/yolov5", "yolov5s")

results = model("https://ultralytics.com/images/zidane.jpg")
results.print()  # print results to console
results.save()   # save to runs/detect/exp

Or run detect.py on files, cameras and streams

Annotated outputs are written to `runs/detect`.

bashbash
python detect.py --weights yolov5s.pt --source 0              # webcam
python detect.py --weights yolov5s.pt --source img.jpg        # image
python detect.py --weights yolov5s.pt --source vid.mp4        # video
python detect.py --weights yolov5s.pt --source path/to/images/
python detect.py --weights yolov5s.pt --source 'rtsp://example.com/media.mp4'

Train a model

This is the README's command for training YOLOv5s from scratch on COCO. For your own dataset, follow the Train Custom Data tutorial in the YOLOv5 docs and point `--data` at your dataset YAML. `--batch-size -1` lets AutoBatch pick the batch size for your GPU.

bashbash
python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5s.yaml --batch-size 64
Grid of ten training plots over 300 epochs for YOLOv5s, m, l and x: GIoU, objectness and classification losses falling on train and validation sets, and precision, recall, mAP@0.5 and mAP@0.5:0.95 rising
Training curves for four YOLOv5 sizes on COCO: losses fall and mAP climbs over 300 epochs, with the larger models ending higher.YOLOv5 README ↗

Segment, classify and export

The segmentation and classification models use the same script pattern from their own folders; `export.py` converts any of them for deployment.

bashbash
python segment/predict.py --weights yolov5m-seg.pt --source data/images/bus.jpg
python classify/predict.py --weights yolov5s-cls.pt --source data/images/bus.jpg

python export.py --weights yolov5s-seg.pt --include onnx engine --img 640 --device 0

Commands and code are distilled from the project's own documentation — always check the official repo for the latest.

When to use it

Segmentation output on a street photo: a green bus is masked and boxed as bus 0.88 while several pedestrians are masked in red and boxed as person with confidences between 0.73 and 0.92
segment/predict.py on the repo's bundled bus.jpg sample: the bus and each pedestrian get their own mask.YOLOv5 README ↗
  • Reach for it when an existing system, paper or edge deployment is built on YOLOv5 checkpoints and you need to retrain, reproduce or re-export them
  • Reach for it when you want a real-time detector whose training and inference code are short, plain scripts you can read and edit
  • Reach for it to fine-tune a small detector or segmenter on a custom dataset and ship it as ONNX or TensorRT
  • Reach for the `ultralytics` package instead when you want newer YOLO architectures, pose or OBB tasks, or a single `yolo` CLI

Version history

Every verified update to YOLOv5 that AI/TLDR tracked, newest first — each links to our coverage and the official changeset.

  1. 2022-11-22v7.0

    Added instance-segmentation models (YOLOv5n-seg to YOLOv5x-seg) with segment/train, val and predict scripts.

  2. 2022-08-17v6.2

    Added image-classification models (YOLOv5-cls) with training, validation and export support.

How YOLOv5 compares

YOLOv5 alongside other open-source vision & understanding tools AI/TLDR tracks, ranked by GitHub stars.

ToolStarsWhat it does
PaddleOCR★ 92.1kA toolkit for detecting and recognizing text in images across many languages, plus document parsing.
OpenCV★ 91.4kThe long-standing open-source computer-vision library for C++, Python and Java; version 5 rewrote the DNN engine and added ONNX-based LLM and VLM inference in-library.
Ultralytics YOLO★ 63kA framework for training and running YOLO models for real-time object detection, segmentation, and tracking.
YOLOv5★ 58.2kThe script-driven PyTorch repo behind the YOLOv5 detection, segmentation and classification models
Supervision★ 51.5kA Python toolkit for processing, annotating, and visualizing detections and segmentations from many vision models.
MediaPipe★ 37.5kGoogle's on-device ML framework: ready-to-run vision, text and audio tasks with one cross-platform API for Android, iOS, web, desktop and edge.
MMDetection★ 33kAn OpenMMLab toolbox with many object detection and instance segmentation algorithms for research and production.
vit-pytorch★ 25.6kA single pip package with readable PyTorch implementations of the Vision Transformer and dozens of its research variants, for training image models from scratch.