YOLOv5: PyTorch Object Detection & Segmentation
Ultralytics YOLOv5 is a PyTorch model for object detection, instance segmentation, and image classification, with pretrained weights and a train.py/detect.py workflow that still works well. Reach for it if you want a stable, documented training loop you can run today. Skip it for a new project — Ultralytics' own README now points newcomers to the actively maintained `ultralytics` package instead.
Understanding Ultralytics YOLOv5
Ultralytics YOLOv5 is an open-source computer vision model built on PyTorch that detects, segments, and classifies objects using one codebase and one set of training scripts. It ships five model sizes — YOLOv5n, YOLOv5s, YOLOv5m, YOLOv5l, and YOLOv5x — trading accuracy for inference speed, plus P6 variants (yolov5n6 through yolov5x6) tuned for larger 1280px inputs.
Core Capabilities of YOLOv5
- ✓Five detection model sizes (n/s/m/l/x) plus P6 variants for 1280px inputs, so you pick a speed/accuracy trade-off without redesigning the architecture.
- ✓Pretrained COCO checkpoints auto-download from the latest GitHub release the first time you reference a weights name.
- ✓detect.py accepts almost any source — webcam, local image or video, a directory, a glob pattern, a .txt list of paths or stream URLs, a YouTube link, or an RTSP/RTMP/HTTP stream.
- ✓PyTorch Hub loading (`torch.hub.load("ultralytics/yolov5", "yolov5s")`) returns a results object with `.print()`, `.show()`, `.save()`, `.crop()`, and `.pandas()` methods.
- ✓Instance segmentation models, added in release v7.0, run through the same train/val/export scripts as the detection models.
- ✓Export tutorial covers ONNX, TensorRT, CoreML, and TFLite in one place.
- ✓AutoBatch (`--batch-size -1`) picks a batch size to fit your GPU memory instead of you guessing one.
- ✓Logged integrations with Weights & Biases, Comet ML, ClearML, and Roboflow for tracking training runs, plus DeepSparse for faster inference.
Getting Started with YOLOv5
Clone the repo and install dependencies into a Python 3.8+ environment that already has PyTorch 1.8+: ```bash git clone https://github.com/ultralytics/yolov5 cd yolov5 pip install -r requirements.txt ``` There's no PyPI package for yolov5 itself. You work directly out of the cloned repo, which is also why weights and datasets download automatically into it the first time you run detect.py or train.py.
Running Inference and Training
For a one-off inference call, load a checkpoint through PyTorch Hub and run it directly on an image, URL, or numpy array: ```python import torch model = torch.hub.load("ultralytics/yolov5", "yolov5s") results = model("https://ultralytics.com/images/zidane.jpg") results.print() ``` For batch inference from the command line, `detect.py` takes almost any source: ```bash python detect.py --weights yolov5s.pt --source img.jpg ``` Training on COCO (or your own `data.yaml`) goes through `train.py`, where `--batch-size -1` triggers AutoBatch instead of you picking a number by hand: ```bash python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5s.yaml --batch-size 64 ``` The README notes YOLOv5n through YOLOv5x take roughly one to eight days to train for 300 epochs on a single V100 GPU — multi-GPU training cuts that down.
Strengths
- ✓detect.py's source handling is unusually broad — the same script takes a webcam, a directory, a glob, a text file of paths, a YouTube URL, or an RTSP stream, so you're not writing separate ingestion code per source type.
- ✓Pretrained COCO checkpoints and datasets download automatically from GitHub releases the first time you reference them — no separate download step.
- ✓Five model sizes plus P6 variants mean you swap `yolov5n` for `yolov5x` in a config flag rather than redesigning the network for a different speed/accuracy point.
- ✓The export tutorial covers ONNX, TensorRT, CoreML, and TFLite in one place, so getting a trained model onto a phone or an edge device isn't a separate research project.
- ✓Built-in integrations with Weights & Biases, Comet ML, and ClearML mean experiment tracking without wiring a logger yourself.
When to Consider Other Models
- △YOLOv5 is now in maintenance mode — Ultralytics' own README steers new users toward the actively maintained `ultralytics` package for newer architectures, pose estimation, and oriented bounding boxes, none of which YOLOv5 has.
- △AGPL-3.0 is a copyleft license; shipping YOLOv5 inside a closed-source product means either open-sourcing your code or buying an Enterprise License from Ultralytics.
- △Full-size training runs are GPU-heavy — the README's own numbers put YOLOv5x at multiple days on a single V100, so you need real hardware or a cloud GPU to reproduce the published benchmarks.
- △There's no pip-installable `yolov5` package; you work out of a cloned repo rather than an importable library, which is friction if you wanted it as a dependency in an existing project.
Exploring Other Ultralytics YOLO Models
Frequently Asked Questions
YOLOv5 is released under the AGPL-3.0 license, a strong copyleft license. Ultralytics also offers a separate Enterprise License for teams that need different terms.
YOLOv5 can be used in commercial projects, but its AGPL-3.0 license requires you to open-source code you distribute alongside it — teams that need to stay closed-source use Ultralytics' Enterprise License instead.
YOLOv5 handles object detection, instance segmentation, and image classification, each trainable and exportable through the same set of scripts (train.py, detect.py, val.py, export.py).
Point train.py at a data.yaml describing your classes and image paths, plus a model config like yolov5s.yaml, then run `python train.py --data data.yaml --weights yolov5s.pt`. The README's Train Custom Data tutorial covers labeling format in detail.
YOLOv5's README describes it as mature and production-proven rather than a target for new features — Ultralytics directs active development toward the newer `ultralytics` package, which adds pose estimation and oriented bounding boxes that YOLOv5 doesn't have.
YOLOv5's export tutorial covers ONNX, TensorRT, CoreML, and TFLite, letting you move a trained checkpoint from a training GPU to a mobile device, an edge box, or a server-side inference runtime.
The problem it solves
Training a decent object detector from scratch means writing a data loader, an augmentation pipeline, an anchor-matching loss, and an export path to whatever runtime your deployment target needs — getting all four right takes weeks. YOLOv5 packages that whole pipeline (train.py, detect.py, val.py, export.py) around pretrained COCO weights, so a developer with a labeled dataset in YOLO format can go from zero to a trained, exported model without building the surrounding tooling themselves.
Best use cases
- •Real-time object detection on a webcam or RTSP camera feed via detect.py, no custom capture code needed.
- •Training a custom detector on a labeled dataset by pointing train.py at a data.yaml and one of the yolov5*.yaml configs.
- •Instance segmentation on COCO-style data using the segment/ scripts introduced in v7.0.
- •Exporting a trained model to ONNX or TensorRT for deployment on an NVIDIA Jetson device.
- •Quick prototyping via PyTorch Hub inside a notebook, without cloning the repo's full detect.py pipeline.
Who should try it — and who should skip
Reach for YOLOv5 if you have a labeled dataset in YOLO format and want a training/export pipeline that's already been through years of production use, with tutorials for the exact problem you're hitting. Skip it if you're starting a brand-new project with no existing YOLOv5 code to maintain — Ultralytics' own README sends new users to the `ultralytics` package instead, and if you need pose estimation or oriented bounding boxes, YOLOv5 simply doesn't have them.
Related repositories
Curious whether yolov5 is right for you?
Let ChatGPT, Claude, or Perplexity look into it — click below and see what AI actually says about yolov5.
