学习资源站

17-添加EMA注意力机制(ICASSP2023_实测涨点)_fasternet用ema注意力机制

YOLOv5改进系列(16)——添加EMA注意力机制(ICASSP2023|实测涨点)

 🚀一、EMA介绍  

1.1 简介

       通道或空间的显著有效性注意机制对产生更多可辨识的特征表示的显著效果,然而用通道降维对跨通道关系进行建模关系,可能会给提取深层视觉表征带来副作用。比如CA注意力机制虽对精度有一定的提升但是由于需要对整个特征图进行注意力权重的计算,因此需要额外的计算导致消耗资源,另外无法捕捉通道之间长距离的依赖关系
      深圳的航天科工2023年6月提出的新型的高效多尺度注意力EMA模块,该模块首先将部分通道维度重塑为批量维度,以避免通用卷积进行某种形式的降维;接着在每个并行子网络中构建局部的跨通道交互利用一种新的跨空间学习方法融合两个并行子网络的输出特征图,设计了一个多尺度并行子网络来建立短和长依赖关系。EMA的网络结构图如图所示。


🚀二、在Neck端添加EMA注意力机制方法

2.1 添加顺序 

(1)models/common.py    -->  加入新增的网络结构

(2)     models/yolo.py       -->  设定网络结构的传参细节,将EMA类名加入其中。(当新的自定义模块中存在输入输出维度时,要使用qw调整输出维度)
(3) models/yolov5*.yaml  -->  新建一个文件夹,如yolov5s_EMA.yaml,修改现有模型结构配置文件。(当引入新的层时,要修改后续的结构中的from参数)
(4)         train.py                -->  修改‘--cfg’默认参数,训练时指定模型结构配置文件 


2.2 具体添加步骤 

第①步:在common.py中添加EMA模块

第一步,通过与CA类似的处理,将两个编码特征连接在图像高度方向上,并使其共享相同的1×1卷积而不会降低1x1分支的维数。在将1x1卷积的输出分解为2个向量后,使用两个非线性Sigmid函数来拟合线性卷积上的2D二进制分布。为了在1×1分支中的两个平行路线之间实现不同的跨通道交互特征,通过简单的乘法将每组内的两个通道注意力图聚合在一起。

第二步,3×3分支通过卷积捕获局部跨通道交互,以扩大特征空间。

这样,EMA不仅对通道间信息进行编码以调整不同通道的重要性,而目将精确的空间结构信息保存到通道中。

将下面的EMA代码复制粘贴到common.py文件的末尾。

  1. #EMA
  2. class EMA(nn.Module):
  3. def __init__(self, channels, factor=8):
  4. super(EMA, self).__init__()
  5. self.groups = factor # 分组因子
  6. assert channels // self.groups > 0
  7. self.softmax = nn.Softmax(-1) #softmax操作
  8. self.agp = nn.AdaptiveAvgPool2d((1, 1)) # 1×1平均池化层
  9. self.pool_h = nn.AdaptiveAvgPool2d((None, 1)) # X平均池化层 h=1
  10. self.pool_w = nn.AdaptiveAvgPool2d((1, None)) # Y平均池化层 w=1
  11. self.gn = nn.GroupNorm(channels // self.groups, channels // self.groups) # 分组操作
  12. self.conv1x1 = nn.Conv2d(channels // self.groups, channels // self.groups, kernel_size=1, stride=1, padding=0) # 1×1卷积分支
  13. self.conv3x3 = nn.Conv2d(channels // self.groups, channels // self.groups, kernel_size=3, stride=1, padding=1) # 3×3卷积分支
  14. def forward(self, x):
  15. b, c, h, w = x.size()
  16. group_x = x.reshape(b * self.groups, -1, h, w) # b*g,c//g,h,w
  17. x_h = self.pool_h(group_x) # 得到平均池化之后的h
  18. x_w = self.pool_w(group_x).permute(0, 1, 3, 2) # 得到平均池化之后的w
  19. hw = self.conv1x1(torch.cat([x_h, x_w], dim=2)) # 先拼接,然后送入1×1卷积
  20. x_h, x_w = torch.split(hw, [h, w], dim=2)
  21. x1 = self.gn(group_x * x_h.sigmoid() * x_w.permute(0, 1, 3, 2).sigmoid())
  22. x2 = self.conv3x3(group_x) # 3×3卷积分支
  23. x11 = self.softmax(self.agp(x1).reshape(b * self.groups, -1, 1).permute(0, 2, 1))
  24. x12 = x2.reshape(b * self.groups, c // self.groups, -1) # b*g, c//g, hw
  25. x21 = self.softmax(self.agp(x2).reshape(b * self.groups, -1, 1).permute(0, 2, 1))
  26. x22 = x1.reshape(b * self.groups, c // self.groups, -1) # b*g, c//g, hw
  27. weights = (torch.matmul(x11, x12) + torch.matmul(x21, x22)).reshape(b * self.groups, 1, h, w)
  28. return (group_x * weights.sigmoid()).reshape(b, c, h, w)

如下图所示:


第②步:在yolo.py文件里的parse_model函数加入类名

首先找到yolo.py里面parse_model函数的这一行。

 然后把刚才加入的类EMA添加到这个注册表里面:


第③步:创建自定义的yaml文件 

首先在models文件夹下复制yolov5s.yaml 文件,粘贴并重命名为 yolov5s_EMA.yaml 

接着修改  yolov5s_EMA.yaml ,将EMA模块加到我们想添加的位置。 这里先演示的是在Neck端添加。

复制下面代码,粘贴到刚才新创建的yaml文件。

  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. # Parameters
  3. nc: 1 # number of classes
  4. depth_multiple: 0.33 # model depth multiple
  5. width_multiple: 0.50 # layer channel multiple
  6. anchors:
  7. - [10,13, 16,30, 33,23] # P3/8
  8. - [30,61, 62,45, 59,119] # P4/16
  9. - [116,90, 156,198, 373,326] # P5/32
  10. # YOLOv5 v6.0 backbone
  11. backbone:
  12. # [from, number, module, args]
  13. [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
  14. [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
  15. [-1, 3, C3, [128]],
  16. [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
  17. [-1, 6, C3, [256]],
  18. [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
  19. [-1, 9, C3, [512]],
  20. [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
  21. [-1, 3, C3, [1024]],
  22. [-1, 1, SPPF, [1024, 5]], # 9
  23. ]
  24. # YOLOv5 v6.0 head
  25. head:
  26. [[-1, 1, Conv, [512, 1, 1]],
  27. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  28. [[-1, 6], 1, Concat, [1]], # cat backbone P4
  29. [-1, 3, C3, [512, False]], # 13
  30. [-1, 1, Conv, [256, 1, 1]],
  31. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  32. [[-1, 4], 1, Concat, [1]], # cat backbone P3
  33. [-1, 3, C3, [256, False]], # 17 (P3/8-small)
  34. [-1, 1, EMA, [256]], # 加入到小目标层后
  35. [-1, 1, Conv, [256, 3, 2]],
  36. [[-1, 14], 1, Concat, [1]], # cat head P4
  37. [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
  38. [-1, 1, EMA, [512]], # 加入到中目标层后
  39. [-1, 1, Conv, [512, 3, 2]],
  40. [[-1, 10], 1, Concat, [1]], # cat head P5
  41. [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
  42. [-1, 1, EMA, [1024]], # 加入到大目标层后
  43. [[18, 22, 26], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  44. ]

第④步:验证是否加入成功

运行yolo.py

这样就修改成功了~


第⑤步:修改train.py中 ‘--cfg’默认参数

我们先找到 train.py 文件的parse_opt函数,然后将第二行‘--cfg’default改为yolov5s_EMA.yaml,然后就可以开始训练啦~


 🚀三、在C3中添加EMA注意力机制方法

3.1 具体添加步骤

第①步:在common.py中添加C3_EMA模块

将下面的代码复制粘贴到common.py文件的末尾。

  1. #EMA
  2. class EMA(nn.Module):
  3. def __init__(self, channels, factor=8):
  4. super(EMA, self).__init__()
  5. self.groups = factor # 分组率
  6. assert channels // self.groups > 0
  7. self.softmax = nn.Softmax(-1) # Softmax
  8. self.agp = nn.AdaptiveAvgPool2d((1, 1)) # 平均池化层
  9. self.pool_h = nn.AdaptiveAvgPool2d((None, 1)) # x平均池化层 h=1
  10. self.pool_w = nn.AdaptiveAvgPool2d((1, None)) # y平均池化层 w=1
  11. self.gn = nn.GroupNorm(channels // self.groups, channels // self.groups) # 分组操作
  12. self.conv1x1 = nn.Conv2d(channels // self.groups, channels // self.groups, kernel_size=1, stride=1, padding=0) # 1×1卷积分支
  13. self.conv3x3 = nn.Conv2d(channels // self.groups, channels // self.groups, kernel_size=3, stride=1, padding=1) # 3×3卷积分支
  14. def forward(self, x):
  15. b, c, h, w = x.size()
  16. group_x = x.reshape(b * self.groups, -1, h, w) # b*g,c//g,h,w
  17. x_h = self.pool_h(group_x)
  18. x_w = self.pool_w(group_x).permute(0, 1, 3, 2)
  19. hw = self.conv1x1(torch.cat([x_h, x_w], dim=2))
  20. x_h, x_w = torch.split(hw, [h, w], dim=2)
  21. x1 = self.gn(group_x * x_h.sigmoid() * x_w.permute(0, 1, 3, 2).sigmoid())
  22. x2 = self.conv3x3(group_x)
  23. x11 = self.softmax(self.agp(x1).reshape(b * self.groups, -1, 1).permute(0, 2, 1))
  24. x12 = x2.reshape(b * self.groups, c // self.groups, -1) # b*g, c//g, hw
  25. x21 = self.softmax(self.agp(x2).reshape(b * self.groups, -1, 1).permute(0, 2, 1))
  26. x22 = x1.reshape(b * self.groups, c // self.groups, -1) # b*g, c//g, hw
  27. weights = (torch.matmul(x11, x12) + torch.matmul(x21, x22)).reshape(b * self.groups, 1, h, w)
  28. return (group_x * weights.sigmoid()).reshape(b, c, h, w)
  29. class C3_EMA3(nn.Module):
  30. # CSP Bottleneck with 3 convolutions
  31. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  32. super().__init__()
  33. c_ = int(c2 * e) # hidden channels
  34. self.cv1 = Conv(c1, c_, 1, 1)
  35. self.cv2 = Conv(c1, c_, 1, 1)
  36. self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2)
  37. self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
  38. self.m1 = nn.ModuleList([EMA(2 * c_)]) # 添加在最后一个卷积之前
  39. def forward(self, x):
  40. return self.cv3(self.m1[0](torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1)))
  41. class C3_EMA2(nn.Module):
  42. # CSP Bottleneck with 3 convolutions
  43. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  44. super().__init__()
  45. c_ = int(c2 * e) # hidden channels
  46. self.cv1 = Conv(c1, c_, 1, 1)
  47. self.cv2 = Conv(c1, c_, 1, 1)
  48. self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2)
  49. self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
  50. self.m1 = nn.ModuleList([EMA(c1)]) # 添加在最后一个卷积之前
  51. def forward(self, x):
  52. return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(self.m1[0](x))), 1))
  53. class C3_EMA1(nn.Module):
  54. # CSP Bottleneck with 3 convolutions
  55. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  56. super().__init__()
  57. c_ = int(c2 * e) # hidden channels
  58. self.cv1 = Conv(c1, c_, 1, 1)
  59. self.cv2 = Conv(c1, c_, 1, 1)
  60. self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2)
  61. self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
  62. self.m1 = nn.ModuleList([EMA(c_)]) # 添加在最后一个卷积之前
  63. def forward(self, x):
  64. return self.cv3(torch.cat((self.m(self.m1[0](self.cv1(x))), self.cv2(x)), 1))

第②步:在yolo.py文件里的parse_model函数加入类名


第③步:创建自定义的yaml文件 

  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. # Parameters
  3. nc: 80 # number of classes
  4. depth_multiple: 0.33 # model depth multiple
  5. width_multiple: 0.50 # layer channel multiple
  6. anchors:
  7. - [10,13, 16,30, 33,23] # P3/8
  8. - [30,61, 62,45, 59,119] # P4/16
  9. - [116,90, 156,198, 373,326] # P5/32
  10. # YOLOv5 v6.0 backbone
  11. backbone:
  12. # [from, number, module, args]
  13. [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
  14. [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
  15. [-1, 3, C3_EMA1, [128]],
  16. [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
  17. [-1, 6, C3, [256]],
  18. [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
  19. [-1, 9, C3, [512]],
  20. [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
  21. [-1, 3, C3, [1024]],
  22. [-1, 1, SPPF, [1024, 5]], # 9
  23. ]
  24. # YOLOv5 v6.0 head
  25. head:
  26. [[-1, 1, Conv, [512, 1, 1]],
  27. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  28. [[-1, 6], 1, Concat, [1]], # cat backbone P4
  29. [-1, 3, C3, [512, False]], # 13
  30. [-1, 1, Conv, [256, 1, 1]],
  31. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  32. [[-1, 4], 1, Concat, [1]], # cat backbone P3
  33. [-1, 3, C3, [256, False]], # 17 (P3/8-small)
  34. [-1, 1, Conv, [256, 3, 2]],
  35. [[-1, 14], 1, Concat, [1]], # cat head P4
  36. [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
  37. [-1, 1, Conv, [512, 3, 2]],
  38. [[-1, 10], 1, Concat, [1]], # cat head P5
  39. [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
  40. [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  41. ]

第④步:验证是否加入成功

运行yolo.py


第⑤步:修改train.py中 ‘--cfg’默认参数

这步同上,就不再过多叙述了~


PS:

(1)在我的数据集上,使用EMA比CA涨了1.3,还是很不错的。

CA:

EMA:

 (2)在我的数据集上,两种方法精度差不多,但第一种比第二种方法精度略高一丝。