注意力模型CBAM

论文:CBAM: Convolutional Block Attention Module 

 

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

 

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

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

Channel attention module:

feature map 的每个channel都被视为一个feature detector,channel attention主要关注于输入图片中什么(what)是有意义的。为了高效地计算channel attention,论文使用最大池化平均池化对feature map在空间维度上进行压缩,得到两个不同的空间背景描述:F^{c}_{max}F^{c}_{avg}。使用由MLP组成的共享网络对这两个不同的空间背景描述进行计算得到channel attention map:M_c \in\mathbb R^{C*1*1}。计算过程如下:

其中W_0 \in \mathbb R^{C/r * C}W_1 \in \mathbb R^{C * C/r}W_0后使用了Relu作为激活函数。

 

Spatial attention module

与channel attention不同,spatial attention主要关注于位置信息(where)。为了计算spatial attention,论文首先在channel的维度上使用最大池化平均池化得到两个不同的特征描述F^{s}_{max} \in \mathbb R_{1*H*W}F^{s}_{avg} \in \mathbb R_{1*H*W},然后使用concatenation将两个特征描述合并,并使用卷积操作生成spatial attention map M_s(F) \in \mathbb R_{H*W}。计算过程如下:

其中,f^{7*7}表示7*7的卷积层

下图为channel attention和spatial attention的示意图:

 

代码:

import tensorflow as tf
import numpy as np

slim = tf.contrib.slim

def combined_static_and_dynamic_shape(tensor):
  """Returns a list containing static and dynamic values for the dimensions.

  Returns a list of static and dynamic values for shape dimensions. This is
  useful to preserve static shapes when available in reshape operation.

  Args:
    tensor: A tensor of any type.

  Returns:
    A list of size tensor.shape.ndims containing integers or a scalar tensor.
  """
  static_tensor_shape = tensor.shape.as_list()
  dynamic_tensor_shape = tf.shape(tensor)
  combined_shape = []
  for index, dim in enumerate(static_tensor_shape):
    if dim is not None:
      combined_shape.append(dim)
    else:
      combined_shape.append(dynamic_tensor_shape[index])
  return combined_shape

def convolutional_block_attention_module(feature_map, index, inner_units_ratio=0.5):
    """
    CBAM: convolution block attention module, which is described in "CBAM: Convolutional Block Attention Module"
    Architecture : "https://arxiv.org/pdf/1807.06521.pdf"
    If you want to use this module, just plug this module into your network
    :param feature_map : input feature map
    :param index : the index of convolution block attention module
    :param inner_units_ratio: output units number of fully connected layer: inner_units_ratio*feature_map_channel
    :return:feature map with channel and spatial attention
    """
    with tf.variable_scope("cbam_%s" % (index)):
        feature_map_shape = combined_static_and_dynamic_shape(feature_map)
        # channel attention
        channel_avg_weights = tf.nn.avg_pool(
            value=feature_map,
            ksize=[1, feature_map_shape[1], feature_map_shape[2], 1],
            strides=[1, 1, 1, 1],
            padding='VALID'
        )
        channel_max_weights = tf.nn.max_pool(
            value=feature_map,
            ksize=[1, feature_map_shape[1], feature_map_shape[2], 1],
            strides=[1, 1, 1, 1],
            padding='VALID'
        )
        channel_avg_reshape = tf.reshape(channel_avg_weights,
                                         [feature_map_shape[0], 1, feature_map_shape[3]])
        channel_max_reshape = tf.reshape(channel_max_weights,
                                         [feature_map_shape[0], 1, feature_map_shape[3]])
        channel_w_reshape = tf.concat([channel_avg_reshape, channel_max_reshape], axis=1)

        fc_1 = tf.layers.dense(
            inputs=channel_w_reshape,
            units=feature_map_shape[3] * inner_units_ratio,
            name="fc_1",
            activation=tf.nn.relu
        )
        fc_2 = tf.layers.dense(
            inputs=fc_1,
            units=feature_map_shape[3],
            name="fc_2",
            activation=None
        )
        channel_attention = tf.reduce_sum(fc_2, axis=1, name="channel_attention_sum")
        channel_attention = tf.nn.sigmoid(channel_attention, name="channel_attention_sum_sigmoid")
        channel_attention = tf.reshape(channel_attention, shape=[feature_map_shape[0], 1, 1, feature_map_shape[3]])
        feature_map_with_channel_attention = tf.multiply(feature_map, channel_attention)
        # spatial attention
        channel_wise_avg_pooling = tf.reduce_mean(feature_map_with_channel_attention, axis=3)
        channel_wise_max_pooling = tf.reduce_max(feature_map_with_channel_attention, axis=3)

        channel_wise_avg_pooling = tf.reshape(channel_wise_avg_pooling,
                                              shape=[feature_map_shape[0], feature_map_shape[1], feature_map_shape[2],
                                                     1])
        channel_wise_max_pooling = tf.reshape(channel_wise_max_pooling,
                                              shape=[feature_map_shape[0], feature_map_shape[1], feature_map_shape[2],
                                                     1])

        channel_wise_pooling = tf.concat([channel_wise_avg_pooling, channel_wise_max_pooling], axis=3)
        spatial_attention = slim.conv2d(
            channel_wise_pooling,
            1,
            [7, 7],
            padding='SAME',
            activation_fn=tf.nn.sigmoid,
            scope="spatial_attention_conv"
        )
        feature_map_with_attention = tf.multiply(feature_map_with_channel_attention, spatial_attention)
        return feature_map_with_attention

#example
feature_map = tf.constant(np.random.rand(2,8,8,32), dtype=tf.float16)
feature_map_with_attention = convolutional_block_attention_module(feature_map, 1)

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    result = sess.run(feature_map_with_attention)
    print(result.shape)

 

### CBAM模块概述 CBAM(Convolutional Block Attention Module)是一种轻量级的注意力机制模块,旨在通过显式建模特征图的空间和通道维度上的重要性来增强卷积神经网络的表现能力[^1]。该模块能够无缝嵌入到现有的CNN架构中,并且可以通过端到端的方式与其他组件一起进行联合训练。 --- ### CBAM结构组成 CBAM主要由两个子模块构成:**通道注意力Channel Attention)** 和 **空间注意力(Spatial Attention)**。这两个子模块依次作用于输入特征图上,从而生成最终的加权特征表示。 #### 1. 通道注意力 通道注意力模块用于捕捉不同通道之间的依赖关系。其核心思想是对每个通道的重要性进行评分并重新调整权重。具体过程如下: - 首先分别计算全局平均池化(Global Average Pooling, GAP)和全局最大池化(Global Max Pooling, GMP),得到两种一维向量。 - 将这两种向量送入共享全连接层(Fully Connected Layer),并通过ReLU激活函数处理后相加。 - 使用Sigmoid激活函数将结果映射至[0, 1]区间,作为各个通道的权重系数。 ```python import torch.nn as nn class ChannelAttention(nn.Module): def __init__(self, channel, reduction=16): super(ChannelAttention, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.max_pool = nn.AdaptiveMaxPool2d(1) self.fc = nn.Sequential( nn.Conv2d(channel, channel // reduction, kernel_size=1), nn.ReLU(inplace=True), nn.Conv2d(channel // reduction, channel, kernel_size=1) ) def forward(self, x): avg_out = self.fc(self.avg_pool(x)) max_out = self.fc(self.max_pool(x)) out = avg_out + max_out return torch.sigmoid(out) ``` 上述代码实现了通道注意力的核心逻辑[^2]。 #### 2. 空间注意力 空间注意力模块则关注于特征图中像素级别的关联性。它通过对特征图的不同位置赋予不同的权重,进一步提升模型对目标区域的关注度。其实现方式类似于通道注意力,但操作对象从通道扩展到了整个二维空间域。 - 同样采用GAP和GMP提取特征信息。 - 对所得特征应用卷积核大小为7×7的一维卷积操作以融合多尺度上下文信息。 - 利用Sigmoid函数生成逐像素的权重掩码。 ```python class SpatialAttention(nn.Module): def __init__(self, kernel_size=7): super(SpatialAttention, self).__init__() assert kernel_size in (3, 7), 'kernel size must be 3 or 7' padding = 3 if kernel_size == 7 else 1 self.conv = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False) def forward(self, x): avg_out = torch.mean(x, dim=1, keepdim=True) max_out, _ = torch.max(x, dim=1, keepdim=True) scale = torch.cat([avg_out, max_out], dim=1) scale = self.conv(scale) return torch.sigmoid(scale) ``` 以上展示了如何构建空间注意力部分。 --- ### 整体流程与PyTorch实现 完整的CBAM模块会先后调用通道注意力和空间注意力两步完成特征重校准工作。以下是综合版本的具体编码形式: ```python class CBAM(nn.Module): def __init__(self, channels, reduction_ratio=16, kernel_size=7): super(CBAM, self).__init__() self.channel_attention = ChannelAttention(channels, reduction=reduction_ratio) self.spatial_attention = SpatialAttention(kernel_size=kernel_size) def forward(self, input_tensor): ca_output = self.channel_attention(input_tensor) * input_tensor sa_output = self.spatial_attention(ca_output) * ca_output return sa_output ``` 此段脚本定义了一个名为`CBAM`的新类,继承自`nn.Module`父类,并封装了前述提到的功能单元。 --- ### 应用场景分析 研究表明,在多种计算机视觉任务(如图像分类、物体检测等)中引入CBAM均能带来显著性能增益[^3]。例如当将其融入经典残差网络——ResNet之中时(见图3所示配置方案),不仅提升了预测精度还保持较低额外开销水平。 此外由于设计灵活简便的特点决定了它可以轻松适配其他主流框架比如MobileNets系列或者DenseNet家族成员身上同样有效果良好表现出来。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值