当前位置:   article > 正文

YOLOv8/YOLOv7/YOLOv5系列算法改进[NO.14]主干网络C3替换为轻量化网络Ghostnet_ghostnet bottleneck融入yolo系列算法

ghostnet bottleneck融入yolo系列算法

 前 言

作为当前先进的深度学习目标检测算法YOLOv5,已经集合了大量的trick,但是还是有提高和改进的空间,针对具体应用场景下的检测难点,可以不同的改进方法。此后的系列文章,将重点对YOLOv5的如何改进进行详细的介绍,目的是为了给那些搞科研的同学需要创新点或者搞工程项目的朋友需要达到更好的效果提供自己的微薄帮助和参考。

一、解决问题

       YOLOv5主干特征提取网络采用C3结构,带来较大的参数量,检测速度较慢,应用受限,在某些真实的应用场景如移动或者嵌入式设备,如此大而复杂的模型时难以被应用的。首先是模型过于庞大,面临着内存不足的问题,其次这些场景要求低延迟,或者说响应速度要快,想象一下自动驾驶汽车的行人检测系统如果速度很慢会发生什么可怕的事情。所以,研究小而高效的CNN模型在这些场景至关重要,至少目前是这样,尽管未来硬件也会越来越快。本文尝试将主干特征提取网络替换为更轻量的Ghostnet网络,以实现网络模型的轻量化,平衡速度和精度。

YOLOv5改进之十二:主干网络C3替换为轻量化网络ShuffleNetV2_人工智能算法工程师0301的博客-CSDN博客https://blog.csdn.net/m0_70388905/article/details/125612052YOLOv5改进之十一:主干网络C3替换为轻量化网络MobileNetV3_人工智能算法工程师0301的博客-CSDN博客https://blog.csdn.net/m0_70388905/article/details/125593267

二、基本原理

论文地址:[1911.11907] GhostNet: More Features from Cheap Operations (arxiv.org)https://arxiv.org/abs/1911.11907

Ghost模块即插即用,通过堆叠Ghost模块得出Ghost bottleneck,进而搭建轻量级神经网络——GhostNet。在ImageNet分类任务,GhostNet在相似计算量情况下Top-1正确率达75.7%,高于MobileNetV3的75.2%。

三、YOLOv7方 法

YOLOv7中已经添加了GhostSPPCSPC、GhostConv模块。

修改YOLOv7.yaml文件如下所示:

  1. # parameters
  2. nc: 1 # number of classes
  3. depth_multiple: 1.0 # model depth multiple
  4. width_multiple: 1.0 # layer channel multiple
  5. # anchors
  6. anchors:
  7. - [12,16, 19,36, 40,28] # P3/8
  8. - [36,75, 76,55, 72,146] # P4/16
  9. - [142,110, 192,243, 459,401] # P5/32
  10. # yolov7 backbone
  11. backbone:
  12. # [from, number, module, args]
  13. [[-1, 1, GhostConv, [32, 3, 1]], # 0
  14. [-1, 1, GhostConv, [64, 3, 2]], # 1-P1/2
  15. [-1, 1, GhostConv, [64, 3, 1]],
  16. [-1, 1, GhostConv, [128, 3, 2]], # 3-P2/4
  17. [-1, 1, GhostConv, [64, 1, 1]],
  18. [-2, 1, GhostConv, [64, 1, 1]],
  19. [-1, 1, GhostConv, [64, 3, 1]],
  20. [-1, 1, GhostConv, [64, 3, 1]],
  21. [-1, 1, GhostConv, [64, 3, 1]],
  22. [-1, 1, GhostConv, [64, 3, 1]],
  23. [[-1, -3, -5, -6], 1, Concat, [1]],
  24. [-1, 1, GhostConv, [256, 1, 1]], # 11
  25. [-1, 1, MP, []],
  26. [-1, 1, GhostConv, [128, 1, 1]],
  27. [-3, 1, GhostConv, [128, 1, 1]],
  28. [-1, 1, GhostConv, [128, 3, 2]],
  29. [[-1, -3], 1, Concat, [1]], # 16-P3/8
  30. [-1, 1, GhostConv, [128, 1, 1]],
  31. [-2, 1, GhostConv, [128, 1, 1]],
  32. [-1, 1, GhostConv, [128, 3, 1]],
  33. [-1, 1, GhostConv, [128, 3, 1]],
  34. [-1, 1, GhostConv, [128, 3, 1]],
  35. [-1, 1, GhostConv, [128, 3, 1]],
  36. [[-1, -3, -5, -6], 1, Concat, [1]],
  37. [-1, 1, GhostConv, [512, 1, 1]], # 24
  38. [-1, 1, MP, []],
  39. [-1, 1, GhostConv, [256, 1, 1]],
  40. [-3, 1, GhostConv, [256, 1, 1]],
  41. [-1, 1, GhostConv, [256, 3, 2]],
  42. [[-1, -3], 1, Concat, [1]], # 29-P4/16
  43. [-1, 1, GhostConv, [256, 1, 1]],
  44. [-2, 1, GhostConv, [256, 1, 1]],
  45. [-1, 1, GhostConv, [256, 3, 1]],
  46. [-1, 1, GhostConv, [256, 3, 1]],
  47. [-1, 1, GhostConv, [256, 3, 1]],
  48. [-1, 1, GhostConv, [256, 3, 1]],
  49. [[-1, -3, -5, -6], 1, Concat, [1]],
  50. [-1, 1, GhostConv, [1024, 1, 1]], # 37
  51. [-1, 1, MP, []],
  52. [-1, 1, GhostConv, [512, 1, 1]],
  53. [-3, 1, GhostConv, [512, 1, 1]],
  54. [-1, 1, GhostConv, [512, 3, 2]],
  55. [[-1, -3], 1, Concat, [1]], # 42-P5/32
  56. [-1, 1, GhostConv, [256, 1, 1]],
  57. [-2, 1, GhostConv, [256, 1, 1]],
  58. [-1, 1, GhostConv, [256, 3, 1]],
  59. [-1, 1, GhostConv, [256, 3, 1]],
  60. [-1, 1, GhostConv, [256, 3, 1]],
  61. [-1, 1, GhostConv, [256, 3, 1]],
  62. [[-1, -3, -5, -6], 1, Concat, [1]],
  63. [-1, 1, GhostConv, [1024, 1, 1]], # 50
  64. ]
  65. # yolov7 head
  66. head:
  67. [[-1, 1, GhostSPPCSPC, [512]], # 51
  68. [-1, 1, GhostConv, [256, 1, 1]],
  69. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  70. [37, 1, GhostConv, [256, 1, 1]], # route backbone P4
  71. [[-1, -2], 1, Concat, [1]],
  72. [-1, 1, GhostConv, [256, 1, 1]],
  73. [-2, 1, GhostConv, [256, 1, 1]],
  74. [-1, 1, GhostConv, [128, 3, 1]],
  75. [-1, 1, GhostConv, [128, 3, 1]],
  76. [-1, 1, GhostConv, [128, 3, 1]],
  77. [-1, 1, GhostConv, [128, 3, 1]],
  78. [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
  79. [-1, 1, GhostConv, [256, 1, 1]], # 63
  80. [-1, 1, GhostConv, [128, 1, 1]],
  81. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  82. [24, 1, GhostConv, [128, 1, 1]], # route backbone P3
  83. [[-1, -2], 1, Concat, [1]],
  84. [-1, 1, GhostConv, [128, 1, 1]],
  85. [-2, 1, GhostConv, [128, 1, 1]],
  86. [-1, 1, GhostConv, [64, 3, 1]],
  87. [-1, 1, GhostConv, [64, 3, 1]],
  88. [-1, 1, GhostConv, [64, 3, 1]],
  89. [-1, 1, GhostConv, [64, 3, 1]],
  90. [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
  91. [-1, 1, GhostConv, [128, 1, 1]], # 75
  92. [-1, 1, MP, []],
  93. [-1, 1, GhostConv, [128, 1, 1]],
  94. [-3, 1, GhostConv, [128, 1, 1]],
  95. [-1, 1, GhostConv, [128, 3, 2]],
  96. [[-1, -3, 63], 1, Concat, [1]],
  97. [-1, 1, GhostConv, [256, 1, 1]],
  98. [-2, 1, GhostConv, [256, 1, 1]],
  99. [-1, 1, GhostConv, [128, 3, 1]],
  100. [-1, 1, GhostConv, [128, 3, 1]],
  101. [-1, 1, GhostConv, [128, 3, 1]],
  102. [-1, 1, GhostConv, [128, 3, 1]],
  103. [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
  104. [-1, 1, GhostConv, [256, 1, 1]], # 88
  105. [-1, 1, MP, []],
  106. [-1, 1, GhostConv, [256, 1, 1]],
  107. [-3, 1, GhostConv, [256, 1, 1]],
  108. [-1, 1, GhostConv, [256, 3, 2]],
  109. [[-1, -3, 51], 1, Concat, [1]],
  110. [-1, 1, GhostConv, [512, 1, 1]],
  111. [-2, 1, GhostConv, [512, 1, 1]],
  112. [-1, 1, GhostConv, [256, 3, 1]],
  113. [-1, 1, GhostConv, [256, 3, 1]],
  114. [-1, 1, GhostConv, [256, 3, 1]],
  115. [-1, 1, GhostConv, [256, 3, 1]],
  116. [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
  117. [-1, 1, GhostConv, [512, 1, 1]], # 101
  118. [75, 1, RepConv, [256, 3, 1]],
  119. [88, 1, RepConv, [512, 3, 1]],
  120. [101, 1, RepConv, [1024, 3, 1]],
  121. [[102,103,104], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
  122. ]

四、YOLOv5方 法

第一步:修改common.py,增加ghostC3模块。

  1. class GhostBottleneck(nn.Module):
  2. # Ghost Bottleneck https://github.com/huawei-noah/ghostnet
  3. def __init__(self, c1, c2, k=3, s=1): # ch_in, ch_out, kernel, stride
  4. super().__init__()
  5. c_ = c2 // 2
  6. self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), # pw
  7. DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw
  8. GhostConv(c_, c2, 1, 1, act=False)) # pw-linear
  9. self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False),
  10. Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity()
  11. def forward(self, x):
  12. return self.conv(x) + self.shortcut(x)
  13. class C3Ghost(C3):
  14. # C3 module with GhostBottleneck()
  15. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
  16. super().__init__(c1, c2, n, shortcut, g, e)
  17. c_ = int(c2 * e) # hidden channels
  18. 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即可。

结 果:本人在多个数据集上做了大量实验,针对不同的数据集效果不同,map值有所下降,但是权值模型大小降低,参数量下降。

预告一下:下一篇内容将继续分享网络轻量化方法的分享——深度可分离卷积。有兴趣的朋友可以关注一下我,有问题可以留言或者私聊我哦

PS:主干网络的替换不仅仅是适用改进YOLOv5,也可以改进其他的YOLO网络以及目标检测网络,比如YOLOv4、v3等。

最后,希望能互粉一下,做个朋友,一起学习交流。

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

闽ICP备14008679号