视觉Attention之SENet和CBAM概述

Attention最早出现于NLP中,随着不断的发展,cv中对Attention的应用不断增加,早期两个比较经典的视觉Attention机制分别是SENet和CBAM,分别在17年和18年的大赛中夺魁,本文即对两种算法进行简单的介绍。

SENet

我们可以通过卷积得到feature map,SENet认为这个feature map中每个通道的重要程度是不一样的,应该给其分配权值,即代表每个通道各自的重要程度,具体的结构示意图如下:

在这里插入图片描述
在传统的Inception模型的基础上,增加上述module。假设输入是一张 c ∗ h ∗ w c*h*w chw的特征图,首先经过global pooling,将维度变为 c ∗ 1 ∗ 1 c*1*1 c11,随后经过FC层,将维度变为 c / 16 ∗ 1 ∗ 1 c/16*1*1 c/1611,再经过一个FC层,将维度还原成 c ∗ 1 ∗ 1 c*1*1 c11。这个 c ∗ 1 ∗ 1 c*1*1 c11表示对每一个通道分配的权重,将这个矩阵和输入feature map做矩阵全乘,即实现了Attention。

CBAM

SENet仅仅是对不同通道做了Attention,而没有关注spatial这个因素。因此CBAM即关注了通道因素,也关注了空间位置的因素。其结构示意图如下所示:

在这里插入图片描述
可见CBAM先通过一个Channel Attention模块,实现通道层面的注意力。在经过一个spatial attention模块,实现空间层面的注意力。这里的Channel Attention模块与SENet的略有不同,其结构示意图如下所示:

在这里插入图片描述

SENet仅仅采用了maxpool,而CBAM将maxpool与avgpool进行融合,使得提取的高层特征更加丰富,将两个pool后的结果相加,其余操作与SENet的一致。

在spatial attention模块,也与channel attention大同小异。现将每个空间位置的所有像素和分别作avgpool和maxpool,得到两张 h ∗ w ∗ 1 h*w*1 hw1的map,将两者concat起来,得到 h ∗ w ∗ 2 h*w*2 hw2的图,随后再经过一个卷积层,卷积核大小7x7,输出通道数为1,得到一个尺寸为 h ∗ w ∗ 1 h*w*1 hw1的map,这个输出相当于权重的分配矩阵,与之前做完channel attention的feature map做矩阵全乘,即可得到Attention的结果。

结合一段代码可以更好地理解:

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, 1, 7, padding='same', activation=tf.nn.sigmoid)    # (B, W, H, 1)
    y = tf.multiply(x, y)  # (B, W, H, C)

    return y

参考:https://blog.csdn.net/buziran/article/details/88387147

猜你喜欢

转载自blog.csdn.net/jackzhang11/article/details/109211948
今日推荐