当前位置:   article > 正文

YOLOv9有效改进|使用空间和通道重建卷积SCConv改进RepNCSPELAN4

repncspelan


专栏介绍:YOLOv9改进系列 | 包含深度学习最新创新,主力高效涨点!!!


一、改进点介绍

        SCConv是一种即插即用的空间和通道重建卷积。

        RepNCSPELAN4是YOLOv9中的特征提取模块,类似YOLOv5和v8中的C2f与C3模块。


二、RepNCSPELAN4_SCConv模块详解

 2.1 模块简介

       RepNCSPELAN4_SCConv的主要思想:  使用SCConv替换RepNCSPELAN4中的Conv模块。


三、 RepNCSPELAN4_SCConv模块使用教程

3.1 RepNCSPELAN4_SCConv模块的代码

  1. class RepNBottleneck_SC(RepNBottleneck):
  2. # Standard bottleneck
  3. def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5): # ch_in, ch_out, shortcut, kernels, groups, expand
  4. super().__init__( c1, c2, shortcut, g, k, e)
  5. c_ = int(c2 * e) # hidden channels
  6. self.cv1 = RepConvN(c1, c_, k[0], 1)
  7. self.cv2 = SCConv(c_, c2, s=1, g=g)
  8. self.add = shortcut and c1 == c2
  9. class RepNCSP_SCConv(RepNCSP):
  10. # CSP Bottleneck with 3 convolutions
  11. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  12. super().__init__(c1, c2, n, shortcut, g, e)
  13. c_ = int(c2 * e) # hidden channels
  14. self.cv1 = Conv(c1, c_)
  15. self.cv2 = SCConv(c1, c_)
  16. self.cv3 = Conv(2 * c_, c2) # optional act=FReLU(c2)
  17. self.m = nn.Sequential(*(RepNBottleneck_SC(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
  18. class RepNCSPELAN4SCConv(RepNCSPELAN4):
  19. # csp-elan
  20. def __init__(self, c1, c2, c3, c4, c5=1): # ch_in, ch_out, number, shortcut, groups, expansion
  21. super().__init__(c1, c2, c3, c4, c5)
  22. self.cv1 = Conv(c1, c3, k=1, s=1)
  23. self.cv2 = nn.Sequential(RepNCSP_SCConv(c3 // 2, c4, c5), SCConv(c4, c4))
  24. self.cv3 = nn.Sequential(RepNCSP_SCConv(c4, c4, c5), SCConv(c4, c4))
  25. self.cv4 = Conv(c3 + (2 * c4), c2, 1, 1)
  26. class SCConv(nn.Module):
  27. """https://github.com/MCG-NKU/SCNet/blob/master/scnet.py"""
  28. def __init__(self, inplanes, planes,k=3, s=1, p=1, dilation=1, g=1, pooling_r=4):
  29. super(SCConv, self).__init__()
  30. self.k2 = nn.Sequential(
  31. nn.AvgPool2d(kernel_size=pooling_r, stride=pooling_r),
  32. Conv(inplanes, planes, k=k, s=s, p=p, d=dilation, g=g, act=False))
  33. self.k3 = Conv(inplanes, planes, k=k, s=s, p=p, d=dilation, g=g, act=False)
  34. self.k4 = Conv(inplanes, planes, k=k, s=s, p=p, d=dilation, g=g, act=False)
  35. def forward(self, x):
  36. identity = x
  37. out = torch.sigmoid(torch.add(identity, F.interpolate(self.k2(x), identity.size()[2:]))) # sigmoid(identity + k2)
  38. out = torch.mul(self.k3(x), out) # k3 * sigmoid(identity + k2)
  39. out = self.k4(out) # k4
  40. return out

3.2 在YOlO v9中的添加教程

阅读YOLOv9添加模块教程或使用下文操作

        1. 将YOLOv9工程中models下common.py文件中的最下行否则可能因类继承报错)增加模块的代码。

         2. 将YOLOv9工程中models下yolo.py文件中的第681行(可能因版本变化而变化)增加以下代码。

          RepNCSPELAN4, SPPELAN, RepNCSPELAN4SCConv1}:

3.3 运行配置文件

  1. # YOLOv9
  2. # Powered bu https://blog.csdn.net/StopAndGoyyy
  3. # parameters
  4. nc: 80 # number of classes
  5. #depth_multiple: 0.33 # model depth multiple
  6. depth_multiple: 1 # model depth multiple
  7. #width_multiple: 0.25 # layer channel multiple
  8. width_multiple: 1 # layer channel multiple
  9. #activation: nn.LeakyReLU(0.1)
  10. #activation: nn.ReLU()
  11. # anchors
  12. anchors: 3
  13. # YOLOv9 backbone
  14. backbone:
  15. [
  16. [-1, 1, Silence, []],
  17. # conv down
  18. [-1, 1, Conv, [64, 3, 2]], # 1-P1/2
  19. # conv down
  20. [-1, 1, Conv, [128, 3, 2]], # 2-P2/4
  21. # elan-1 block
  22. [-1, 1, RepNCSPELAN4SCConv1, [256, 128, 64, 1]], # 3
  23. # avg-conv down
  24. [-1, 1, ADown, [256]], # 4-P3/8
  25. # elan-2 block
  26. [-1, 1, RepNCSPELAN4, [512, 256, 128, 1]], # 5
  27. # avg-conv down
  28. [-1, 1, ADown, [512]], # 6-P4/16
  29. # elan-2 block
  30. [-1, 1, RepNCSPELAN4, [512, 512, 256, 1]], # 7
  31. # avg-conv down
  32. [-1, 1, ADown, [512]], # 8-P5/32
  33. # elan-2 block
  34. [-1, 1, RepNCSPELAN4, [512, 512, 256, 1]], # 9
  35. ]
  36. # YOLOv9 head
  37. head:
  38. [
  39. # elan-spp block
  40. [-1, 1, SPPELAN, [512, 256]], # 10
  41. # up-concat merge
  42. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  43. [[-1, 7], 1, Concat, [1]], # cat backbone P4
  44. # elan-2 block
  45. [-1, 1, RepNCSPELAN4, [512, 512, 256, 1]], # 13
  46. # up-concat merge
  47. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  48. [[-1, 5], 1, Concat, [1]], # cat backbone P3
  49. # elan-2 block
  50. [-1, 1, RepNCSPELAN4, [256, 256, 128, 1]], # 16 (P3/8-small)
  51. # avg-conv-down merge
  52. [-1, 1, ADown, [256]],
  53. [[-1, 13], 1, Concat, [1]], # cat head P4
  54. # elan-2 block
  55. [-1, 1, RepNCSPELAN4, [512, 512, 256, 1]], # 19 (P4/16-medium)
  56. # avg-conv-down merge
  57. [-1, 1, ADown, [512]],
  58. [[-1, 10], 1, Concat, [1]], # cat head P5
  59. # elan-2 block
  60. [-1, 1, RepNCSPELAN4, [512, 512, 256, 1]], # 22 (P5/32-large)
  61. # multi-level reversible auxiliary branch
  62. # routing
  63. [5, 1, CBLinear, [[256]]], # 23
  64. [7, 1, CBLinear, [[256, 512]]], # 24
  65. [9, 1, CBLinear, [[256, 512, 512]]], # 25
  66. # conv down
  67. [0, 1, Conv, [64, 3, 2]], # 26-P1/2
  68. # conv down
  69. [-1, 1, Conv, [128, 3, 2]], # 27-P2/4
  70. # elan-1 block
  71. [-1, 1, RepNCSPELAN4, [256, 128, 64, 1]], # 28
  72. # avg-conv down fuse
  73. [-1, 1, ADown, [256]], # 29-P3/8
  74. [[23, 24, 25, -1], 1, CBFuse, [[0, 0, 0]]], # 30
  75. # elan-2 block
  76. [-1, 1, RepNCSPELAN4, [512, 256, 128, 1]], # 31
  77. # avg-conv down fuse
  78. [-1, 1, ADown, [512]], # 32-P4/16
  79. [[24, 25, -1], 1, CBFuse, [[1, 1]]], # 33
  80. # elan-2 block
  81. [-1, 1, RepNCSPELAN4, [512, 512, 256, 1]], # 34
  82. # avg-conv down fuse
  83. [-1, 1, ADown, [512]], # 35-P5/32
  84. [[25, -1], 1, CBFuse, [[2]]], # 36
  85. # elan-2 block
  86. [-1, 1, RepNCSPELAN4, [512, 512, 256, 1]], # 37
  87. # detection head
  88. # detect
  89. [[31, 34, 37, 16, 19, 22], 1, DualDDetect, [nc]], # DualDDetect(A3, A4, A5, P3, P4, P5)
  90. ]

3.4 训练过程


欢迎关注!


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

闽ICP备14008679号