当前位置:   article > 正文

YOLOv9独家原创改进|使用可改变核卷积AKConv改进RepNCSPELAN4

repncspelan4


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


一、改进点介绍

        AKConv是一种具有任意数量的参数和任意采样形状的可变卷积核,对不规则特征有更好的提取效果。

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


二、RepNCSPELAN4-AKConv模块详解

 2.1 模块简介

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


三、 RepNCSPELAN4-AKConv模块使用教程

3.1 RepNCSPELAN4-AKConv模块的代码

  1. class RepNCSP_AKConv(RepNCSP):
  2. # CSP Bottleneck with 3 convolutions
  3. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  4. super().__init__(c1, c2, n, shortcut, g, e)
  5. c_ = int(c2 * e) # hidden channels
  6. self.cv1 = AKConv(c1, c_)
  7. self.cv2 = AKConv(c1, c_)
  8. self.cv3 = AKConv(2 * c_, c2) # optional act=FReLU(c2)
  9. self.m = nn.Sequential(*(RepNBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
  10. class RepNCSPELAN4AKConv1(RepNCSPELAN4):
  11. def __init__(self, c1, c2, c3, c4, c5=1): # ch_in, ch_out, number, shortcut, groups, expansion
  12. super().__init__(c1, c2, c3, c4, c5)
  13. self.cv1 = Conv(c1, c3, 1, 1)
  14. self.cv2 = nn.Sequential(RepNCSP_AKConv(c3//2, c4, c5), Conv(c4, c4, 3, 1))
  15. self.cv3 = nn.Sequential(RepNCSP_AKConv(c4, c4, c5), Conv(c4, c4, 3, 1))
  16. self.cv4 = AKConv(c3+(2*c4), c2, 1, 1)
  17. from einops import rearrange
  18. class AKConv(nn.Module):
  19. def __init__(self, inc, outc, num_param=5, stride=1):
  20. """
  21. 初始化参数说明:
  22. inc: 输入通道数, outc: 输出通道数, num_param:(卷积核)参数量, stride = 1:卷积步长默认为1, bias = None:默认无偏执
  23. """
  24. super(AKConv, self).__init__()
  25. self.num_param = num_param
  26. self.stride = stride
  27. self.conv = Conv(inc, outc, k=(num_param, 1), s=(num_param, 1) )
  28. self.p_conv = nn.Conv2d(inc, 2 * num_param, kernel_size=3, padding=1, stride=stride)
  29. nn.init.constant_(self.p_conv.weight, 0)
  30. self.p_conv.register_full_backward_hook(self._set_lr)
  31. @staticmethod
  32. def _set_lr(module, grad_input, grad_output):
  33. grad_input = (grad_input[i] * 0.1 for i in range(len(grad_input)))
  34. grad_output = (grad_output[i] * 0.1 for i in range(len(grad_output)))
  35. def forward(self, x):
  36. # N is num_param.
  37. offset = self.p_conv(x)
  38. dtype = offset.data.type()
  39. N = offset.size(1) // 2
  40. # (b, 2N, h, w)
  41. p = self._get_p(offset, dtype)
  42. # (b, h, w, 2N)
  43. p = p.contiguous().permute(0, 2, 3, 1)
  44. q_lt = p.detach().floor()
  45. q_rb = q_lt + 1
  46. q_lt = torch.cat([torch.clamp(q_lt[..., :N], 0, x.size(2) - 1), torch.clamp(q_lt[..., N:], 0, x.size(3) - 1)],
  47. dim=-1).long()
  48. q_rb = torch.cat([torch.clamp(q_rb[..., :N], 0, x.size(2) - 1), torch.clamp(q_rb[..., N:], 0, x.size(3) - 1)],
  49. dim=-1).long()
  50. q_lb = torch.cat([q_lt[..., :N], q_rb[..., N:]], dim=-1)
  51. q_rt = torch.cat([q_rb[..., :N], q_lt[..., N:]], dim=-1)
  52. # clip p
  53. p = torch.cat([torch.clamp(p[..., :N], 0, x.size(2) - 1), torch.clamp(p[..., N:], 0, x.size(3) - 1)], dim=-1)
  54. # bilinear kernel (b, h, w, N)
  55. g_lt = (1 + (q_lt[..., :N].type_as(p) - p[..., :N])) * (1 + (q_lt[..., N:].type_as(p) - p[..., N:]))
  56. g_rb = (1 - (q_rb[..., :N].type_as(p) - p[..., :N])) * (1 - (q_rb[..., N:].type_as(p) - p[..., N:]))
  57. g_lb = (1 + (q_lb[..., :N].type_as(p) - p[..., :N])) * (1 - (q_lb[..., N:].type_as(p) - p[..., N:]))
  58. g_rt = (1 - (q_rt[..., :N].type_as(p) - p[..., :N])) * (1 + (q_rt[..., N:].type_as(p) - p[..., N:]))
  59. # resampling the features based on the modified coordinates.
  60. x_q_lt = self._get_x_q(x, q_lt, N)
  61. x_q_rb = self._get_x_q(x, q_rb, N)
  62. x_q_lb = self._get_x_q(x, q_lb, N)
  63. x_q_rt = self._get_x_q(x, q_rt, N)
  64. # bilinear
  65. x_offset = g_lt.unsqueeze(dim=1) * x_q_lt + \
  66. g_rb.unsqueeze(dim=1) * x_q_rb + \
  67. g_lb.unsqueeze(dim=1) * x_q_lb + \
  68. g_rt.unsqueeze(dim=1) * x_q_rt
  69. x_offset = self._reshape_x_offset(x_offset, self.num_param)
  70. out = self.conv(x_offset)
  71. return out
  72. # generating the inital sampled shapes for the AKConv with different sizes.
  73. def _get_p_n(self, N, dtype):
  74. base_int = round(math.sqrt(self.num_param))
  75. row_number = self.num_param // base_int
  76. mod_number = self.num_param % base_int
  77. p_n_x, p_n_y = torch.meshgrid(
  78. torch.arange(0, row_number),
  79. torch.arange(0, base_int), indexing='xy')
  80. p_n_x = torch.flatten(p_n_x)
  81. p_n_y = torch.flatten(p_n_y)
  82. if mod_number > 0:
  83. mod_p_n_x, mod_p_n_y = torch.meshgrid(
  84. torch.arange(row_number, row_number + 1),
  85. torch.arange(0, mod_number), indexing='xy')
  86. mod_p_n_x = torch.flatten(mod_p_n_x)
  87. mod_p_n_y = torch.flatten(mod_p_n_y)
  88. p_n_x, p_n_y = torch.cat((p_n_x, mod_p_n_x)), torch.cat((p_n_y, mod_p_n_y))
  89. p_n = torch.cat([p_n_x, p_n_y], 0)
  90. p_n = p_n.view(1, 2 * N, 1, 1).type(dtype)
  91. return p_n
  92. # no zero-padding
  93. def _get_p_0(self, h, w, N, dtype):
  94. p_0_x, p_0_y = torch.meshgrid(
  95. torch.arange(0, h * self.stride, self.stride),
  96. torch.arange(0, w * self.stride, self.stride), indexing='xy')
  97. p_0_x = torch.flatten(p_0_x).view(1, 1, h, w).repeat(1, N, 1, 1)
  98. p_0_y = torch.flatten(p_0_y).view(1, 1, h, w).repeat(1, N, 1, 1)
  99. p_0 = torch.cat([p_0_x, p_0_y], 1).type(dtype)
  100. return p_0
  101. def _get_p(self, offset, dtype):
  102. N, h, w = offset.size(1) // 2, offset.size(2), offset.size(3)
  103. # (1, 2N, 1, 1)
  104. p_n = self._get_p_n(N, dtype)
  105. # (1, 2N, h, w)
  106. p_0 = self._get_p_0(h, w, N, dtype)
  107. p = p_0 + p_n + offset
  108. return p
  109. def _get_x_q(self, x, q, N):
  110. b, h, w, _ = q.size()
  111. padded_w = x.size(3)
  112. c = x.size(1)
  113. # (b, c, h*w)
  114. x = x.contiguous().view(b, c, -1)
  115. # (b, h, w, N)
  116. index = q[..., :N] * padded_w + q[..., N:] # offset_x*w + offset_y
  117. # (b, c, h*w*N)
  118. index = index.contiguous().unsqueeze(dim=1).expand(-1, c, -1, -1, -1).contiguous().view(b, c, -1)
  119. x_offset = x.gather(dim=-1, index=index).contiguous().view(b, c, h, w, N)
  120. return x_offset
  121. # Stacking resampled features in the row direction.
  122. @staticmethod
  123. def _reshape_x_offset(x_offset, num_param):
  124. b, c, h, w, n = x_offset.size()
  125. x_offset = rearrange(x_offset, 'b c h w n -> b c (h n) w')
  126. return x_offset

3.2 在YOlO v9中的添加教程

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

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

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

            RepNCSPELAN4, SPPELAN, RepNCSPELAN4AKConv1}:

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, RepNCSPELAN4AKConv1, [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/我家自动化/article/detail/393603
推荐阅读
相关标签
  

闽ICP备14008679号