yolov5增加小目标检测层
一,model文件解释
yolov5的模型配置文件如下:
-
# parameters
-
nc: 80 # number of classes
-
depth_multiple: 1.0 # model depth multiple
-
width_multiple: 1.0 # layer channel multiple
-
-
# anchors
-
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, BottleneckCSP, [128]],
-
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
-
[-1, 9, BottleneckCSP, [256]],
-
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
-
[-1, 9, BottleneckCSP, [512]],
-
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
-
[-1, 1, SPP, [1024, [5, 9, 13]]],
-
[-1, 3, BottleneckCSP, [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, BottleneckCSP, [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, BottleneckCSP, [256, False]], # 17 (P3/8-small)
-
-
[-1, 1, Conv, [256, 3, 2]],
-
[[-1, 14], 1, Concat, [1]], # cat head P4
-
[-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium)
-
-
[-1, 1, Conv, [512, 3, 2]],
-
[[-1, 10], 1, Concat, [1]], # cat head P5
-
[-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large)
-
-
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
-
]
1.类别数和模型深度宽度控制
-
# parameters
-
nc: 80 # number of classes
-
depth_multiple: 1.0 # model depth multiple
-
width_multiple: 1.0 # layer channel multiple
nc:代表类别数
depth_multiple:模型深度参数
width_multiple:模型宽度参数
其中模型深度宽度控制,是通过上面两个参数,作用于BottleneckCSP。
2.初始Anchor参数
-
# anchors
-
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
-
原始模型,只有三个检测层,因此对应三组初始化Anchor值。当输入图像尺寸为640X640时,# P3/8 对应的检测层大小为80X80大小,可以用来检测大小在8X8以上的目标。# P4/16对应的检测层大小为40X40大小,可以用来检测大小在16X16以上的目标。# P5/32对应的检测层大小为20X20大小,可以用来检测32X32以上的目标。
3.骨干网络
-
# 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, BottleneckCSP, [128]],
-
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
-
[-1, 9, BottleneckCSP, [256]],
-
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
-
[-1, 9, BottleneckCSP, [512]],
-
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
-
[-1, 1, SPP, [1024, [5, 9, 13]]],
-
[-1, 3, BottleneckCSP, [1024, False]], # 9
-
]
这一段配置主要是为了提取图像特征
4.检测头部
-
# 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, BottleneckCSP, [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, BottleneckCSP, [256, False]], # 17 (P3/8-small)
-
-
[-1, 1, Conv, [256, 3, 2]],
-
[[-1, 14], 1, Concat, [1]], # cat head P4
-
[-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium)
-
-
[-1, 1, Conv, [512, 3, 2]],
-
[[-1, 10], 1, Concat, [1]], # cat head P5
-
[-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large)
-
-
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
-
]
这一段配置,实际上包括了两个部分,即颈部(Neck)和Prediction,在颈部,主要利用类似FPN+PAN的方式。对骨干网络提取到的特征,进行多尺度融合处理。再送入检测层。
在yolov3中,该阶段只用到了FPN融合,FPN是一种自顶向下传达强语义特征的网络,即一个正金字塔形结构,融合的特征图越来越小。在yolov5中,除了使用FPN结构对特征进行融合,还使用到了PAN结构,PAN与FPN网络正好相反,是一个倒金字塔结构,自底向上传达强定位特征。两者相互结合。
二,增加小目标的配置文件
-
# parameters
-
nc: 1 # number of classes
-
depth_multiple: 1.0 # model depth multiple
-
width_multiple: 1.0 # layer channel multiple
-
-
# anchors
-
anchors:
-
- [5,6, 8,14, 15,11] #4
-
- [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, BottleneckCSP, [128]], #160*160
-
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
-
[-1, 9, BottleneckCSP, [256]], #80*80
-
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
-
[-1, 9, BottleneckCSP, [512]], #40*40
-
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
-
[-1, 1, SPP, [1024, [5, 9, 13]]],
-
[-1, 3, BottleneckCSP, [1024, False]], # 9 20*20
-
]
-
-
# YOLOv5 head
-
head:
-
[[-1, 1, Conv, [512, 1, 1]], #20*20
-
[-1, 1, nn.Upsample, [None, 2, 'nearest']], #40*40
-
[[-1, 6], 1, Concat, [1]], # cat backbone P4 40*40
-
[-1, 3, BottleneckCSP, [512, False]], # 13 40*40
-
-
[-1, 1, Conv, [512, 1, 1]], #40*40
-
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
-
[[-1, 4], 1, Concat, [1]], # cat backbone P3 80*80
-
[-1, 3, BottleneckCSP, [512, False]], # 17 (P3/8-small) 80*80
-
-
[-1, 1, Conv, [256, 1, 1]], #18 80*80
-
[-1, 1, nn.Upsample, [None, 2, 'nearest']], #19 160*160
-
[[-1, 2], 1, Concat, [1]], #20 cat backbone p2 160*160
-
[-1, 3, BottleneckCSP, [256, False]], #21 160*160
-
-
[-1, 1, Conv, [256, 3, 2]], #22 80*80
-
[[-1, 18], 1, Concat, [1]], #23 80*80
-
[-1, 3, BottleneckCSP, [256, False]], #24 80*80
-
-
[-1, 1, Conv, [256, 3, 2]], #25 40*40
-
[[-1, 14], 1, Concat, [1]], # 26 cat head P4 40*40
-
[-1, 3, BottleneckCSP, [512, False]], # 27 (P4/16-medium) 40*40
-
-
[-1, 1, Conv, [512, 3, 2]], #28 20*20
-
[[-1, 10], 1, Concat, [1]], #29 cat head P5 #20*20
-
[-1, 3, BottleneckCSP, [1024, False]], # 30 (P5/32-large) 20*20
-
-
[[21, 24, 27, 30], 1, Detect, [nc, anchors]], # Detect(p2, P3, P4, P5)
-
]
-
可以看到,主要对两个地方进行了修改
1.初始Anchor设置
-
# anchors
-
anchors:
-
- [5,6, 8,14, 15,11] #4
-
- [10,13, 16,30, 33,23] # P3/8
-
- [30,61, 62,45, 59,119] # P4/16
-
- [116,90, 156,198, 373,326] # P5/32
-
这里的修改,主要是增加了一组Anchor;在这里没什么要求,只要数值小一点就行
2.检测头部
-
# YOLOv5 head
-
head:
-
[[-1, 1, Conv, [512, 1, 1]], #20*20
-
[-1, 1, nn.Upsample, [None, 2, 'nearest']], #40*40
-
[[-1, 6], 1, Concat, [1]], # cat backbone P4 40*40
-
[-1, 3, BottleneckCSP, [512, False]], # 13 40*40
-
-
[-1, 1, Conv, [512, 1, 1]], #40*40
-
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
-
[[-1, 4], 1, Concat, [1]], # cat backbone P3 80*80
-
[-1, 3, BottleneckCSP, [512, False]], # 17 (P3/8-small) 80*80
-
-
[-1, 1, Conv, [256, 1, 1]], #18 80*80
-
[-1, 1, nn.Upsample, [None, 2, 'nearest']], #19 160*160
-
[[-1, 2], 1, Concat, [1]], #20 cat backbone p2 160*160
-
[-1, 3, BottleneckCSP, [256, False]], #21 160*160
-
-
[-1, 1, Conv, [256, 3, 2]], #22 80*80
-
[[-1, 18], 1, Concat, [1]], #23 80*80
-
[-1, 3, BottleneckCSP, [256, False]], #24 80*80
-
-
[-1, 1, Conv, [256, 3, 2]], #25 40*40
-
[[-1, 14], 1, Concat, [1]], # 26 cat head P4 40*40
-
[-1, 3, BottleneckCSP, [512, False]], # 27 (P4/16-medium) 40*40
-
-
[-1, 1, Conv, [512, 3, 2]], #28 20*20
-
[[-1, 10], 1, Concat, [1]], #29 cat head P5 #20*20
-
[-1, 3, BottleneckCSP, [1024, False]], # 30 (P5/32-large) 20*20
-
-
[[21, 24, 27, 30], 1, Detect, [nc, anchors]], # Detect(p2, P3, P4, P5)
-
]
-
在这一部分,主要增加了几个操作层,在第17层后,继续对特征图进行上采样等处理,使得特征图继续扩大,同时在第20层时,将获取到的大小为160X160的特征图与骨干网络中第2层特征图进行concat融合,以此获取更大的特征图进行小目标检测。
在第31层,即检测层,增加小目标检测层,一共使用四层[21, 24, 27, 30]进行检测。
在增加检测层后,带来的问题就是计算量增加,导致推理检测速度降低。不过对于小目标,确实有很好的改善。
三,检测效果
解释:为了测试性能,将一张包含众多文本目标的图像,复制四份,拼接成一张大图,原始图像尺寸达到7503X5588。输入测试网络尺寸为640X640大小。
1.加入小目标检测层效果
2.原始检测效果
可以看到,增加小目标检测层后,对于小目标具有更好的检测效果!
文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/118714043
- 点赞
- 收藏
- 关注作者
评论(0)