(一)前言
1.GSConv模块
2.Slim Neck(GSBottleneck+VoVGSCSP)
(二)YOLOv5改进之GSConv+Slim Neck
1.配置common.py文件
2.配置yolo.py文件
3.配置YOLOv5/YOLOv7_GSConv.yaml文件
作者提出了一种新方法 GSConv 来减轻模型的复杂度并保持准确性。GSConv可以更好地平衡模型的准确性和速度。并且,提供了一种设计范式Slim Neck,以实现检测器更高的计算成本效益。实验过程中,与原始网络相比,改进方法获得了最优秀的检测结果。
如上图所示,作者在 SODA10M 的无人驾驶数据集上比较了最先进的Slim Neck 检测器和原始检测器的速度和准确度,并证实了该方法的有效性。
复制粘贴以下代码:
#GSConv_______________________________________________________
class GSConv(nn.Module):
def __init__(self, c1, c2, k=1, s=1, g=1, act=True):
super().__init__()
c_ = c2 // 2
self.cv1 = Conv(c1, c_, k, s, None, g, act)
self.cv2 = Conv(c_, c_, 5, 1, None, c_, act)
def forward(self, x):
x1 = self.cv1(x)
x2 = torch.cat((x1, self.cv2(x1)), 1)
# shuffle
b, n, h, w = x2.data.size()
b_n = b * n // 2
y = x2.reshape(b_n, 2, h * w)
y = y.permute(1, 0, 2)
y = y.reshape(2, -1, n // 2, h, w)
return torch.cat((y[0], y[1]), 1)
找到相应位置,加入GSConv。
if m in [Conv, GhostConv, Bottleneck, GhostBottleneck, SPP,
DWConv, MixConv2d, Focus, CrossConv, BottleneckCSP, C3, C3TR, GSConv]:
下面以YOLOv5为例。
# 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, GSConv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 1, GSConv, [256, 3, 2]], # 3-P3/8
[-1, 9, C3, [256]],
[-1, 1, GSConv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, GSConv, [1024, 3, 2]], # 7-P5/32
[-1, 1, SPP, [1024, [5, 9, 13]]],
[-1, 3, C3, [1024, False]], # 9
]
# YOLOv5 head
head:
[[-1, 1, GSConv, [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, GSConv, [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, GSConv, [256, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
[-1, 1, GSConv, [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)
]