维度-空间注意力总结

近年来,注意力机制一直受到大家的追捧,本文总结下几篇显著性检测中的维度-空间注意力机制。

BAM

图1. BAM的整体框架图
图1. BAM的整体框架图
如图1所示,BAM分为并行的两部分:Channel Attention和Spatial Attention,Channel Attention包括一个Global avg pool(全局平均池化)和一个带隐藏层的多次感知器(MLP),为了节省开销、减小参数量,隐藏层的系数r被设置为维度C除以16,在MLP之后使用BN层来调整输出的尺度;Spatial Attention首先经过一个11卷积整合和压缩输入维度信息,然后是两个33的卷积操作来有效的获取上下文信息,最后使用1*1卷积将维度重新变为初始输入维度,同样使用BN层来收尾。经过验证,使用逐元素相加然后经过sigmoid函数最后加上原来的输入,一个并行的维度-空间注意力就完成了。代码如下:

class ChannelGate(nn.Module):
    def __init__(self, gate_channel, reduction_ratio=16, num_layers=1):
        super(ChannelGate, self).__init__()
        # self.gate_activation = gate_activation
        self.gate_c = nn.Sequential()
        self.gate_c.add_module('flatten', Flatten())
        gate_channels = [gate_channel]
        gate_channels += [gate_channel // reduction_ratio] * num_layers
        gate_channels += [gate_channel]
        for i in range(len(gate_channels) - 2):
            self.gate_c.add_module('gate_c_fc_%d' % i, nn.Linear(gate_channels[i], gate_channels[i + 1]))
            self.gate_c.add_module('gate_c_bn_%d' % (i + 1), nn.BatchNorm1d(gate_channels[i + 1]))
            self.gate_c.add_module('gate_c_relu_%d' % (i + 1), nn.ReLU())
        self.gate_c.add_module('gate_c_fc_final', nn.Linear(gate_channels[-2], gate_channels[-1]))

    def forward(self, in_tensor):
        avg_pool = F.avg_pool2d(in_tensor, in_tensor.size(2), stride=in_tensor.size(2))
        return self.gate_c(avg_pool).unsqueeze(2).unsqueeze(3).expand_as(in_tensor)
class SpatialGate(nn.Module):
    def __init__(self, gate_channel, reduction_ratio=16, dilation_conv_num=2, dilation_val=4):
        super(SpatialGate, self).__init__()
        self.gate_s = nn.Sequential()
        self.gate_s.add_module('gate_s_conv_reduce0',
                               nn.Conv2d(gate_channel, gate_channel // reduction_ratio, kernel_size=1))
        self.gate_s.add_module('gate_s_bn_reduce0', nn.BatchNorm2d(gate_channel // reduction_ratio))
        self.gate_s.add_module('gate_s_relu_reduce0', nn.ReLU())
        for i in range(dilation_conv_num):
            self.gate_s.add_module('gate_s_conv_di_%d' % i,
                                   nn.Conv2d(gate_channel // reduction_ratio, gate_channel // reduction_ratio,
                                             kernel_size=3, \
                                             padding=dilation_val, dilation=dilation_val))
            self.gate_s.add_module('gate_s_bn_di_%d' % i, nn.BatchNorm2d(gate_channel // reduction_ratio))
            self.gate_s.add_module('gate_s_relu_di_%d' % i, nn.ReLU())
        self.gate_s.add_module('gate_s_conv_final', nn.Conv2d(gate_channel // reduction_ratio, 1, kernel_size=1))

    def forward(self, in_tensor):
        return self.gate_s(in_tensor).expand_as(in_tensor)

CBAM

	图2. CBAM的整体架构
图2. CBAM的整体架构
图3. Channel Attention和Spatial Attention的细节图
图3. Channel Attention和Spatial Attention的细节图
和BAM不同的是,它采用串联的维度-空间注意力架构,而不是并行的。对Channel Attention而言,它首先经过一个并行的MaxPool和AVGPool,将结果通过一个共享的MLP模块,MLP模块同样有一个隐藏层,为了减少参数量,使用C/16来作为隐藏层的系数,然后将两个输出先相加,再进行sigmoid操作,输出经过维度注意力模块后的结果;空间注意力模块则是先沿着通道的轴分别应用最大值和平均值操作对每行取最大值、平均值,将结果进行卷积操作(卷积核大小为7),对结果进行sigmoid操作就得到了空间注意力处理后的结果。

BBS_Net

图4. DEM模块的架构图
图4. DEM模块的架构图
可以看到BBSNet的维度-空间注意力模块和CBAM模块一样都是采用串联的方式,对于Channel Attention:首先经过自适应最大池化操作,然后经过和CBAM相同的1*1卷积操作,系数同样被设置为C/16,最后经过sigmoid函数;Spatial Attention则是根据padding的不同,将卷积核大小分别设为3或7,这不同于CBAM中的固定以7为卷积核大小的方法,然后经过BatchNorm2d进行归一化操作得到空间注意力处理后的结果。代码如下:

class ChannelAttention(nn.Module):
    def __init__(self, in_planes, ratio=16):
        super(ChannelAttention, self).__init__()

        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):
        max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))
        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(1, 1, kernel_size, padding=padding, bias=False)
    self.sigmoid = nn.Sigmoid()

def forward(self, x):
    # 对每行取最大值,舍弃了CBAM中的平均值
    max_out, _ = torch.max(x, dim=1, keepdim=True)
    x = max_out
    x = self.conv1(x)
    return self.sigmoid(x)

``

Depth-induced Multi-scale Recurrent Attention Network for Saliency Detection

图5. DMRANet的分支:DMSW和RAM的细节
图5. DMRANet的分支:DMSW和RAM的细节
DMRA的维度注意力和LSTM结合在了一起,对于维度注意力:先经过卷积操作,然后和经过LSTM处理后的结果加在一起,再经过平均池化操作,然后按行进行softmax操作。对于空间注意力:首先进行两次卷积操作,对第二次卷积操作后的结果求sigmoid,然后和第一次的卷积结果逐行相乘(torch.mul),最终得到经过注意力机制后的结果。代码如下:

depth = self.conv_c(F_sum)
            h_c = self.conv_h(h_step)
            depth = depth + h_c
            depth = self.pool_avg(depth)
            # 按照每一行进行soft归一化
            depth = torch.mul(F.softmax(depth, dim=1), 64)
            F_sum_wt = torch.mul(depth_fw_ori, depth)

Is depth really necessary for salient object detection

图6. DASNet的维度注意力框架图
图6. DASNet的维度注意力框架图
它的维度注意力首先都输入的RGB、深度特征图做卷积+BN+ReLU的操作,然后元素相乘,再将相乘后的结果和原输入进行cat,然后进行全局平均池化操作,再卷积,通过sigmoid函数后和经过cat后的结果元素相乘,把原始的RGB和深度特征图和元素相乘后的结果分别元素相加,最后把元素相加后的结果进行cat,形成最终的输出。未提供源码

Select, supplement and focus for RGB-D saliency detection

在这里插入图片描述
图7. SSF的CAU和BSU的框架图
它的Attention模块很简单:首先是一个自适应平均池化操作、然后是两个隐藏层、加上ReLU和sigmoid操作,然后和自适应平均池化后的结果元素相乘,注意力模块就结束了。代码如下:

class AttentionLayer(nn.Module):
    def __init__(self, channel, reduction=2, multiply=True):
        super(AttentionLayer, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            nn.Linear(channel, channel // reduction),
            nn.ReLU(inplace=True),
            nn.Linear(channel // reduction, channel),
            nn.Sigmoid()
        )
        self.multiply = multiply

    def forward(self, x):
        b, c, _, _ = x.size()
        y = self.avg_pool(x).view(b, c)
        y = self.fc(y).view(b, c, 1, 1)
        if self.multiply == True:
            return x * y
        else:
            return y
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值