当前位置:   article > 正文

注意力机制_scse注意力机制

scse注意力机制

整理了以下几个方便使用并且对提高模型效果有一定效果的注意力机制,但对于不同的模型和不同的数据集,效果不同。

  • Squeeze and Excitation(SE)

  • Efficient Channel Attention(ECA)

  • Coordinate Attention(CA)

  • Convolutional Block Attention Module(CBAM

  • Concurrent Spatial and Channel Squeeze & Excitation(SCSE)

Squeeze and Excitation(SE)

结构:

SE模块是给提取到的特征每个通道乘以不同的权重。

  1. Squeeze:将特征进行全局平均池化,得到1x1xC的向量。

  1. Excitation:为了提升模型的泛化能力,对向量进行两次全连接操作,第一次全连接操作将其通道数进行缩小,第二次全连接操作将通道数还原,最后将得到的向量映射为0至1范围内的小数,对应不同的通道乘以不同的权重。

(比率reduction不是固定的,可以根据不同的模型,自行进行调整,原文分别将r设置为2、4、8、16、32,实验结果在比率为16时既保证了准确率,也保证了模型的复杂度。)

代码:

  1. class SEAttention(nn.Module):
  2. def __init__(self, channel=512,reduction=16):
  3. super().__init__()
  4. self.avg_pool = nn.AdaptiveAvgPool2d(1)
  5. self.fc = nn.Sequential(
  6. nn.Linear(channel, channel // reduction, bias=False),
  7. nn.ReLU(inplace=True),
  8. nn.Linear(channel // reduction, channel, bias=False),
  9. nn.Sigmoid()
  10. )
  11. def init_weights(self):
  12. for m in self.modules():
  13. if isinstance(m, nn.Conv2d):
  14. init.kaiming_normal_(m.weight, mode='fan_out')
  15. if m.bias is not None:
  16. init.constant_(m.bias, 0)
  17. elif isinstance(m, nn.BatchNorm2d):
  18. init.constant_(m.weight, 1)
  19. init.constant_(m.bias, 0)
  20. elif isinstance(m, nn.Linear):
  21. init.normal_(m.weight, std=0.001)
  22. if m.bias is not None:
  23. init.constant_(m.bias, 0)
  24. def forward(self, x):
  25. b, c, _, _ = x.size()
  26. y = self.avg_pool(x).view(b, c)
  27. y = self.fc(y).view(b, c, 1, 1)
  28. return x * y.expand_as(x)
  29. if __name__ == '__main__':
  30. input = torch.randn((4, 320, 4, 4))
  31. model = SEAttention(320, 16)  #输入参数为输入通道数以及比率
  32. output = model(input)

论文:

https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8701503

(IEEE2019)


Efficient Channel Attention(ECA)

结构:

ECA模块是SE模块的变体,由于SE模块中将得到的权重输入两层全连接层,会将通道的数量减小之后再还原,改论文通过一层全连接层与两层全连接层分别进行实验对比得到:单层的全连接层效果优于双层全连接层,因此要避免通道数量的减少。与此同时,ECA模块还提出跨通道交互,学习相邻的k个通道之间的信息,相比于对角矩阵可以更好地学习通道间的信息,相比于普通的CxC的全矩阵,可以减少参数量。

ECA模块先对输入的特征图进行全局平均池化,得到一个1x1xC的向量,对其进行卷积,维持其维数不变化,最后将得到向量映射到0到1之间的小数作为通道权重,与原特征图进行相乘。

代码:

  1. class ECA_Block(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_Block, 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. # feature descriptor on the global spatial information
  14. y = self.avg_pool(x)
  15. # Two different branches of ECA module
  16. y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)
  17. # Multi-scale information fusion
  18. y = self.sigmoid(y)
  19. return x * y.expand_as(x)
  20. if __name__ == '__main__':
  21. input = torch.randn((4, 320, 4, 4))
  22. model = ECA_Block(320)
  23. output = model(input)

论文:

https://arxiv.org/pdf/1910.03151.pdf


Coordinate Attention(CA)

结构:

CA模块既考虑到通道之间的关系,也考虑了位置间的相关性, 在SE模块的基础上进行改进。

  1. Coordinate Information Embedding:分别对特征进行长度和宽度的平均池化得到一对跟方向有关的特征图CxHx1以及Cx1xW。

  1. Coordinate Attention Generation:,将CxHx1的特征图进行转置,得到的Cx1xH与Cx1xW进行拼接,Cx1x(H+W),对其卷积成(C/r)x1x(H+W)并映射到0至1之间的小数,最后将特征图还原成跟原来尺度一致的两个特征图,分别通过卷积把通道数还原成C,分别通过sigmoid,将得到的权重分别乘以特征图。

代码:

  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. class h_sigmoid(nn.Module):
  5. def __init__(self, inplace=True):
  6. super(h_sigmoid, self).__init__()
  7. self.relu = nn.ReLU6(inplace=inplace)
  8. def forward(self, x):
  9. return self.relu(x + 3) / 6
  10. class h_swish(nn.Module):
  11. def __init__(self, inplace=True):
  12. super(h_swish, self).__init__()
  13. self.sigmoid = h_sigmoid(inplace=inplace)
  14. def forward(self, x):
  15. return x * self.sigmoid(x)
  16. class CoordAtt(nn.Module):
  17. def __init__(self, inp, oup, reduction=32):
  18. super(CoordAtt, self).__init__()
  19. self.pool_h = nn.AdaptiveAvgPool2d((None, 1))
  20. self.pool_w = nn.AdaptiveAvgPool2d((1, None))
  21. mip = max(8, inp // reduction)
  22. self.conv1 = nn.Conv2d(inp, mip, kernel_size=1, stride=1, padding=0)
  23. self.bn1 = nn.BatchNorm2d(mip)
  24. self.act = h_swish()
  25. self.conv_h = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)
  26. self.conv_w = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)
  27. def forward(self, x):
  28. identity = x
  29. n,c,h,w = x.size()
  30. x_h = self.pool_h(x)
  31. x_w = self.pool_w(x).permute(0, 1, 3, 2)
  32. y = torch.cat([x_h, x_w], dim=2)
  33. y = self.conv1(y)
  34. y = self.bn1(y)
  35. y = self.act(y)
  36. x_h, x_w = torch.split(y, [h, w], dim=2)
  37. x_w = x_w.permute(0, 1, 3, 2)
  38. a_h = self.conv_h(x_h).sigmoid()
  39. a_w = self.conv_w(x_w).sigmoid()
  40. out = identity * a_w * a_h
  41. return out
  42. if __name__ == '__main__':
  43. input = torch.randn((4, 320, 4, 4))
  44. model = CoordAtt(320, 320, 16)      #输入参数为输入通道数、输出通道数以及比率
  45. output = model(input)

论文:

https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=9577301


Convolutional Block Attention Module(CBAM)

结构:

CBAM模块不仅有通道注意力还有空间注意力,分别将特征图的各个通道乘以不同的权重以及对各个位置乘以不同的权重:

  1. Channel Attention Module:对特征进行最大池化以及平均池化,得到Cx1x1的向量,将这两个向量分别输入共享权重的多层感知机后相加,将其映射到0至1之间的小数,得到通道注意力的权重,与原特征图相乘。

  1. Spatial Attention Module:分别对特征图进行最大池化和平均池化,将得到的两个1xHxW的矩阵拼接成2xHxW的特征图,对其进行卷积,通道数降为1,将矩阵映射成0至1之间的小数,生成空间注意力权重,与特征图相乘。

代码:

  1. class channel_attention(nn.Module):
  2. def __init__(self, channel, ratio=16):
  3. super(channel_attention, self).__init__()
  4. self.max_pool = nn.AdaptiveMaxPool2d(1)
  5. self.avg_pool = nn.AdaptiveAvgPool2d(1)
  6. self.fc = nn.Sequential(
  7. nn.Linear(channel, channel // ratio, bias=False),
  8. nn.ReLU(),
  9. nn.Linear(channel // ratio, channel, bias=False)
  10. )
  11. self.sigmoid = nn.Sigmoid()
  12. def forward(self, x):
  13. b, c, h, w, = x.size()
  14. max_pool_out = self.max_pool(x).view(x.size(0), -1)
  15. avg_pool_out = self.avg_pool(x).view(x.size(0), -1)
  16. max_fc_out = self.fc(max_pool_out)
  17. avg_fc_out = self.fc(avg_pool_out)
  18. out = max_fc_out + avg_fc_out
  19. out = self.sigmoid(out).view(b, c, 1, 1)
  20. # print(out)
  21. return out * x
  22. class spatial_attention(nn.Module):
  23. def __init__(self, kernel_size=7):
  24. super(spatial_attention, self).__init__()
  25. padding = kernel_size // 2
  26. self.conv = nn.Conv2d(2, 1, kernel_size, stride=1, padding=padding, bias=False)
  27. self.sigmoid = nn.Sigmoid()
  28. def forward(self, x):
  29. max_pool_out, _ = torch.max(x, dim=1, keepdim=True)
  30. mean_pool_out = torch.mean(x, dim=1, keepdim=True)
  31. pool_out = torch.cat([max_pool_out, mean_pool_out], dim=1)
  32. out = self.conv(pool_out)
  33. out = self.sigmoid(out)
  34. # print(out)
  35. return out * x
  36. class CBAM_Block(nn.Module):
  37. def __init__(self, channel, ratio=16, kernel_size=7):
  38. super(CBAM_Block, self).__init__()
  39. self.channel_attention = channel_attention(channel, ratio=ratio)
  40. self.spatial_attention = spatial_attention(kernel_size=kernel_size)
  41. def forward(self, x):
  42. x = self.channel_attention(x)
  43. x = self.spatial_attention(x)
  44. return x
  45. if __name__ == '__main__':
  46. input = torch.randn((4, 320, 4, 4))
  47. model = CBAM_Block(320)
  48. output = model(input)

论文:

https://arxiv.org/pdf/1807.06521.pdf


Concurrent Spatial and Channel Squeeze & Excitation(SCSE)

结构:

SCSE模块既包含空间注意力又包含通道注意力,分别将不同注意力机制得到的权重与特征图相乘,最后相加,与CBAM的串联式相比,SCSE模块是将空间注意力与通道注意力并联式组合:

  1. Spatial Squeeze and Channel Excitation Block (cSE):

通道注意力首先将特征图平均池化得到1x1xC的向量,通过两层卷积层对通道进行放缩,然后还原,映射到0到1之间的小数得到通道注意力的权重,与原特征图相乘。

  1. Channel Squeeze and Spatial Excitation Block (sSE):

空间注意力将特征图进行卷积,通道数变为1,得到1xHxW的位置信息权重,分别于对应位置的特征相乘。

代码:

  1. class SCSEModule(nn.Module):
  2. def __init__(self, in_channels, reduction):
  3. super().__init__()
  4. self.cSE = nn.Sequential(
  5. nn.AdaptiveAvgPool2d(1),
  6. nn.Conv2d(in_channels, in_channels // reduction, 1),
  7. nn.ReLU(inplace=True),
  8. nn.Conv2d(in_channels // reduction, in_channels, 1),
  9. nn.Sigmoid(),
  10. )
  11. self.sSE = nn.Sequential(nn.Conv2d(in_channels, 1, 1), nn.Sigmoid())
  12. def forward(self, x):
  13. return x * self.cSE(x) + x * self.sSE(x)
  14. if __name__ == '__main__':
  15. input = torch.randn((4, 320, 4, 4))
  16. model = SCSEModule(320,12)
  17. output = model(input)

论文:

https://arxiv.org/pdf/1803.02579.pdf


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

闽ICP备14008679号