当前位置:   article > 正文

YOLOv8/YOLOv7/YOLOv5系列算法改进[NO.14]主干网络C3替换为轻量化网络Ghostnet_yolov8 替换backbone

yolov8 替换backbone

一、解决问题

作为当前先进的深度学习目标检测算法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)
  ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140

四、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)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

第二步:将yolo.py中注册模块。

if m in [Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv, BottleneckCSP,
         CoordAtt,CrossConv,C3,CTR3,C3TR,C3SPP, C3Ghost,
  • 1
  • 2

第三步:进行修改yaml文件
将C3模块替换成C3Ghost即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/461072
推荐阅读
相关标签
  

闽ICP备14008679号