yolov5 增加检测层
yolov5 增加检测层
参考1:https://blog.csdn.net/weixin_41868104/article/details/111596851
参考2:https://blog.csdn.net/jacke121/article/details/118714043
原模型结构
原配置文件
# 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 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Focus, [64, 3]], # 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, 9, 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, 1, SPP, [1024, [5, 9, 13]]],
[-1, 3, C3, [1024, False]], # 9
]
# YOLOv5 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)
]
新模型结构
新配置文件
# 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:
- [ 5,6,8,14,15,11 ]
- [ 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 backbone
backbone:
# [from, number, module, args]
[ [ -1, 1, Focus, [ 64, 3 ] ], # 0-P1/2
[ -1, 1, Conv, [ 128, 3, 2 ] ], # 1-P2/4
[ -1, 3, C3, [ 128 ] ], # 2
[ -1, 1, Conv, [ 256, 3, 2 ] ], # 3-P3/8
[ -1, 9, C3, [ 256 ] ], # 4
[ -1, 1, Conv, [ 512, 3, 2 ] ], # 5-P4/16
[ -1, 9, C3, [ 512 ] ], # 6
[ -1, 1, Conv, [ 1024, 3, 2 ] ], # 7-P5/32
[ -1, 1, SPP, [ 1024, [ 5, 9, 13 ] ] ],
[ -1, 3, C3, [ 1024, False ] ], # 9
]
# YOLOv5 head
head:
[ [ -1, 1, Conv, [ 512, 1, 1 ] ], # 10
[ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ], # 11
[ [ -1, 6 ], 1, Concat, [ 1 ] ], # cat backbone P4 #12
[ -1, 3, C3, [ 512, False ] ], # 13
[ -1, 1, Conv, [ 512, 1, 1 ] ], # 14
[ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ], # 15
[ [ -1, 4 ], 1, Concat, [ 1 ] ], # cat backbone P3 16
[ -1, 3, C3, [ 512, False ] ], # 17
[ -1, 1, Conv, [ 256, 1, 1 ] ], # 18
[ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ], #19
[ [ -1, 2 ], 1, Concat, [ 1 ] ], # cat backbone P3 # 20
[ -1, 3, C3, [ 256, False ] ], # 21 (P3/8-small) 21
[ -1, 1, Conv, [ 256, 3, 2 ] ], #22
[ [ -1, 18 ], 1, Concat, [ 1 ] ], # cat backbone P3 # 23
[ -1, 3, C3, [ 256, False ] ], # 24 (P3/8-small)
[ -1, 1, Conv, [ 256, 3, 2 ] ],
[ [ -1, 14 ], 1, Concat, [ 1 ] ], # cat head P4
[ -1, 3, C3, [ 512, False ] ], # 27 (P4/16-medium)
[ -1, 1, Conv, [ 512, 3, 2 ] ],
[ [ -1, 10 ], 1, Concat, [ 1 ] ], # cat head P5
[ -1, 3, C3, [ 1024, False ] ], # 30 (P5/32-large)
[ [ 21,24,27,30 ], 1, Detect, [ nc, anchors ] ], # Detect(P3, P4, P5)
]
主要修改点:
1、 添加针对小目标的 Anchor参数
2、 从骨干网络的第2层就开始增强特征
3、 添加针对第2层的检测头部
按照原作者(参考1)上解释:
在第17层后,继续对特征图进行上采样等处理,使得特征图继续扩大,同时在第20层时,将获取到的大小为160X160的特征图与骨干网络中第2层特征图进行concat融合,以此获取更大的特征图进行小目标检测。
结果
yolov5s1 原生的yolov5s模型
yolov5s2 增加检测头的yolov5s模型
由于yolov5s2并不能完全迁移之前的权重数据,需要重新训练模型,其中精确度并不一定比yolov5s高。
以下结果 仅仅代表个人实验
yolov5s1 | yolov5s2 | yolov5m | |
---|---|---|---|
layers | 283 | 341 | 391 |
parameter | 7063542 | 7724296 | 21056406 |
gpu_mem | 10G | 11.4G | 12.6G |
weights | 14M | 15M | 41M |
GFLOPs | 16.4 | 27.5 | 50.4 |
time | 0.0017 | 0.0023 | 0.034 |
max_ap05 | 0.8856 | 0.89846 | 0.9016 |
附加
此外论文 《TPH-YOLOv5: Improved YOLOv5 Based on Transformer Prediction Head for Object Detection on Drone》
针对高空无人机对地面目标的检测 也采用了该四个检测头结构
论文链接: https://arxiv.org/pdf/2108.11539.pdf
论文解读: https://zhuanlan.zhihu.com/p/410166419