目录

Ultralytics

构建环境

Ultralytics 镜像

  • GPU
    docker pull ultralytics/ultralytics:latest
    
  • CPU
    docker pull ultralytics/ultralytics:latest-cpu
    
  • Apple Silicon
    docker pull ultralytics/ultralytics:latest-arm64
    

本地安装

pip install ultralytics

基于 COCO128 数据集的目标检测范例

运行容器

git clone https://github.com/ultralytics/ultralytics.git
docker run --runtime=nvidia -it --name ultralytics -v `pwd`/ultralytics:/usr/src/ultralytics ultralytics/ultralytics:latest

yolo 命令的使用参数

yolo TASK MODE ARGS

训练模型

yolo train data=coco128.yaml model=yolov8n.pt

训练可视化(Comet)

pip install comet_ml
export COMET_API_KEY=xxx
yolo train data=coco128.yaml model=yolov8n.pt project=coco128
  • project 指定项目名字,在 Comet 中会自动生成,默认使用的:yolov8

ClearML

  • 安装
    pip install clearml
    
  • 配置
    clearml-init
    
  • 集成
    from clearml import Task
    task = Task.init(project_name="my project", task_name="my task")
    

评估模型

yolo val data=coco128.yaml model=runs/detect/train/weights/best.pt project=coco128

预测

yolo predict data=coco128.yaml model=runs/detect/train/weights/best.pt source=project/images/val/ save=true
  • save=true 保存预测结果(runs/detect/predict)
  • show=true 展示预测结果(UI Window)

对检测出来的结果裁剪分类保存

yolo predict data=project/data.yaml model=runs/detect/train/weights/best.pt source=project/images/val/ save_crop=true
  • classes=0 或 classes=[0,2,5] 过滤指定的类别

导出模型

yolo export model=yolov8s.pt format=onnx

导出的模型格式

Format format Argument Model Metadata
PyTorch   yolov8n.pt
TorchScript torchscript yolov8n.torchscript
ONNX onnx yolov8n.onnx
OpenVINO openvino yolov8n_openvino_model/
TensorRT engine yolov8n.engine
CoreML coreml yolov8n.mlmodel
TF SavedModel saved_model yolov8n_saved_model/
TF GraphDef pb yolov8n.pb
TF Lite tflite yolov8n.tflite
TF Edge TPU edgetpu yolov8n_edgetpu.tflite
TF.js tfjs yolov8n_web_model/
PaddlePaddle paddle yolov8n_paddle_model/

基于 mnist160 数据集的分类

yolo classify train data=mnist160 model=yolov8n-cls.pt epochs=100 imgsz=64
yolo classify val data=mnist160 model=runs/classify/train/weights/best.pt

Benchmark

GPU

yolo benchmark model=yolov8n.pt imgsz=640 half=False device=0
Ultralytics YOLOv8.0.58 🚀 Python-3.10.9 torch-2.0.0 CUDA:0 (Tesla T4, 15110MiB)
Server ✅ (64 CPUs, 251.6 GB RAM, 381.2/548.6 GB disk)

Benchmarks complete for yolov8n.pt on coco128.yaml at imgsz=640 (964.68s)
                   Format Status❔  Size (MB)  metrics/mAP50-95(B)  Inference time (ms/im)
0                 PyTorch       ✅        6.2               0.4478                   13.39
1             TorchScript       ✅       12.4               0.4525                    5.33
2                    ONNX       ✅       12.2               0.4525                   10.97
3                OpenVINO       ❌        0.0                  NaN                     NaN
4                TensorRT       ✅       16.1               0.4525                    4.80
5                  CoreML       ❌        0.0                  NaN                     NaN
6   TensorFlow SavedModel       ✅       30.6               0.4525                   90.53
7     TensorFlow GraphDef       ✅       12.3               0.4525                   80.57
8         TensorFlow Lite       ❌        0.0                  NaN                     NaN
9     TensorFlow Edge TPU       ❌        0.0                  NaN                     NaN
10          TensorFlow.js       ❌        0.0                  NaN                     NaN
11           PaddlePaddle       ✅       24.4               0.4525                  334.54

CPU

yolo benchmark model=yolov8n.pt imgsz=640 half=False device=cpu
Ultralytics YOLOv8.0.58 🚀 Python-3.10.9 torch-2.0.0 CPU
Setup complete ✅ (64 CPUs, 251.6 GB RAM, 383.1/548.6 GB disk)

Benchmarks complete for yolov8n.pt on coco128.yaml at imgsz=640 (647.94s)
                   Format Status❔  Size (MB)  metrics/mAP50-95(B)  Inference time (ms/im)
0                 PyTorch       ✅        6.2               0.4478                  186.26
1             TorchScript       ✅       12.4               0.4525                  231.86
2                    ONNX       ✅       12.2               0.4525                   61.08
3                OpenVINO       ✅       12.3               0.4525                   21.58
4                TensorRT       ❌        0.0                  NaN                     NaN
5                  CoreML       ❎       12.1                  NaN                     NaN
6   TensorFlow SavedModel       ✅       30.6               0.4525                   86.86
7     TensorFlow GraphDef       ✅       12.3               0.4525                   80.34
8         TensorFlow Lite       ✅       12.2               0.4525                  190.41
9     TensorFlow Edge TPU       ❌       40.9                  NaN                     NaN
10          TensorFlow.js       ❌       12.3                  NaN                     NaN
11           PaddlePaddle       ❌        0.0                  NaN                     NaN

工具

标注数据

参考资料