摘要
我们提出卷积块注意模块(CBAM),一个简单而有效的前馈卷积神经网络的注意模块。给定一个中间特征图,我们的模块沿着两个独立的维度(通道和空间)依次推断注意力权重,然后将注意力图乘以输入特征图,以进行自适应特征细化。因为CBAM是一个轻量级的通用模块,它可以无缝集成到任何CNN架构中,开销可以忽略不计,并且可以与基本CNN一起进行端到端训练。我们通过在ImageNet-1K、MS COCO检测和VOC 2007检测数据集上的大量实验来验证我们的CBAM。我们的实验表明,各种模型在分类和检测性能上的改进是一致的,证明了CBAM的广泛适用性。代码和模型将公开提供。
def CBAM(input, reduction):
"""
@Convolutional Block Attention Module
"""
_, width, height, channel = input.get_shape() # (B, W, H, C)
# channel attention
x_mean = tf.reduce_mean(input, axis=(1, 2), keepdims=True) # (B, 1, 1, C)
x_mean = tf.layers.conv2d(x_mean, channel // reduction, 1, activation=tf.nn.relu, name='CA1') # (B, 1, 1, C // r)
x_mean = tf.layers.conv2d(x_mean, channel, 1, name='CA2') # (B, 1, 1, C)
x_max = tf.reduce_max(input, axis=(1, 2), keepdims=True) # (B, 1, 1, C)
x_max = tf.layers.conv2d(x_max, channel // reduction, 1, activation=tf.nn.relu, name='CA1', reuse=True)
# (B, 1, 1, C // r)
x_max = tf.layers.conv2d(x_max, channel, 1, name='CA2', reuse=True) # (B, 1, 1, C)
x = tf.add(x_mean, x_max) # (B, 1, 1, C)
x = tf.nn.sigmoid(x) # (B, 1, 1, C)
x = tf.multiply(input, x) # (B, W, H, C)
# spatial attention
y_mean = tf.reduce_mean(x, axis=3, keepdims=True) # (B, W, H, 1)
y_max = tf.reduce_max(x, axis=3, keepdims=True) # (B, W, H, 1)
y = tf.concat([y_mean, y_max], axis=-1) # (B, W, H, 2)
y = tf.layers.conv2d(y