文章目录
1、前言
在边缘设备的场景下,目前的YOLOv5s,虽然能够快速实现目标检测,但是运行速度依旧稍慢点,本文在牺牲一点精度前提下,提高目标检测速度,即轻量化YOLOv5s模型,并部署到边缘设备上,可以在CPU上达到实时的检测效果,满足业务的性能需求。
2、轻量化模型结构:
轻量化思路:
1、改进锚框,将对应的锚框全部减半
2、将yolov5s的模型的channels通道数全部都减少一半。
3、训练时,输入图片大小为320,即从 640x640 变为 320x320
原始yolov5s.yaml模型结构
yaml
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
# Parameters
nc: 80 # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple
anchors:
- [10,13, 16,30, 33,23] # P3/8
- [30,61, 62,45, 59,119] # P4/16
- [116,90, 156,198, 373,326] # P5/32
# YOLOv5 v6.0 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 6, C3, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 3, C3, [1024]],
[-1, 1, SPPF, [1024, 5]], # 9
]
# YOLOv5 v6.0 head
head:
[[-1, 1, Conv, [512, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [512, False]], # 13
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]
轻量化yolov5s-320.yaml结构
python
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
# Parameters
nc: 10 # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple
anchors:
- [5,6, 8,15, 16,11] # P3/8
- [15,30, 31,22, 29,59] # P4/16
- [58,45, 78,99, 186,163] # P5/32
# YOLOv5 v6.0 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Conv, [32, 6, 2, 2]], # 0-P1/2
[-1, 1, Conv, [64, 3, 2]], # 1-P2/4
[-1, 3, C3, [64]],
[-1, 1, Conv, [128, 3, 2]], # 3-P3/8
[-1, 6, C3, [128]],
[-1, 1, Conv, [256, 3, 2]], # 5-P4/16
[-1, 9, C3, [256]],
[-1, 1, Conv, [512, 3, 2]], # 7-P5/32
[-1, 3, C3, [512]],
[-1, 1, SPPF, [512, 5]], # 9
]
# YOLOv5 v6.0 head
head:
[[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [256, False]], # 13
[-1, 1, Conv, [128, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [128, False]], # 17 (P3/8-small)
[-1, 1, Conv, [128, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [256, False]], # 20 (P4/16-medium)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [512, False]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]
模型查看
python
# 查看模型结构可以运行
python models/yolo.py --cfg yolov5s-320.yaml
# 训练时候设置--img 320
python train.py --batch-size 16 --epochs 100 --img 320 --cfg models/yolov5s-320.yaml --data data/traffic.yaml --weights weights/yolov5s.pt --device 0 > myout.file 2>&1 &
3、模型对比
模型 | input-size | params(M) | GFLOPs | precision | recall | mAP_0.5 | mAP_0.5:0.95 | 模型大小 |
---|---|---|---|---|---|---|---|---|
YOLOv5s | 640×640 | 7.04 | 15.8 | 0.987 | 0.99 | 0.993 | 0.828 | 14.4MB |
YOLOv5s-320 | 320x320 | 1.77 | 4.2 | 0.895 | 0.992 | 0.912 | 0.749 | 3.9MB |
4、训练结果图
这是训练epoch的可视化图,可以看到mAP随着Epoch训练,逐渐提高
这是每个类别的F1-Score分数
这是模型的PR曲线
这是混淆矩阵