yolov7代码结构简单,效果还好,但是动辄超过70几个模块的配置文件对于想要对网络进行魔改的朋友还是不怎么友好的,使用最小的tiny也有77个模块
代码的整体结构简单,直接将ELAN结构化写成一个类就能像yolov5一样仅仅只有20几个模块,方便对网络结构进行魔改,看着也不容易头晕。
v7的网络结构可以参看:理解yolov7网络结构_yolov7的常用anchor_athrunsunny的博客-CSDN博客
这里先放一张改好的网络结构运行结果
好,上主菜,这里对tiny的结构进行修改,v7的修改类似就是在ELAN这个类中增加卷积层就行
先创建配置文件yolov7-tiny-ELAN.yaml
# parameters
nc: 80 # number of classes
depth_multiple: 1.0 # model depth multiple
width_multiple: 1.0 # layer channel multipleactivation: nn.ReLU()
# 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# yolov7-tiny backbone
backbone:# [from, number, module, args] c2, k=1, s=1, p=None, g=1, act=True[[-1, 1, Conv, [32, 3, 2, None, 1]], # 0-P1/2[-1, 1, Conv, [64, 3, 2, None, 1]], # 1-P2/4[-1, 1, ELAN, [64, 1, 1, None, 1]], # 2[-1, 1, MP, []], # 3-P3/8[-1, 1, ELAN, [128, 1, 1, None, 1]], # 4[-1, 1, MP, []], # 5-P4/16[-1, 1, ELAN, [256, 1, 1, None, 1]], # 6[-1, 1, MP, []], # 7-P5/32[-1, 1, ELAN, [512, 1, 1, None, 1]], # 8]# yolov7-tiny head
head:[[-1, 1, SPPCSPCSIM, [256]], # 9[-1, 1, Conv, [128, 1, 1, None, 1]],[-1, 1, nn.Upsample, [None, 2, 'nearest']],[6, 1, Conv, [128, 1, 1, None, 1]], # route backbone P4[[-1, -2], 1, Concat, [1]], # 13[-1, 1, ELAN, [128, 1, 1, None, 1]], # 14[-1, 1, Conv, [64, 1, 1, None, 1]],[-1, 1, nn.Upsample, [None, 2, 'nearest']],[4, 1, Conv, [64, 1, 1, None, 1]], # route backbone P3[[-1, -2], 1, Concat, [1]],[-1, 1, ELAN, [64, 1, 1, None, 1]], # 19[-1, 1, Conv, [128, 3, 2, None, 1]],[[-1, 14], 1, Concat, [1]],[-1, 1, ELAN, [128, 1, 1, None, 1]], # 22[-1, 1, Conv, [256, 3, 2, None, 1]],[[-1, 9], 1, Concat, [1]],[-1, 1, ELAN, [256, 1, 1, None, 1]], # 25[19, 1, Conv, [128, 3, 1, None, 1]],[22, 1, Conv, [256, 3, 1, None, 1]],[25, 1, Conv, [512, 3, 1, None, 1]],[[26,27,28], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)]
在common.py中增加
class ELAN(nn.Module):# Yolov7 ELAN with args(ch_in, ch_out, kernel, stride, padding, groups, activation)def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):super().__init__()c_ = int(c2 // 2)c_out = c_ * 4self.cv1 = Conv(c1, c_, k=k, s=s, p=p, g=g, act=act)self.cv2 = Conv(c1, c_, k=k, s=s, p=p, g=g, act=act)self.cv3 = Conv(c_, c_, k=3, s=s, p=p, g=g, act=act)self.cv4 = Conv(c_, c_, k=3, s=s, p=p, g=g, act=act)self.cv5 = Conv(c_out, c2, k=k, s=s, p=p, g=g, act=act)def forward(self, x):x1 = self.cv1(x)x2 = self.cv2(x)x3 = self.cv3(x2)x4 = self.cv4(x3)x5 = torch.cat((x1, x2, x3, x4), 1)return self.cv5(x5)class SPPCSPCSIM(nn.Module):def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)):super(SPPCSPCSIM, self).__init__()c_ = int(2 * c2 * e) # hidden channelsself.cv1 = Conv(c1, c_, 1, 1)self.cv2 = Conv(c1, c_, 1, 1)self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])self.cv3 = Conv(4 * c_, c_, 1, 1)self.cv4 = Conv(2 * c_, c2, 1, 1)def forward(self, x):x1 = self.cv1(x)x2 = self.cv2(x)x3 = torch.cat([x2] + [m(x2) for m in self.m], 1)x4 = self.cv3(x3)x5 = torch.cat((x1, x4), 1)return self.cv4(x5)
在yolo.py中的parse_model中增加
if m in (Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,BottleneckCSP, C3, C3TR, C3SPP, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x, SPPCSPC, RepConv,RFEM, ELAN, SPPCSPCSIM):c1, c2 = ch[f], args[0]if c2 != no: # if not outputc2 = make_divisible(c2 * gw, 8)args = [c1, c2, *args[1:]]if m in [BottleneckCSP, C3, C3TR, C3Ghost, C3x]:args.insert(2, n) # number of repeatsn = 1
使用 yolov7-tiny-ELAN.yaml在yolo.py中运行就能看到上面结构精简后的网络结构图。