CVPR-2021
github:https://github.com/Andrew-Qibin/CoordAttention
SENet(【SENet】《Squeeze-and-Excitation Networks》) 注意力忽视了 position information
作者 embedding positional information into channel attention,采用 two 1D feature encoding processes,提出 coordinate attention,direction-aware and position-sensitive
SE / CBAM / CA 结构对比
CBAM 虽然有 spatial attention,但是 fail in modeling long-range dependencies that are essential for vision tasks
下面看看 CA 的公式表达
结合代码看看
import torch
import torch.nn as nn
import math
import torch.nn.functional as F
class h_sigmoid(nn.Module):
def __init__(self, inplace=True):
super(h_sigmoid, self).__init__()
self.relu = nn.ReLU6(inplace=inplace)
def forward(self, x):
return self.relu(x + 3) / 6
class h_swish(nn.Module):
def __init__(self, inplace=True):
super(h_swish, self).__init__()
self.sigmoid = h_sigmoid(inplace=inplace)
def forward(self, x):
return x * self.sigmoid(x)
class CoordAtt(nn.Module):
def __init__(self, inp, oup, reduction=32):
super(CoordAtt, self).__init__()
self.pool_h = nn.AdaptiveAvgPool2d((None, 1))
self.pool_w = nn.AdaptiveAvgPool2d((1, None))
mip = max(8, inp // reduction)
self.conv1 = nn.Conv2d(inp, mip, kernel_size=1, stride=1, padding=0)
self.bn1 = nn.BatchNorm2d(mip)
self.act = h_swish()
self.conv_h = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)
self.conv_w = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)
def forward(self, x):
identity = x
n,c,h,w = x.size()
x_h = self.pool_h(x) #(n, c, h, 1)
x_w = self.pool_w(x).permute(0, 1, 3, 2) #(n, c, 1, w)-> (n, c, w, 1) 注意这里转化成和 x_h 一样的排列形式了
y = torch.cat([x_h, x_w], dim=2) # 方便拼一起,conv->bn->act
y = self.conv1(y)
y = self.bn1(y)
y = self.act(y)
x_h, x_w = torch.split(y, [h, w], dim=2) # 再拆开
x_w = x_w.permute(0, 1, 3, 2) # 这里转化回去了
a_h = self.conv_h(x_h).sigmoid()
a_w = self.conv_w(x_w).sigmoid()
out = identity * a_w * a_h
return out
在网络中的插入位置,以 mobileNetV2(【MobileNet V2】《MobileNetV2:Inverted Residuals and Linear Bottlenecks》) 和 MobileNeXt 为例
(1)Importance of coordinate attention
相比于仅水平或者竖直方向的 spatial attention,合起来最猛
(2)Different weight multipliers
0.25,0.75,1.0 均有一致性的提升
比 NAS 出来的 MobileNeXt + SE 都猛
AutoML 时代,提点的方式之一,加新的原料,加 search space 中的素材
(3)The impact of reduction ratio r
猛,均有提升
(1)Attention for Mobile Networks
capture long-range dependencies among spatial locations that are essential for vision tasks
(2)Stronger Baseline
(1) Object Detection
COCO 和 VOC 都有提升
(2) Semantic Segmentation
比 CBAM 猛
来自:注意力的理解心得