CBAM: Convolutional Block Attention Module—— channel attention + spatial attention

  1. 影响卷积神经网络的几大因素:
  • Depth: VGG, ResNet
  • Width: GoogLeNet
  • Cardinality: Xception, ResNeXt
  • Attention:channel attention, spatial attention
  1. Attention在人类感知系统中扮演了重要角色,人类视觉系统的一大重要性质是人类并不是试图一次处理完整个场景,与此相反,为了更好地捕捉视觉结构,人类利用一系列的局部瞥见,选择性地聚焦于突出的部分。

CBAM: Convolutional Block Attention Module—— channel attention + spatial attention

CBAM: Convolutional Block Attention Module—— channel attention + spatial attention

CBAM: Convolutional Block Attention Module—— channel attention + spatial attention

CBAM其实就是顺序进行channel attention和spatial attention:

  • Channel attention: focus on what feature map is meaningful; 全连接层是使用卷积核=1的卷积实现的
  • Spatial attention:focus on where is an informative part;沿channel 轴的求均值操作

CBAM: Convolutional Block Attention Module—— channel attention + spatial attention

Attention和fature map是元素级别的相乘,相乘时会自动进行broadcast(copy)操作,即channel attention沿着spatial维度广播,spatial attention沿着channel维度广播

class ChannelAttention(nn.Module):
    def __init__(self, in_planes, ratio=16):
        super(ChannelAttention, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.max_pool = nn.AdaptiveMaxPool2d(1)

        self.fc1   = nn.Conv2d(in_planes, in_planes // 16, 1, bias=False)
        self.relu1 = nn.ReLU()
        self.fc2   = nn.Conv2d(in_planes // 16, in_planes, 1, bias=False)

        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))
        max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))
        out = avg_out + max_out
        return self.sigmoid(out)

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.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        avg_out = torch.mean(x, dim=1, keepdim=True)
        max_out, _ = torch.max(x, dim=1, keepdim=True)
        x = torch.cat([avg_out, max_out], dim=1)
        x = self.conv1(x)
        return self.sigmoid(x)

 

参考代码:https://github.com/luuuyi/CBAM.PyTorch/blob/master/model/resnet_cbam.py

上一篇:人群密度估计之CSRNet---Dilated Convolutional Neural Networks for Understanding the Highly Congested Scenes


下一篇:“卷积神经网络(Convolutional Neural Network,CNN)”之问