packaged ultralytics/yolov5
pip install yolov5
Overview
Install
Install yolov5 using pip (for Python >=3.7)
pip install yolov5
Install yolov5 using pip `(for Python 3.6)`
pip install "numpy>=1.18.5,<1.20" "matplotlib>=3.2.2,<4"
pip install yolov5
Use from Python
Basic
import yolov5
model = yolov5.load('yolov5s')
img = 'https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg'
results = model(img)
results = model(img, size=1280)
results = model(img, augment=True)
predictions = results.pred[0]
boxes = predictions[:, :4]
scores = predictions[:, 4]
categories = predictions[:, 5]
results.show()
results.save(save_dir='results/')
Alternative
from yolov5 import YOLOv5
model_path = "yolov5/weights/yolov5s.pt"
device = "cuda:0"
yolov5 = YOLOv5(model_path, device)
image1 = 'https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg'
image2 = 'https://github.com/ultralytics/yolov5/blob/master/data/images/bus.jpg'
results = yolov5.predict(image1)
results = yolov5.predict(image1, size=1280)
results = yolov5.predict(image1, augment=True)
results = yolov5.predict([image1, image2], size=1280, augment=True)
predictions = results.pred[0]
boxes = predictions[:, :4]
scores = predictions[:, 4]
categories = predictions[:, 5]
results.show()
results.save(save_dir='results/')
Train/Detect/Test/Export
- You can directly use these functions by importing them:
from yolov5 import train, val, detect, export
train.run(imgsz=640, data='coco128.yaml')
val.run(imgsz=640, data='coco128.yaml', weights='yolov5s.pt')
detect.run(imgsz=640)
export.run(imgsz=640, weights='yolov5s.pt')
- You can pass any argument as input:
from yolov5 import detect
img_url = 'https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg'
detect.run(source=img_url, weights="yolov5s6.pt", conf_thres=0.25, imgsz=640)
Use from CLI
You can call yolov5 train
, yolov5 detect
, yolov5 val
and yolov5 export
commands after installing the package via pip
:
Training
Finetune one of the pretrained YOLOv5 models using your custom data.yaml
:
$ yolov5 train --data data.yaml --weights yolov5s.pt --batch-size 16 --img 640
yolov5m.pt 8
yolov5l.pt 4
yolov5x.pt 2
Visualize your experiments via Neptune.AI:
$ yolov5 train --data data.yaml --weights yolov5s.pt --neptune_project NAMESPACE/PROJECT_NAME --neptune_token YOUR_NEPTUNE_TOKEN
Inference
yolov5 detect command runs inference on a variety of sources, downloading models automatically from the latest YOLOv5 release and saving results to runs/detect
.
$ yolov5 detect --source 0
file.jpg
file.mp4
path/
path/*.jpg
rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa
rtmp://192.168.1.105/live/test
http://112.50.243.8/PLTV/88888888/224/3221225900/1.m3u8
Export
You can export your fine-tuned YOLOv5 weights to any format such as torchscript
, onnx
, coreml
, pb
, tflite
, tfjs
:
$ yolov5 export --weights yolov5s.pt --include 'torchscript,onnx,coreml,pb,tfjs'