This is the official YOLOv5 🚀 notebook by Ultralytics, and is freely available for redistribution under the GPL-3.0 license. For more information please visit https://github.com/ultralytics/yolov5 and https://ultralytics.com. Thank you!
Clone repo, install dependencies and check PyTorch and GPU.
!git clone https://github.com/ultralytics/yolov5 # clone
%cd yolov5
%pip install -qr requirements.txt # install
import torch
import utils
display = utils.notebook_init() # checks
YOLOv5 🚀 v6.1-257-g669f707 Python-3.7.13 torch-1.11.0+cu113 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB)
Setup complete ✅ (8 CPUs, 51.0 GB RAM, 38.8/166.8 GB disk)
detect.py
runs YOLOv5 inference on a variety of sources, downloading models automatically from the latest YOLOv5 release, and saving results to runs/detect
. Example inference sources are:
python detect.py --source 0 # webcam
img.jpg # image
vid.mp4 # video
path/ # directory
path/*.jpg # glob
'https://youtu.be/Zgi9g1ksQHc' # YouTube
'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream
!python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images
display.Image(filename='runs/detect/exp/zidane.jpg', width=600)
detect: weights=['yolov5s.pt'], source=data/images, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False YOLOv5 🚀 v6.1-257-g669f707 Python-3.7.13 torch-1.11.0+cu113 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB) Downloading https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5s.pt to yolov5s.pt... 100% 14.1M/14.1M [00:00<00:00, 225MB/s] Fusing layers... YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients image 1/2 /content/yolov5/data/images/bus.jpg: 640x480 4 persons, 1 bus, Done. (0.013s) image 2/2 /content/yolov5/data/images/zidane.jpg: 384x640 2 persons, 2 ties, Done. (0.015s) Speed: 0.6ms pre-process, 14.1ms inference, 23.9ms NMS per image at shape (1, 3, 640, 640) Results saved to runs/detect/exp
Validate a model's accuracy on COCO val or test-dev datasets. Models are downloaded automatically from the latest YOLOv5 release. To show results by class use the --verbose
flag. Note that pycocotools
metrics may be ~1% better than the equivalent repo metrics, as is visible below, due to slight differences in mAP computation.
Download COCO val 2017 dataset (1GB - 5000 images), and test model accuracy.
# Download COCO val
torch.hub.download_url_to_file('https://ultralytics.com/assets/coco2017val.zip', 'tmp.zip')
!unzip -q tmp.zip -d ../datasets && rm tmp.zip
0%| | 0.00/780M [00:00<?, ?B/s]
# Run YOLOv5x on COCO val
!python val.py --weights yolov5x.pt --data coco.yaml --img 640 --iou 0.65 --half
val: data=/content/yolov5/data/coco.yaml, weights=['yolov5x.pt'], batch_size=32, imgsz=640, conf_thres=0.001, iou_thres=0.65, task=val, device=, workers=8, single_cls=False, augment=False, verbose=False, save_txt=False, save_hybrid=False, save_conf=False, save_json=True, project=runs/val, name=exp, exist_ok=False, half=True, dnn=False YOLOv5 🚀 v6.1-257-g669f707 Python-3.7.13 torch-1.11.0+cu113 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB) Downloading https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5x.pt to yolov5x.pt... 100% 166M/166M [00:04<00:00, 39.4MB/s] Fusing layers... YOLOv5x summary: 444 layers, 86705005 parameters, 0 gradients Downloading https://ultralytics.com/assets/Arial.ttf to /root/.config/Ultralytics/Arial.ttf... 100% 755k/755k [00:00<00:00, 47.9MB/s] val: Scanning '/content/datasets/coco/val2017' images and labels...4952 found, 48 missing, 0 empty, 0 corrupt: 100% 5000/5000 [00:00<00:00, 8742.34it/s] val: New cache created: /content/datasets/coco/val2017.cache Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 157/157 [01:11<00:00, 2.21it/s] all 5000 36335 0.743 0.625 0.683 0.504 Speed: 0.1ms pre-process, 4.9ms inference, 1.2ms NMS per image at shape (32, 3, 640, 640) Evaluating pycocotools mAP... saving runs/val/exp/yolov5x_predictions.json... loading annotations into memory... Done (t=0.42s) creating index... index created! Loading and preparing results... DONE (t=4.91s) creating index... index created! Running per image evaluation... Evaluate annotation type *bbox* DONE (t=77.89s). Accumulating evaluation results... DONE (t=15.36s). Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.506 Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.688 Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.549 Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.340 Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.557 Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.651 Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.382 Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.631 Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.684 Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.528 Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.737 Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.833 Results saved to runs/val/exp
Download COCO test2017 dataset (7GB - 40,000 images), to test model accuracy on test-dev set (20,000 images, no labels). Results are saved to a *.json
file which should be zipped and submitted to the evaluation server at https://competitions.codalab.org/competitions/20794.
# Download COCO test-dev2017
torch.hub.download_url_to_file('https://ultralytics.com/assets/coco2017labels.zip', 'tmp.zip')
!unzip -q tmp.zip -d ../datasets && rm tmp.zip
!f="test2017.zip" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f -d ../datasets/coco/images
# Run YOLOv5x on COCO test
!python val.py --weights yolov5x.pt --data coco.yaml --img 640 --iou 0.65 --half --task test
Train a YOLOv5s model on the COCO128 dataset with --data coco128.yaml
, starting from pretrained --weights yolov5s.pt
, or from randomly initialized --weights '' --cfg yolov5s.yaml
.
automatically from the latest YOLOv5 release
runs/train/
with incrementing run directories, i.e. runs/train/exp2
, runs/train/exp3
etc.Roboflow enables you to easily organize, label, and prepare a high quality dataset with your own custom data. Roboflow also makes it easy to establish an active learning pipeline, collaborate with your team on dataset improvement, and integrate directly into your model building workflow with the roboflow
pip package.
# Tensorboard (optional)
%load_ext tensorboard
%tensorboard --logdir runs/train
# Weights & Biases (optional)
%pip install -q wandb
import wandb
wandb.login()
# Train YOLOv5s on COCO128 for 3 epochs
!python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt --cache
train: weights=yolov5s.pt, cfg=, data=coco128.yaml, hyp=data/hyps/hyp.scratch-low.yaml, epochs=3, batch_size=16, imgsz=640, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, noplots=False, evolve=None, bucket=, cache=ram, image_weights=False, device=, multi_scale=False, single_cls=False, optimizer=SGD, sync_bn=False, workers=8, project=runs/train, name=exp, exist_ok=False, quad=False, cos_lr=False, label_smoothing=0.0, patience=100, freeze=[0], save_period=-1, local_rank=-1, entity=None, upload_dataset=False, bbox_interval=-1, artifact_alias=latest github: up to date with https://github.com/ultralytics/yolov5 ✅ YOLOv5 🚀 v6.1-257-g669f707 Python-3.7.13 torch-1.11.0+cu113 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB) hyperparameters: lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.5, cls_pw=1.0, obj=1.0, obj_pw=1.0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0 Weights & Biases: run 'pip install wandb' to automatically track and visualize YOLOv5 🚀 runs (RECOMMENDED) TensorBoard: Start with 'tensorboard --logdir runs/train', view at http://localhost:6006/ from n params module arguments 0 -1 1 3520 models.common.Conv [3, 32, 6, 2, 2] 1 -1 1 18560 models.common.Conv [32, 64, 3, 2] 2 -1 1 18816 models.common.C3 [64, 64, 1] 3 -1 1 73984 models.common.Conv [64, 128, 3, 2] 4 -1 2 115712 models.common.C3 [128, 128, 2] 5 -1 1 295424 models.common.Conv [128, 256, 3, 2] 6 -1 3 625152 models.common.C3 [256, 256, 3] 7 -1 1 1180672 models.common.Conv [256, 512, 3, 2] 8 -1 1 1182720 models.common.C3 [512, 512, 1] 9 -1 1 656896 models.common.SPPF [512, 512, 5] 10 -1 1 131584 models.common.Conv [512, 256, 1, 1] 11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 12 [-1, 6] 1 0 models.common.Concat [1] 13 -1 1 361984 models.common.C3 [512, 256, 1, False] 14 -1 1 33024 models.common.Conv [256, 128, 1, 1] 15 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 16 [-1, 4] 1 0 models.common.Concat [1] 17 -1 1 90880 models.common.C3 [256, 128, 1, False] 18 -1 1 147712 models.common.Conv [128, 128, 3, 2] 19 [-1, 14] 1 0 models.common.Concat [1] 20 -1 1 296448 models.common.C3 [256, 256, 1, False] 21 -1 1 590336 models.common.Conv [256, 256, 3, 2] 22 [-1, 10] 1 0 models.common.Concat [1] 23 -1 1 1182720 models.common.C3 [512, 512, 1, False] 24 [17, 20, 23] 1 229245 models.yolo.Detect [80, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]] Model summary: 270 layers, 7235389 parameters, 7235389 gradients Transferred 349/349 items from yolov5s.pt AMP: checks passed ✅ Scaled weight_decay = 0.0005 optimizer: SGD with parameter groups 57 weight (no decay), 60 weight, 60 bias albumentations: version 1.0.3 required by YOLOv5, but version 0.1.12 is currently installed train: Scanning '/content/datasets/coco128/labels/train2017.cache' images and labels... 128 found, 0 missing, 2 empty, 0 corrupt: 100% 128/128 [00:00<?, ?it/s] train: Caching images (0.1GB ram): 100% 128/128 [00:00<00:00, 978.19it/s] val: Scanning '/content/datasets/coco128/labels/train2017.cache' images and labels... 128 found, 0 missing, 2 empty, 0 corrupt: 100% 128/128 [00:00<?, ?it/s] val: Caching images (0.1GB ram): 100% 128/128 [00:00<00:00, 207.08it/s] Plotting labels to runs/train/exp/labels.jpg... AutoAnchor: 4.27 anchors/target, 0.994 Best Possible Recall (BPR). Current anchors are a good fit to dataset ✅ Image sizes 640 train, 640 val Using 8 dataloader workers Logging results to runs/train/exp Starting training for 3 epochs... Epoch gpu_mem box obj cls labels img_size 0/2 3.72G 0.04609 0.06258 0.01898 260 640: 100% 8/8 [00:03<00:00, 2.38it/s] Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:00<00:00, 4.45it/s] all 128 929 0.724 0.638 0.718 0.477 Epoch gpu_mem box obj cls labels img_size 1/2 4.57G 0.04466 0.06904 0.01721 210 640: 100% 8/8 [00:00<00:00, 8.21it/s] Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:00<00:00, 4.62it/s] all 128 929 0.732 0.658 0.746 0.488 Epoch gpu_mem box obj cls labels img_size 2/2 4.57G 0.04489 0.06445 0.01634 269 640: 100% 8/8 [00:00<00:00, 9.12it/s] Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:00<00:00, 4.59it/s] all 128 929 0.783 0.652 0.758 0.502 3 epochs completed in 0.003 hours. Optimizer stripped from runs/train/exp/weights/last.pt, 14.9MB Optimizer stripped from runs/train/exp/weights/best.pt, 14.9MB Validating runs/train/exp/weights/best.pt... Fusing layers... Model summary: 213 layers, 7225885 parameters, 0 gradients Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 4/4 [00:03<00:00, 1.27it/s] all 128 929 0.785 0.653 0.761 0.503 person 128 254 0.866 0.71 0.82 0.531 bicycle 128 6 0.764 0.546 0.62 0.375 car 128 46 0.615 0.556 0.565 0.211 motorcycle 128 5 1 0.952 0.995 0.761 airplane 128 6 0.937 1 0.995 0.751 bus 128 7 0.816 0.714 0.723 0.642 train 128 3 0.985 0.667 0.863 0.561 truck 128 12 0.553 0.417 0.481 0.258 boat 128 6 1 0.317 0.418 0.132 traffic light 128 14 0.668 0.287 0.372 0.227 stop sign 128 2 0.789 1 0.995 0.796 bench 128 9 0.691 0.444 0.614 0.265 bird 128 16 0.955 1 0.995 0.666 cat 128 4 0.811 1 0.995 0.797 dog 128 9 1 0.657 0.886 0.637 horse 128 2 0.806 1 0.995 0.647 elephant 128 17 0.955 0.882 0.932 0.691 bear 128 1 0.681 1 0.995 0.895 zebra 128 4 0.87 1 0.995 0.947 giraffe 128 9 0.881 1 0.995 0.734 backpack 128 6 0.926 0.667 0.808 0.359 umbrella 128 18 0.811 0.667 0.864 0.507 handbag 128 19 0.768 0.211 0.352 0.183 tie 128 7 0.778 0.714 0.822 0.495 suitcase 128 4 0.805 1 0.995 0.534 frisbee 128 5 0.697 0.8 0.8 0.74 skis 128 1 0.734 1 0.995 0.4 snowboard 128 7 0.859 0.714 0.852 0.563 sports ball 128 6 0.612 0.667 0.603 0.328 kite 128 10 0.855 0.592 0.624 0.249 baseball bat 128 4 0.403 0.25 0.401 0.171 baseball glove 128 7 0.7 0.429 0.467 0.323 skateboard 128 5 1 0.57 0.862 0.512 tennis racket 128 7 0.753 0.429 0.635 0.327 bottle 128 18 0.59 0.4 0.578 0.293 wine glass 128 16 0.654 1 0.925 0.503 cup 128 36 0.77 0.806 0.845 0.521 fork 128 6 0.988 0.333 0.44 0.312 knife 128 16 0.755 0.579 0.684 0.404 spoon 128 22 0.827 0.436 0.629 0.354 bowl 128 28 0.784 0.648 0.753 0.528 banana 128 1 0.802 1 0.995 0.108 sandwich 128 2 1 0 0.606 0.545 orange 128 4 0.921 1 0.995 0.691 broccoli 128 11 0.379 0.455 0.468 0.338 carrot 128 24 0.777 0.542 0.73 0.503 hot dog 128 2 0.562 1 0.828 0.712 pizza 128 5 0.802 0.814 0.962 0.694 donut 128 14 0.694 1 0.981 0.848 cake 128 4 0.864 1 0.995 0.858 chair 128 35 0.636 0.648 0.628 0.319 couch 128 6 1 0.606 0.857 0.555 potted plant 128 14 0.739 0.786 0.837 0.476 bed 128 3 1 0 0.806 0.568 dining table 128 13 0.862 0.483 0.602 0.405 toilet 128 2 0.941 1 0.995 0.846 tv 128 2 0.677 1 0.995 0.796 laptop 128 3 1 0 0.83 0.532 mouse 128 2 1 0 0.0931 0.0466 remote 128 8 1 0.612 0.659 0.534 cell phone 128 8 0.645 0.25 0.437 0.227 microwave 128 3 0.797 1 0.995 0.734 oven 128 5 0.435 0.4 0.44 0.29 sink 128 6 0.345 0.167 0.301 0.211 refrigerator 128 5 0.645 0.8 0.804 0.545 book 128 29 0.603 0.207 0.301 0.171 clock 128 9 0.785 0.889 0.888 0.734 vase 128 2 0.477 1 0.995 0.92 scissors 128 1 1 0 0.995 0.199 teddy bear 128 21 0.862 0.667 0.823 0.549 toothbrush 128 5 0.809 1 0.995 0.65 Results saved to runs/train/exp
Weights & Biases (W&B) is now integrated with YOLOv5 for real-time visualization and cloud logging of training runs. This allows for better run comparison and introspection, as well improved visibility and collaboration for teams. To enable W&B pip install wandb
, and then train normally (you will be guided through setup on first use).
During training you will see live updates at https://wandb.ai/home, and you can create and share detailed Reports of your results. For more information see the YOLOv5 Weights & Biases Tutorial.
All results are logged by default to runs/train
, with a new experiment directory created for each new training as runs/train/exp2
, runs/train/exp3
, etc. View train and val jpgs to see mosaics, labels, predictions and augmentation effects. Note an Ultralytics Mosaic Dataloader is used for training (shown below), which combines 4 images into 1 mosaic during training.
train_batch0.jpg
shows train batch 0 mosaics and labels
test_batch0_labels.jpg
shows val batch 0 labels
test_batch0_pred.jpg
shows val batch 0 predictions
Training results are automatically logged to Tensorboard and CSV as results.csv
, which is plotted as results.png
(below) after training completes. You can also plot any results.csv
file manually:
from utils.plots import plot_results
plot_results('path/to/results.csv') # plot 'results.csv' as 'results.png'
YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.
Optional extras below. Unit tests validate repo functionality and should be run on any PRs submitted.
# Reproduce
for x in 'yolov5n', 'yolov5s', 'yolov5m', 'yolov5l', 'yolov5x':
!python val.py --weights {x}.pt --data coco.yaml --img 640 --task speed # speed
!python val.py --weights {x}.pt --data coco.yaml --img 640 --conf 0.001 --iou 0.65 # mAP
# PyTorch Hub
import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
# Images
dir = 'https://ultralytics.com/images/'
imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')] # batch of images
# Inference
results = model(imgs)
results.print() # or .show(), .save()
# CI Checks
%%shell
export PYTHONPATH="$PWD" # to run *.py. files in subdirectories
rm -rf runs # remove runs/
for m in yolov5n; do # models
python train.py --img 64 --batch 32 --weights $m.pt --epochs 1 --device 0 # train pretrained
python train.py --img 64 --batch 32 --weights '' --cfg $m.yaml --epochs 1 --device 0 # train scratch
for d in 0 cpu; do # devices
python val.py --weights $m.pt --device $d # val official
python val.py --weights runs/train/exp/weights/best.pt --device $d # val custom
python detect.py --weights $m.pt --device $d # detect official
python detect.py --weights runs/train/exp/weights/best.pt --device $d # detect custom
done
python hubconf.py # hub
python models/yolo.py --cfg $m.yaml # build PyTorch model
python models/tf.py --weights $m.pt # build TensorFlow model
python export.py --img 64 --batch 1 --weights $m.pt --include torchscript onnx # export
done
# Profile
from utils.torch_utils import profile
m1 = lambda x: x * torch.sigmoid(x)
m2 = torch.nn.SiLU()
results = profile(input=torch.randn(16, 3, 640, 640), ops=[m1, m2], n=100)
# Evolve
!python train.py --img 640 --batch 64 --epochs 100 --data coco128.yaml --weights yolov5s.pt --cache --noautoanchor --evolve
!d=runs/train/evolve && cp evolve.* $d && zip -r evolve.zip $d && gsutil mv evolve.zip gs://bucket # upload results (optional)
# VOC
for b, m in zip([64, 64, 64, 32, 16], ['yolov5n', 'yolov5s', 'yolov5m', 'yolov5l', 'yolov5x']): # batch, model
!python train.py --batch {b} --weights {m}.pt --data VOC.yaml --epochs 50 --img 512 --hyp hyp.VOC.yaml --project VOC --name {m} --cache
# TensorRT
# https://docs.nvidia.com/deeplearning/tensorrt/install-guide/index.html#installing-pip
!pip install -U nvidia-tensorrt --index-url https://pypi.ngc.nvidia.com # install
!python export.py --weights yolov5s.pt --include engine --imgsz 640 640 --device 0 # export
!python detect.py --weights yolov5s.engine --imgsz 640 640 --device 0 # inference