改进版ASPP(2):ASPP模块中参加CBAM(卷积留意力模块),即CBAM_ASPP

[复制链接]
查看612 | 回复0 | 2023-8-23 11:59:18 | 显示全部楼层 |阅读模式
1、ASPP模子结构


空洞空间卷积池化金字塔(atrous spatial pyramid pooling (ASPP))通过对于输入的特征以差异的采样率举行采样,即从差异尺度提取输入特征,然后将所获取的特征举行融合,得到最终的特征提取结果。
2、CBAM结构



CBAM包罗CAM(Channel Attention Module)和SAM(Spartial Attention Module)两个子模块,分别在通道上和空间上添加留意力机制。如许不但可以节流参数和盘算力,而且包管了其可以或许做为即插即用的模块集成到现有的网络架构中去。
CBAM代码如下:
  1. import torch
  2. import torch.nn as nn
  3. class CBAMLayer(nn.Module):
  4.     def __init__(self, channel, reduction=16, spatial_kernel=7):
  5.         super(CBAMLayer, self).__init__()
  6.         # channel attention 压缩H,W为1
  7.         self.max_pool = nn.AdaptiveMaxPool2d(1)
  8.         self.avg_pool = nn.AdaptiveAvgPool2d(1)
  9.         # shared MLP
  10.         self.mlp = nn.Sequential(
  11.             # Conv2d比Linear方便操作
  12.             # nn.Linear(channel, channel // reduction, bias=False)
  13.             nn.Conv2d(channel, channel // reduction, 1, bias=False),
  14.             # inplace=True直接替换,节省内存
  15.             nn.ReLU(inplace=True),
  16.             # nn.Linear(channel // reduction, channel,bias=False)
  17.             nn.Conv2d(channel // reduction, channel, 1, bias=False)
  18.         )
  19.         # spatial attention
  20.         self.conv = nn.Conv2d(2, 1, kernel_size=spatial_kernel,
  21.                               padding=spatial_kernel // 2, bias=False)
  22.         self.sigmoid = nn.Sigmoid()
  23.     def forward(self, x):
  24.         max_out = self.mlp(self.max_pool(x))
  25.         avg_out = self.mlp(self.avg_pool(x))
  26.         channel_out = self.sigmoid(max_out + avg_out)
  27.         x = channel_out * x
  28.         max_out, _ = torch.max(x, dim=1, keepdim=True)
  29.         # print('max_out:',max_out.shape)
  30.         avg_out = torch.mean(x, dim=1, keepdim=True)
  31.         # print('avg_out:',avg_out.shape)
  32.         a=torch.cat([max_out, avg_out], dim=1)
  33.         # print('a:',a.shape)
  34.         spatial_out = self.sigmoid(self.conv(torch.cat([max_out, avg_out], dim=1)))
  35.         # print('spatial:',spatial_out.shape)
  36.         x = spatial_out * x
  37.         # print('x:',x.shape)
  38.         return x
复制代码
(假如要直接利用下面的CBAM_ASPP改进代码,发起将这块代码新建py文件生存,然后在CBAM_ASPP所在python文件中导入CBAMLayer类)
3、改进ASPP:CBAM_ASPP结构


该改进方式与之前的SE_ASPP改进方式相同(感爱好的可以点击相识SE_ASPP),也是把CBAM产生的权重值与原来输入的各个特征举行相乘,作为输入特征,可以直接利用。代码如下
  1. class (nn.Module):                       ##加入通道注意力机制
  2.     def __init__(self, dim_in, dim_out, rate=1, bn_mom=0.1):
  3.         super(CBAM_ASPP, self).__init__()
  4.         self.branch1 = nn.Sequential(
  5.             nn.Conv2d(dim_in, dim_out, 1, 1, padding=0, dilation=rate, bias=True),
  6.             nn.BatchNorm2d(dim_out, momentum=bn_mom),
  7.             nn.ReLU(inplace=True),
  8.         )
  9.         self.branch2 = nn.Sequential(
  10.             nn.Conv2d(dim_in, dim_out, 3, 1, padding=6 * rate, dilation=6 * rate, bias=True),
  11.             nn.BatchNorm2d(dim_out, momentum=bn_mom),
  12.             nn.ReLU(inplace=True),
  13.         )
  14.         self.branch3 = nn.Sequential(
  15.             nn.Conv2d(dim_in, dim_out, 3, 1, padding=12 * rate, dilation=12 * rate, bias=True),
  16.             nn.BatchNorm2d(dim_out, momentum=bn_mom),
  17.             nn.ReLU(inplace=True),
  18.         )
  19.         self.branch4 = nn.Sequential(
  20.             nn.Conv2d(dim_in, dim_out, 3, 1, padding=18 * rate, dilation=18 * rate, bias=True),
  21.             nn.BatchNorm2d(dim_out, momentum=bn_mom),
  22.             nn.ReLU(inplace=True),
  23.         )
  24.         self.branch5_conv = nn.Conv2d(dim_in, dim_out, 1, 1, 0, bias=True)
  25.         self.branch5_bn = nn.BatchNorm2d(dim_out, momentum=bn_mom)
  26.         self.branch5_relu = nn.ReLU(inplace=True)
  27.         self.conv_cat = nn.Sequential(
  28.             nn.Conv2d(dim_out * 5, dim_out, 1, 1, padding=0, bias=True),
  29.             nn.BatchNorm2d(dim_out, momentum=bn_mom),
  30.             nn.ReLU(inplace=True),
  31.         )
  32.         # print('dim_in:',dim_in)
  33.         # print('dim_out:',dim_out)
  34.         self.cbam=CBAMLayer(channel=dim_out*5)
  35.     def forward(self, x):
  36.         [b, c, row, col] = x.size()
  37.         conv1x1 = self.branch1(x)
  38.         conv3x3_1 = self.branch2(x)
  39.         conv3x3_2 = self.branch3(x)
  40.         conv3x3_3 = self.branch4(x)
  41.         global_feature = torch.mean(x, 2, True)
  42.         global_feature = torch.mean(global_feature, 3, True)
  43.         global_feature = self.branch5_conv(global_feature)
  44.         global_feature = self.branch5_bn(global_feature)
  45.         global_feature = self.branch5_relu(global_feature)
  46.         global_feature = F.interpolate(global_feature, (row, col), None, 'bilinear', True)
  47.         feature_cat = torch.cat([conv1x1, conv3x3_1, conv3x3_2, conv3x3_3, global_feature], dim=1)
  48.         # print('feature:',feature_cat.shape)
  49.         # 加入cbam注意力机制
  50.         cbamaspp=self.cbam(feature_cat)
  51.         result1=self.conv_cat(cbamaspp)
  52.         return result
复制代码
Reference

[1].Z. Zhu et al., “Semantic Segmentation of FOD Using an Improved Deeplab V3+ Model,” 2022 12th International Conference on CYBER Technology in Automation, Control, and Intelligent Systems (CYBER), 2022, pp. 791-796, doi: 10.1109/CYBER55403.2022.9907730.
[2].Woo, S., Park, J., Lee, JY., Kweon, I.S. (2018). CBAM: Convolutional Block Attention Module. In: Ferrari, V., Hebert, M., Sminchisescu, C., Weiss, Y. (eds) Computer Vision – ECCV 2018. ECCV 2018. Lecture Notes in Computer Science(), vol 11211. Springer, Cham.

来源:https://blog.csdn.net/qq_45014374/article/details/127782301
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则