安装
通过git源码方式安装
> conda create -n YOLO python=3.10
> conda activate YOLO
> git clone https://github.com/ultralytics/ultralytics
> cd ultralytics
# Install the package in editable mode for development
> pip install -e .
基本设置
- 查看配置
> yolo settings
💡 Learn about settings at https://docs.ultralytics.com/quickstart/#ultralytics-settings
Printing '/root/.config/Ultralytics/settings.yaml'
settings_version: 0.0.4
datasets_dir: /root/datasets
weights_dir: weights
runs_dir: runs
uuid: xxxxx
sync: true
api_key: ''
clearml: true
comet: true
dvc: true
hub: true
mlflow: true
neptune: true
raytune: true
tensorboard: true
wandb: true
- 修改配置
# Update a setting
yolo settings runs_dir='/path/to/runs'
# Update multiple settings
yolo settings runs_dir='/path/to/runs' tensorboard=False
# Reset settings to default values
yolo settings reset
- 可视化配置 tensorboard
python -m pip install tensorboard
clearml
python -m pip install clearml
clearml-init
yolov8中使用了挺多类似的编码技巧(比如tensorboard、clearml和albumentations库),如果你没有安装这个库的,相关配置就不会生效。具体实现是通过try/exception来实现,类似以下的方式
try:
# WARNING: do not move import due to protobuf issue in https://github.com/ultralytics/ultralytics/pull/4674
from torch.utils.tensorboard import SummaryWriter
assert not TESTS_RUNNING # do not log pytest
assert SETTINGS['tensorboard'] is True # verify integration is enabled
WRITER = None # TensorBoard SummaryWriter instance
except (ImportError, AssertionError, TypeError):
# TypeError for handling 'Descriptors cannot not be created directly.' protobuf errors in Windows
SummaryWriter = None
训练
- 命令行方式
# Start training from a pretrained *.pt model
> yolo detect train data=coco128.yaml model=yolov8n.pt epochs=100 imgsz=640
-
batch:默认16
-
代码方式
from ultralytics import YOLO
from pathlib import Path
# 加载模型
model = YOLO(Path("./model/yolov8x.pt"))
data_path = "/mnt/workspace/yolo/datasets/xxxx/data.yaml"
# Train the model
model.train(data=data_path, epochs=300, imgsz=1280,batch=4,resume=False)
coco.yaml格式如下:
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/coco128 # dataset root dir
train: images/train2017 # train images (relative to 'path') 128 images
val: images/train2017 # val images (relative to 'path') 128 images
test: # test images (optional)
# Classes
names:
# Download script/URL (optional)
download: https://ultralytics.com/assets/coco128.zip
模型
COCO数据集上的结果
可以使用YOLOv8m,相比n和s mAP提升比较多(6个点),但是相比较8l和8x只有2-3个点降低
指标
- FLOPs(Floating Point Operations Per Second):每秒浮点运算次数,衡量计算机处理器性能的一个重要指标,表示处理器每秒钟能够执行的浮点运算次数。深度学习领域,FLOPs常常被用来衡量模型的复杂度或者计算成本。一个模型的FLOPs越高,意味着它需要更多的计算资源和时间来进行训练或推理。因此,对于需要在资源有限的设备(如移动设备或嵌入式设备)上运行的深度学习模型,通常需要尽可能地减少模型的FLOPs。
- mAP:详解object detection中的mAP。评估目标检测和实例分割等计算机视觉任务中模型的准确性和召回率。它是所有类别的Average Precision(AP)值的平均值,其中AP是Precision-Recall曲线下的面积。在检测不同类别的物体时的准确性和召回率。mAP所有类别的AP值的平均值。mAP值越高,说明模型在整个数据集上的性能越好。
- mAP50(B):Mean Average Precision at IoU 0.50 for Large Objects 。IoU(Intersection over Union,交并比)为0.50 的情況下,针对较大目标计算的平均精度(AP)值的均值
- mAP50-95(B):Mean Average Precision across IoU 0.50 to 0.95 for Large Objects,IoU在0.50到0.95范围内的平均精度均值
- AP计算过程:IoU_threshold=0.5,然后按confidence从高到低排序,然后依据分别调低confidence看不同情况下的precision和recall,最后绘制出来了PR曲线。Pascal VOC 2008中设置IoU的阈值为0.5。COCO mAP:Iou的阈值从固定的0.5调整为在 0.5 - 0.95。
- box_loss:用于衡量预测框位置的损失函數
- cls_loss:分类目标的类别的损失函数,确保模型的类别预测尽可能准确。
- dfl_loss:Distribution Focal Loss
- precision(B):针对较大目标计算的精确率,即模型在预测中有多少是正确的,相对于所有预测中的正确和错误
- recall(B):模型正确预测的目标数与实际目标数之比。它表示模型能够检测到多少实际目标
- lr/pg0-pg2:在深度学习训练中,通常是指对不同的参数组(parameter groups)应用不同的学习率(learning rate)
优化器(optimizer)将模型可学习的参数分成了三个组,分别为pg0:BN参数,pg1:一般权重(weight)参数,pg2:偏置(biase)参数,通过param_group来管理3组参数。【玩转yolov5】请看代码之参数管理及学习率调整
-
F1:精确率(Precision)和召回率(Recall)的调和平均值
-
max F1 index:找到使平均F1分数最大化的阈值的索引。
比较详细的介绍,并且使用了yolov8m.pt模型
问题
- torch 2.1.0和cuda11不兼容
卸载torch之后重装
> python -m pip uninstall torch torchvision
> python -m pip install torch==2.0.1 torchvision==0.15.2
> apt install -y psmisc
> fuser -v /dev/nvidia*
参考
切换DSW中的notebook的python环境
YOLO模型训练的最佳实践,比如数据集大概多少
YOLOv8输出的指标含义以及针对不同指标的策略
What is YOLOv8? The Ultimate Guide.
Comparison YOLOv8 vs. YOLOv5: worse detection after training with custom dataset
pytorch optimizer param_groups
Optimizer 的 param_groups 是用于指定不同的学习率和权重衰减等超参数的集合。每个 param_group 都包含了一组参数,用于调整一组特定的超参数。通过修改 param_groups 中的超参数,我们可以对不同的层或参数使用不同的优化器参数,以便更好地训练模型。