一、项目背景与需求
视频全文介绍
【图像算法 - 10】进阶入门:改进 YOLO11 安全帽检测的关键参数与场景适配
今天我们使用深度学习来训练一个安全帽检测系统,基于YOLO11的安全帽检测系统。我们使用了两万张图片的数据集训练了这次的基于YOLO11的安全帽检测模型,然后在推理的基础上继续使用PyQt设计了可视化的操作界面。
二、数据集
此次训练使用的安全帽检测数据集,有三类,即安全帽(未佩戴)、未带安全帽、安全帽(佩戴)。然后下面是数据集的统计分析图。一共有20000余安全帽(未佩戴)、近12000的未带安全帽目标、45000安全帽目标,标签的中心坐标分布比较均匀,居中偏下较多,同时都不靠近边缘;目标,即三项目标的长宽同样比较集中,属于小目标的范围(小于图像长宽的0.2倍)。
labelme数据标注保姆级教程:从安装到格式转换全流程,附常见问题避坑指南(含视频讲解)
数据参数
# 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: C:\Users\xxx\datasets # dataset root dir
train: helmet_train # train images (relative to 'path') 4 images
val: helmet_train # val images (relative to 'path') 4 images
test: # test images (optional)
## Classes
names:0: helmet_only1: head2: helmet
三、网络选择
这里我们选择的是yolo11s的网络结构,其参数如下表所示:
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license# Ultralytics YOLO11 object detection model with P3/8 - P5/32 outputs
# Model docs: https://docs.ultralytics.com/models/yolo11
# Task docs: https://docs.ultralytics.com/tasks/detect# Parameters
nc: 80 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolo11n.yaml' will call yolo11.yaml with scale 'n'# [depth, width, max_channels]n: [0.50, 0.25, 1024] # summary: 181 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPss: [0.50, 0.50, 1024] # summary: 181 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPsm: [0.50, 1.00, 512] # summary: 231 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPsl: [1.00, 1.00, 512] # summary: 357 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPsx: [1.00, 1.50, 512] # summary: 357 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs# YOLO11n backbone
backbone:# [from, repeats, module, args]- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4- [-1, 2, C3k2, [256, False, 0.25]]- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8- [-1, 2, C3k2, [512, False, 0.25]]- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16- [-1, 2, C3k2, [512, True]]- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32- [-1, 2, C3k2, [1024, True]]- [-1, 1, SPPF, [1024, 5]] # 9- [-1, 2, C2PSA, [1024]] # 10# YOLO11n head
head:- [-1, 1, nn.Upsample, [None, 2, "nearest"]]- [[-1, 6], 1, Concat, [1]] # cat backbone P4- [-1, 2, C3k2, [512, False]] # 13- [-1, 1, nn.Upsample, [None, 2, "nearest"]]- [[-1, 4], 1, Concat, [1]] # cat backbone P3- [-1, 2, C3k2, [256, False]] # 16 (P3/8-small)- [-1, 1, Conv, [256, 3, 2]]- [[-1, 13], 1, Concat, [1]] # cat head P4- [-1, 2, C3k2, [512, False]] # 19 (P4/16-medium)- [-1, 1, Conv, [512, 3, 2]]- [[-1, 10], 1, Concat, [1]] # cat head P5- [-1, 2, C3k2, [1024, True]] # 22 (P5/32-large)- [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)
四、模型训练
环境搭建
【图像算法 - 01】保姆级深度学习环境搭建入门指南:硬件选型 + CUDA/cuDNN/Miniconda/PyTorch/Pycharm 安装全流程(附版本匹配秘籍+文末有视频讲解)
训练指令
from ultralytics import YOLO# Load a model
#model = YOLO("yolo11s.yaml") # build a new model from YAML
#model = YOLO("yolo11s.pt") # load a pretrained model (recommended for training)
model = YOLO("yolo11s.yaml").load("yolo11s.pt") # build from YAML and transfer weights# Train the model
results = model.train(data="helmet.yaml", epochs=100, imgsz=1280)
训练结果
本次训练使用的预训练模型是YOLO11s.pt(COCO数据集训练所得),epochs设置为100,batch设置为8
,imgsz设置为1280,其他均采用默认参数,训练时间大概4小时。