赞
踩
一、解决问题
作为当前先进的深度学习目标检测算法YOLOv5,已经集合了大量的trick,但是还是有提高和改进的空间,针对具体应用场景下的检测难点,可以不同的改进方法。此后的系列文章,将重点对YOLOv5的如何改进进行详细的介绍,目的是为了给那些搞科研的同学需要创新点或者搞工程项目的朋友需要达到更好的效果提供自己的微薄帮助和参考。
YOLOv5主干特征提取网络采用C3结构,带来较大的参数量,检测速度较慢,应用受限,在某些真实的应用场景如移动或者嵌入式设备,如此大而复杂的模型时难以被应用的。首先是模型过于庞大,面临着内存不足的问题,其次这些场景要求低延迟,或者说响应速度要快,想象一下自动驾驶汽车的行人检测系统如果速度很慢会发生什么可怕的事情。所以,研究小而高效的CNN模型在这些场景至关重要,至少目前是这样,尽管未来硬件也会越来越快。本文尝试将主干特征提取网络替换为更轻量的Ghostnet网络,以实现网络模型的轻量化,平衡速度和精度。
二、基本原理
Ghost模块即插即用,通过堆叠Ghost模块得出Ghost bottleneck,进而搭建轻量级神经网络——GhostNet。在ImageNet分类任务,GhostNet在相似计算量情况下Top-1正确率达75.7%,高于MobileNetV3的75.2%。
三、YOLOv7方法
YOLOv7中已经添加了GhostSPPCSPC、GhostConv模块。
修改YOLOv7.yaml文件如下所示:
# parameters nc: 1 # number of classes depth_multiple: 1.0 # model depth multiple width_multiple: 1.0 # layer channel multiple # anchors anchors: - [12,16, 19,36, 40,28] # P3/8 - [36,75, 76,55, 72,146] # P4/16 - [142,110, 192,243, 459,401] # P5/32 # yolov7 backbone backbone: # [from, number, module, args] [[-1, 1, GhostConv, [32, 3, 1]], # 0 [-1, 1, GhostConv, [64, 3, 2]], # 1-P1/2 [-1, 1, GhostConv, [64, 3, 1]], [-1, 1, GhostConv, [128, 3, 2]], # 3-P2/4 [-1, 1, GhostConv, [64, 1, 1]], [-2, 1, GhostConv, [64, 1, 1]], [-1, 1, GhostConv, [64, 3, 1]], [-1, 1, GhostConv, [64, 3, 1]], [-1, 1, GhostConv, [64, 3, 1]], [-1, 1, GhostConv, [64, 3, 1]], [[-1, -3, -5, -6], 1, Concat, [1]], [-1, 1, GhostConv, [256, 1, 1]], # 11 [-1, 1, MP, []], [-1, 1, GhostConv, [128, 1, 1]], [-3, 1, GhostConv, [128, 1, 1]], [-1, 1, GhostConv, [128, 3, 2]], [[-1, -3], 1, Concat, [1]], # 16-P3/8 [-1, 1, GhostConv, [128, 1, 1]], [-2, 1, GhostConv, [128, 1, 1]], [-1, 1, GhostConv, [128, 3, 1]], [-1, 1, GhostConv, [128, 3, 1]], [-1, 1, GhostConv, [128, 3, 1]], [-1, 1, GhostConv, [128, 3, 1]], [[-1, -3, -5, -6], 1, Concat, [1]], [-1, 1, GhostConv, [512, 1, 1]], # 24 [-1, 1, MP, []], [-1, 1, GhostConv, [256, 1, 1]], [-3, 1, GhostConv, [256, 1, 1]], [-1, 1, GhostConv, [256, 3, 2]], [[-1, -3], 1, Concat, [1]], # 29-P4/16 [-1, 1, GhostConv, [256, 1, 1]], [-2, 1, GhostConv, [256, 1, 1]], [-1, 1, GhostConv, [256, 3, 1]], [-1, 1, GhostConv, [256, 3, 1]], [-1, 1, GhostConv, [256, 3, 1]], [-1, 1, GhostConv, [256, 3, 1]], [[-1, -3, -5, -6], 1, Concat, [1]], [-1, 1, GhostConv, [1024, 1, 1]], # 37 [-1, 1, MP, []], [-1, 1, GhostConv, [512, 1, 1]], [-3, 1, GhostConv, [512, 1, 1]], [-1, 1, GhostConv, [512, 3, 2]], [[-1, -3], 1, Concat, [1]], # 42-P5/32 [-1, 1, GhostConv, [256, 1, 1]], [-2, 1, GhostConv, [256, 1, 1]], [-1, 1, GhostConv, [256, 3, 1]], [-1, 1, GhostConv, [256, 3, 1]], [-1, 1, GhostConv, [256, 3, 1]], [-1, 1, GhostConv, [256, 3, 1]], [[-1, -3, -5, -6], 1, Concat, [1]], [-1, 1, GhostConv, [1024, 1, 1]], # 50 ] # yolov7 head head: [[-1, 1, GhostSPPCSPC, [512]], # 51 [-1, 1, GhostConv, [256, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [37, 1, GhostConv, [256, 1, 1]], # route backbone P4 [[-1, -2], 1, Concat, [1]], [-1, 1, GhostConv, [256, 1, 1]], [-2, 1, GhostConv, [256, 1, 1]], [-1, 1, GhostConv, [128, 3, 1]], [-1, 1, GhostConv, [128, 3, 1]], [-1, 1, GhostConv, [128, 3, 1]], [-1, 1, GhostConv, [128, 3, 1]], [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], [-1, 1, GhostConv, [256, 1, 1]], # 63 [-1, 1, GhostConv, [128, 1, 1]], [-1, 1, nn.Upsample, [None, 2, 'nearest']], [24, 1, GhostConv, [128, 1, 1]], # route backbone P3 [[-1, -2], 1, Concat, [1]], [-1, 1, GhostConv, [128, 1, 1]], [-2, 1, GhostConv, [128, 1, 1]], [-1, 1, GhostConv, [64, 3, 1]], [-1, 1, GhostConv, [64, 3, 1]], [-1, 1, GhostConv, [64, 3, 1]], [-1, 1, GhostConv, [64, 3, 1]], [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], [-1, 1, GhostConv, [128, 1, 1]], # 75 [-1, 1, MP, []], [-1, 1, GhostConv, [128, 1, 1]], [-3, 1, GhostConv, [128, 1, 1]], [-1, 1, GhostConv, [128, 3, 2]], [[-1, -3, 63], 1, Concat, [1]], [-1, 1, GhostConv, [256, 1, 1]], [-2, 1, GhostConv, [256, 1, 1]], [-1, 1, GhostConv, [128, 3, 1]], [-1, 1, GhostConv, [128, 3, 1]], [-1, 1, GhostConv, [128, 3, 1]], [-1, 1, GhostConv, [128, 3, 1]], [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], [-1, 1, GhostConv, [256, 1, 1]], # 88 [-1, 1, MP, []], [-1, 1, GhostConv, [256, 1, 1]], [-3, 1, GhostConv, [256, 1, 1]], [-1, 1, GhostConv, [256, 3, 2]], [[-1, -3, 51], 1, Concat, [1]], [-1, 1, GhostConv, [512, 1, 1]], [-2, 1, GhostConv, [512, 1, 1]], [-1, 1, GhostConv, [256, 3, 1]], [-1, 1, GhostConv, [256, 3, 1]], [-1, 1, GhostConv, [256, 3, 1]], [-1, 1, GhostConv, [256, 3, 1]], [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], [-1, 1, GhostConv, [512, 1, 1]], # 101 [75, 1, RepConv, [256, 3, 1]], [88, 1, RepConv, [512, 3, 1]], [101, 1, RepConv, [1024, 3, 1]], [[102,103,104], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5) ]
四、YOLOv5方 法
第一步:修改common.py,增加ghostC3模块。
class GhostBottleneck(nn.Module): # Ghost Bottleneck https://github.com/huawei-noah/ghostnet def __init__(self, c1, c2, k=3, s=1): # ch_in, ch_out, kernel, stride super().__init__() c_ = c2 // 2 self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), # pw DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw GhostConv(c_, c2, 1, 1, act=False)) # pw-linear self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False), Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity() def forward(self, x): return self.conv(x) + self.shortcut(x) class C3Ghost(C3): # C3 module with GhostBottleneck() def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*(GhostBottleneck(c_, c_) for _ in range(n)))
第二步:将yolo.py中注册模块。
if m in [Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv, BottleneckCSP,
CoordAtt,CrossConv,C3,CTR3,C3TR,C3SPP, C3Ghost,
第三步:进行修改yaml文件
将C3模块替换成C3Ghost即可
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。