【Attention】注意力机制在图像上的应用

[SeNet] Squeeze-and-Excitation Networks (CVPR2018)

论文 https://arxiv.org/abs/1709.01507
代码html

详细信息能够阅读下面的blog.
SeNet在channel 维进行了信息融合,是一种self_attention的实现。
在这里插入图片描述
具体来说,SeNet是一种卷积结构,将feature map 先压缩,后激励。
压缩Sequeeze
经过global pooling ,在channel 维出一个 1 × 1 × C 1\times1\times C 的tensor。而后经过一个bttleneck结构。 1 × 1 × C 1\times1\times C 变为 1 × 1 × C r 1\times1\times \frac{C}{r} 经过relu激活后再变为 1 × 1 × C 1\times1\times C 。这个 r r 能够是4或者16,4 在imageNet 上的 image-top1 err 最低为22.25%,16 在imageNet 上的 image-top1 err 最低为6.03%,backbone 为ResNet-50。python

经过bttleneck有两个好处,1是减小模型复杂性,2是增长泛化性。
激励exciation
1 × 1 × C 1\times1\times C 的tensor最终会经过sigmoid函数,表示每一维channel具备的信息的价值的差别,而后与原始张量channel维相乘。
在这里插入图片描述
为何上述的结构会有效呢?
论文中作了简单的分析
在Squeeze 部分,使用Global pooling 起了决定性做用。git

Squeeze NoSqueeze
Global pooling None
FC1 1 × 1 × C r 1\times1\times\frac{C}{r} Conv
FC2 1 × 1 × C 1\times1\times C Conv

在这里插入图片描述
使用全局信息会有明显的提高
在这里插入图片描述github

  1. 在浅层的网络中,不一样类别的激活分布十分类似。表示在在此阶段,全部类别共享相同特征通道。早期的特征更具备通常性。
  2. 随着网络变深,不一样类别所对应的特征通道是不一样。较深层的网络逐渐表现出差别性。
  3. 在靠经网络输出的层,激励的效果趋向于饱和,表示最后的若干层对于不一样类别所提供的差别性不如以前层重要。f 这张图之因此分布相似,偏重不一样是由于以后须要输出分类类别。

https://blog.csdn.net/u014380165/article/details/78006626web

[Non-local] Non-local neural Networks (CVPR2018)

论文 https://arxiv.org/abs/1711.07971
代码 https://github.com/AlexHex7/Non-local_pytorch网络

Non-local这一篇是在point-wise方面作attention的。app

卷积操做的核心在于权重共享和局部相应。经过不断缩小featuresvg

这一篇主要将视频或者音频具备时序顺序的张量的处理。可是思想在2D任然是有效的。能够把全部的T去掉, 1 × 1 × 1 1\times1\times1 卷积变为 1 × 1 1\times1 卷积。函数

具体来讲 把一个张量转置后与自身作矩阵相乘。这样每个像素位置都融合了其余位置的信息。ui

而后经过softmax激活channel维的张量。

同时,输入张量会经过三个不一样的 1 × 1 × 1 1\times1\times1 的卷积层,而后在出口再通过 1 × 1 × 1 1\times1\times1 的卷积层,恢复为原来的channel维数。其实仍然具备一个bottleneck 结构。

最后的结果与原始张量相加。
在这里插入图片描述
核心代码以下

def forward(self, x):
        ''' :param x: (b, c, t, h, w) :return: '''

        batch_size = x.size(0)

        g_x = self.g(x).view(batch_size, self.inter_channels, -1)
        g_x = g_x.permute(0, 2, 1)

        theta_x = self.theta(x).view(batch_size, self.inter_channels, -1)
        theta_x = theta_x.permute(0, 2, 1)
        phi_x = self.phi(x).view(batch_size, self.inter_channels, -1)
        f = torch.matmul(theta_x, phi_x)
        f_div_C = F.softmax(f, dim=-1)

        y = torch.matmul(f_div_C, g_x)
        y = y.permute(0, 2, 1)
        y = y.reshape(batch_size, self.inter_channels, *x.size()[2:])
        W_y = self.W(y)
        z = W_y + x

        return z

[GCNet] Non-local Networks Meet Squeeze-Excitation Networks and Beyond 2019-04

论文 https://arxiv.org/abs/1904.11492
代码 https://github.com/xvjiarui/GCNet

首先对Non-local 作了一个简化。
在这里插入图片描述
而后提出一种通用的attention 模块。

将一个attention的过程分为context modeling 以及 transform两部分。

而后取了SeNet的transform部分,simplified Non-local的context modeling部分组成一个新的attention 模块。

在这里插入图片描述