# YOLOv5

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

- Category: Multimodal Generation / Vision & Understanding
- Language: Python
- Licence: AGPL-3.0
- Repository: https://github.com/ultralytics/yolov5
- Website: https://docs.ultralytics.com/yolov5/
- Canonical page: https://ai-tldr.dev/tools/yolov5/
- Updated: 2022-11-22

## In short

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.

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

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
- 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.

1. Clone the repo and install requirements

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

2. 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.

```python
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
```

3. Or run detect.py on files, cameras and streams

   Annotated outputs are written to `runs/detect`.

```bash
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'
```

4. 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.

```bash
python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5s.yaml --batch-size 64
```

5. 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.

```bash
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
```

## When to use it

- 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

- 2022-11-22 v7.0: Added instance-segmentation models (YOLOv5n-seg to YOLOv5x-seg) with segment/train, val and predict scripts.
- 2022-08-17 v6.2: Added image-classification models (YOLOv5-cls) with training, validation and export support.
