当前位置:   article > 正文

[YOLOv7/YOLOv5系列算法改进NO.4]添加ECA通道注意力机制_在yolov7中引入eca-net

在yolov7中引入eca-net

 前      言

作为当前先进的深度学习目标检测算法YOLOv5,已经集合了大量的trick,但是在处理一些复杂背景问题的时候,还是容易出现错漏检的问题。此后的系列文章,将重点对YOLOv5的如何改进进行详细的介绍,目的是为了给那些搞科研的同学需要创新点或者搞工程项目的朋友需要达到更好的效果提供自己的微薄帮助和参考。

一、解决问题

加入ECA注意力机制,可以让网络更加关注待检测目标,提高检测效果。该注意力机制为一篇CVPR2020上对通道注意力进行改进的文章---ECANet,ECANet主要对SENet模块进行了一些改进,提出了一种不降维的局部跨信道交互策略(ECA模块)和自适应选择一维卷积核大小的方法,从而实现了性能上的提优。

 二、YOLOv7添加方法

第一步:确定添加的位置,作为即插即用的注意力模块,可以添加到YOLOv7网络中的任何地方。

第二步:common.py构建 Eca_layer模块。

  1. class Eca_layer(nn.Module):
  2. """Constructs a ECA module.
  3. Args:
  4. channel: Number of channels of the input feature map
  5. k_size: Adaptive selection of kernel size
  6. """
  7. def __init__(self, channel, k_size=3):
  8. super(Eca_layer, self).__init__()
  9. self.avg_pool = nn.AdaptiveAvgPool2d(1)
  10. self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1) // 2, bias=False)
  11. self.sigmoid = nn.Sigmoid()
  12. def forward(self, x):
  13. # x: input features with shape [b, c, h, w]
  14. b, c, h, w = x.size()
  15. # feature descriptor on the global spatial information
  16. y = self.avg_pool(x)
  17. # Two different branches of ECA module
  18. y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)
  19. # Multi-scale information fusion
  20. y = self.sigmoid(y)
  21. return x * y.expand_as(x)

第三步:yolo.py中注册Eca_layer模块

  1. elif m is Eca_layer:
  2. channel,k_size=args[0],args[1]
  3. channel = make_divisible(channel * gw, 8) if channel != no else channel
  4. args = [channel, k_size]

第四步:修改YOLOv7.yaml文件,本文以修改backbone为例。

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

根据yaml文件画出网络结构图如下所示:

三、YOLOv5添加方法

前三步同YOLOv7方法。

第四步:修改yaml文件,本文以修改backbone为例,将原C3模块后加入该模块。

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