前言:
本次引入了一种新方法 GSConv 来减轻模型的复杂度并保持准确性。GSConv 可以更好地平衡模型的准确性和速度。并且,提供了一种设计范式,Slim-Neck,以实现检测器更高的计算成本效益。在实验中,与原始网络相比,本文方法获得了最先进的结果。
论文链接:https://arxiv.org/pdf/2206.02424.pdf
1、将以下代码加入common.py
#====================================GSConv===========================================
class GSConv(nn.Module):
# GSConv https://github.com/AlanLi1997/slim-neck-by-gsconv
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)
class GSBottleneck(nn.Module):
# GS Bottleneck https://github.com/AlanLi1997/slim-neck-by-gsconv
def __init__(self, c1, c2, k=3, s=1):
super().__init__()
c_ = c2 // 2
# for lighting
self.conv_lighting = nn.Sequential(
GSConv(c1, c_, 1, 1),
GSConv(c_, c2, 1, 1, act=False))
# for receptive field
self.conv = nn.Sequential(
GSConv(c1, c_, 3, 1),
GSConv(c_, c2, 3, 1, act=False))
self.shortcut = nn.Identity()
def forward(self, x):
return self.conv_lighting(x)
class GSBottleneck2(GSBottleneck):
# GS Bottleneck https://github.com/AlanLi1997/slim-neck-by-gsconv
def __init__(self, c1, c2, k=3, s=1):
super().__init__(c1, c2, k, s)
def forward(self, x):
return self.conv(x) + self.shortcut(x)
class VoVGSCSP(nn.Module):
# VoV-GSCSP https://github.com/AlanLi1997/slim-neck-by-gsconv
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
super().__init__()
c_ = int(c2 * e)
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(2 * c_, c2, 1)
self.m = nn.Sequential(*(GSBottleneck(c_, c_) for _ in range(n)))
def forward(self, x):
x1 = self.cv1(x)
return self.cv2(torch.cat((self.m(x1), x1), dim=1))
class VoVGSCSP2(VoVGSCSP):
# VoV-GSCSP2 https://github.com/AlanLi1997/slim-neck-by-gsconv
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
super().__init__(c1, c2, n=1, shortcut=True, g=1, e=0.5)
c_ = int(c2 * e)
self.m = nn.Sequential(*(GSBottleneck2(c_, c_) for _ in range(n)))
#============================================END=========================================
2、打开yolo.py文件
找到parse_model模块,加入类名 注意 有两处需要添加的地方
3、更改yolov5s.yaml
删去原有代码,把下面代码全部复制过去就行
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
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
]
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)
]
搞定!