注意力模型CBAM

论文:CBAM: Convolutional Block Attention Module 

 

Convolutional Block Attention Module (CBAM) 表示卷积模块的注意力机制模块。是一种结合了空间(spatial)和通道(channel)的注意力机制模块。相比于senet只关注通道(channel)的注意力机制可以取得更好的效果。

 

基于传统VGG结构的CBAM模块。需要在每个卷积层后面加该模块。

基于shortcut结构的CBAM模块。例如resnet50,该模块在每个resnet的block后面加该模块。

 

Channel attention module:

 

将输入的featuremap,分别经过基于width和height的global max pooling 和global average pooling,然后分别经过MLP。将MLP输出的特征进行基于elementwise的加和操作,再经过sigmoid激活操作,生成最终的channel attention featuremap。将该channel attention featuremap和input featuremap做elementwise乘法操作,生成Spatial attention模块需要的输入特征。

其中,seigema为sigmoid操作,r表示减少率,其中W0后面需要接RELU激活。

 

Spatial attention module:

 

将Channel attention模块输出的特征图作为本模块的输入特征图。首先做一个基于channel的global max pooling 和global average pooling,然后将这2个结果基于channel 做concat操作。然后经过一个卷积操作,降维为1个channel。再经过sigmoid生成spatial attention feature。最后将该feature和该模块的输入feature做乘法,得到最终生成的特征。

其中,seigema为sigmoid操作,7*7表示卷积核的大小,7*7的卷积核比3*3的卷积核效果更好。

 

The code:

def cbam_module(inputs,reduction_ratio=0.5,name=""):
    with tf.variable_scope("cbam_"+name, reuse=tf.AUTO_REUSE):
        batch_size,hidden_num=inputs.get_shape().as_list()[0],inputs.get_shape().as_list()[3]

        maxpool_channel=tf.reduce_max(tf.reduce_max(inputs,axis=1,keepdims=True),axis=2,keepdims=True)
        avgpool_channel=tf.reduce_mean(tf.reduce_mean(inputs,axis=1,keepdims=True),axis=2,keepdims=True)
        
        maxpool_channel = tf.layers.Flatten()(maxpool_channel)
        avgpool_channel = tf.layers.Flatten()(avgpool_channel)
        
        mlp_1_max=tf.layers.dense(inputs=maxpool_channel,units=int(hidden_num*reduction_ratio),name="mlp_1",reuse=None,activation=tf.nn.relu)
        mlp_2_max=tf.layers.dense(inputs=mlp_1_max,units=hidden_num,name="mlp_2",reuse=None)
        mlp_2_max=tf.reshape(mlp_2_max,[batch_size,1,1,hidden_num])

        mlp_1_avg=tf.layers.dense(inputs=avgpool_channel,units=int(hidden_num*reduction_ratio),name="mlp_1",reuse=True,activation=tf.nn.relu)
        mlp_2_avg=tf.layers.dense(inputs=mlp_1_avg,units=hidden_num,name="mlp_2",reuse=True)
        mlp_2_avg=tf.reshape(mlp_2_avg,[batch_size,1,1,hidden_num])

        channel_attention=tf.nn.sigmoid(mlp_2_max+mlp_2_avg)
        channel_refined_feature=inputs*channel_attention

        maxpool_spatial=tf.reduce_max(inputs,axis=3,keepdims=True)
        avgpool_spatial=tf.reduce_mean(inputs,axis=3,keepdims=True)
        max_avg_pool_spatial=tf.concat([maxpool_spatial,avgpool_spatial],axis=3)
        conv_layer=tf.layers.conv2d(inputs=max_avg_pool_spatial, filters=1, kernel_size=(7, 7), padding="same", activation=None)
        spatial_attention=tf.nn.sigmoid(conv_layer)

        refined_feature=channel_refined_feature*spatial_attention

    return refined_feature

 

### 卷积注意力机制 CBAM 的原理 CBAM(Convolutional Block Attention Module)旨在通过引入通道注意力和空间注意力来提升卷积神经网络(CNN)的感知能力,在不显著增加计算成本的前提下改进模型性能[^2]。 #### 通道注意力机制 通道注意力专注于不同特征图的重要性。具体来说,该部分会分析每个通道上的全局信息并自适应调整各个通道权重。这一步骤通常涉及两个操作: - **平均池化**:对输入特征图执行全局平均池化以获取各通道响应均值。 - **最大池化**:同样作用于整个特征图上,但取的是每条路径的最大激活值。 接着,上述两种统计量被送入共享多层感知器(MLP),最终输出经过sigmoid函数转换后的权值向量,以此加权原特征图得到新的表示形式。 ```python def channel_attention(input_features, reduction_ratio=16): batch_size, num_channels, height, width = input_features.size() avg_pool = torch.mean(input_features.view(batch_size, num_channels, -1), dim=2).view(batch_size, num_channels, 1, 1) max_pool = torch.max(input_features.view(batch_size, num_channels, -1), dim=2)[0].view(batch_size, num_channels, 1, 1) mlp_avg = nn.Sequential( nn.Conv2d(num_channels, num_channels // reduction_ratio, kernel_size=1), nn.ReLU(), nn.Conv2d(num_channels // reduction_ratio, num_channels, kernel_size=1) )(avg_pool) mlp_max = nn.Sequential( nn.Conv2d(num_channels, num_channels // reduction_ratio, kernel_size=1), nn.ReLU(), nn.Conv2d(num_channels // reduction_ratio, num_channels, kernel_size=1) )(max_pool) scale = torch.sigmoid(mlp_avg + mlp_max) return input_features * scale.expand_as(input_features) ``` #### 空间注意力机制 不同于通道级处理方式,这里关注图像内像素位置间的关联性。通过对给定特征映射应用跨维度聚合策略,可以捕捉到更为丰富的上下文依赖关系。此过程主要包含以下几个方面的工作: - 首先分别沿宽度方向以及高度方向实施最大/均值汇聚运算; - 将所得结果拼接在一起形成一个新的张量作为后续全连接层的输入源; - 经过一层ReLU激活之后再经由单个卷积核大小为7×7的标准二维卷积完成最后的空间分布预测任务; ```python def spatial_attention(channel_refined_feature): max_pool = torch.max(channel_refined_feature, dim=1)[0].unsqueeze(1) avg_pool = torch.mean(channel_refined_feature, dim=1).unsqueeze(1) combined = torch.cat([max_pool, avg_pool], dim=1) conv_combined = nn.Conv2d(in_channels=2, out_channels=1, kernel_size=(7, 7), stride=(1, 1), padding='same')(combined) attention_map = torch.sigmoid(conv_combined) return channel_refined_feature * attention_map ``` ### 应用场景与优势 在计算机视觉领域中,尤其是对于物体检测、语义分割等任务而言,利用CBAM能够有效突出重要区域的同时抑制无关干扰因素的影响,进而达到更好的识别效果。此外,由于其轻量化设计思路使得这种技术易于集成至现有框架当中而不造成过多额外负担。
评论 77
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值