SENet(Squeeze Excitation)、CBAM(Convolutional Block Attention Module)和CA(Coordinate Attention)

SENet(Squeeze Excitation)

我们可以看到,已经有很多工作在空间维度上来提升网络的性能。那么很自然想到,网络是否可以从其他层面来考虑去提升性能,比如考虑特征通道之间的关系?我们的工作就是基于这一点并提出了 Squeeze-and-Excitation Networks(简称 SENet)。Squeeze 和 Excitation 是两个非常关键的操作,所以我们以此来命名。动机是希望显式地建模特征通道之间的相互依赖关系。另外,我们并不打算引入一个新的空间维度来进行特征通道间的融合,而是采用了一种全新的「特征重标定」策略。具体来说,就是通过学习的方式来自动获取到每个特征通道的重要程度,然后依照这个重要程度去提升有用的特征并抑制对当前任务用处不大的特征。

简单来说就分为三步:

  1. Squeeze:通过Global Average pooling [n,c,h,w]->[n,c,1,1],得到一个w矩阵
  2. Excitation: 把这个w矩阵通过FC和Relu变成[n,c/r,1,1],再通过FC和Sigmoid变成[n,c,1,1]
  3. Reweight:最后用这个w矩阵来reweight输入

SENet的code如下:

code摘录自 imgclsmob/pytorch/pytorchcv/models/common.py at 68335927ba27f2356093b985bada0bc3989836b1 · osmr/imgclsmob · GitHub

import torch.nn as nn
import torch

def conv1x1(in_channels,
            out_channels,
            stride=1,
            groups=1,
            bias=False):
    return nn.Conv2d(
        in_channels=in_channels,
        out_channels=out_channels,
        kernel_size=1,
        stride=stride,
        groups=groups,
        bias=bias)

class SEBlock(nn.Module):

    def __init__(self,
                 channels,
                 reduction=16):
        super(SEBlock, self).__init__()
        mid_cannels = channels // reduction

        self.pool = nn.AdaptiveAvgPool2d(output_size=1)
        self.conv1 = conv1x1(
            in_channels=channels,
            out_channels=mid_cannels,
            bias=True)
        self.activ = nn.ReLU(inplace=True)
        self.conv2 = conv1x1(
            in_channels=mid_cannels,
            out_channels=channels,
            bias=True)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        w = self.pool(x)
        print('after pool shape {}'.format(w.shape))
        w = self.conv1(w)
        print('after conv1 shape {}'.format(w.shape))
        w = self.activ(w)
        print('after activ shape {}'.format(w.shape))
        w = self.conv2(w)
        print('after conv2 shape {}'.format(w.shape))
        w = self.sigmoid(w)
        print('after sigmoid shape {}'.format(w.shape))
        x = x * w
        return x

if __name__ == '__main__':
    bs,c,h,w=2,16,3,3
    x = torch.randn(bs,c,h,w)
    se = SEBlock(channels=c)
    print(se(x).shape)

'''
输出为:
after pool shape torch.Size([2, 16, 1, 1])
after conv1 shape torch.Size([2, 1, 1, 1])
after activ shape torch.Size([2, 1, 1, 1])
after conv2 shape torch.Size([2, 16, 1, 1])
after sigmoid shape torch.Size([2, 16, 1, 1])
torch.Size([2, 16, 3, 3])
'''

Convolutional Block Attention Module (CBAM)

该网络发表于2018年的CVPR。其主要思想是对特征进行空间和通道上的注意力操作,可以认为是SENet的增强版。

图中第一阶段Channel Attention Module相当于SENet中的pooling用两种方式做了两遍,代码中叫作ChannelGate;第二阶段Spatial Attention Module相当于先用ChannelPool分别用均值最大两种pooling的方式把[n,c,h,w]压缩到[n,2,h,w](代码中ChannelPool的部分),然后再通过一个input_channels=2,output_channels=1的卷积变成[n,1,h,w],最后来一个sigmoid后reweight的操作。ChannelGate整体相当于SENet中pooling->mlp->relu->mlp的方式,而ChannelPool是直接做2种方式的pooling,细节可以参考下面的代码:

import torch
import math
import torch.nn as nn
import torch.nn.functional as F
 
class BasicConv(nn.Module):
    def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, relu=True, bn=True, bias=False):
        super(BasicConv, self).__init__()
        self.out_channels = out_planes
        self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)
        self.bn = nn.BatchNorm2d(out_planes,eps=1e-5, momentum=0.01, affine=True) if bn else None
        self.relu = nn.ReLU() if relu else None
 
    def forward(self, x):
        x = self.conv(x)
        if self.bn is not None:
            x = self.bn(x)
        if self.relu is not None:
            x = self.relu(x)
        return x
 
class Flatten(nn.Module):
    def forward(self, x):
        return x.view(x.size(0), -1)
 
class ChannelGate(nn.Module):
    def __init__(self, gate_channels, reduction_ratio=16, pool_types=['avg', 'max']):
        super(ChannelGate, self).__init__()
        self.gate_channels = gate_channels
        self.mlp = nn.Sequential(
            Flatten(),
            nn.Linear(gate_channels, gate_channels // reduction_ratio),
            nn.ReLU(),
            nn.Linear(gate_channels // reduction_ratio, gate_channels)
            )
        self.pool_types = pool_types
    def forward(self, x):
        channel_att_sum = None
        for pool_type in self.pool_types:
            if pool_type=='avg':
                avg_pool = F.avg_pool2d( x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
                channel_att_raw = self.mlp( avg_pool )
            elif pool_type=='max':
                max_pool = F.max_pool2d( x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
                channel_att_raw = self.mlp( max_pool )
            elif pool_type=='lp':
                lp_pool = F.lp_pool2d( x, 2, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
                channel_att_raw = self.mlp( lp_pool )
            elif pool_type=='lse':
                # LSE pool only
                lse_pool = logsumexp_2d(x)
                channel_att_raw = self.mlp( lse_pool )
 
            if channel_att_sum is None:
                channel_att_sum = channel_att_raw
            else:
                channel_att_sum = channel_att_sum + channel_att_raw
 
        scale = F.sigmoid( channel_att_sum ).unsqueeze(2).unsqueeze(3).expand_as(x)
        return x * scale
 
def logsumexp_2d(tensor):
    tensor_flatten = tensor.view(tensor.size(0), tensor.size(1), -1)
    s, _ = torch.max(tensor_flatten, dim=2, keepdim=True)
    outputs = s + (tensor_flatten - s).exp().sum(dim=2, keepdim=True).log()
    return outputs
 
class ChannelPool(nn.Module):
    def forward(self, x):
        # torch.max(x,1)[0] 表示在dim=1这维算最大,max返回namedtuple (values, indices) ,这里只取values
        # 整个函数是把channel维度的最大和平均给拼起来
        return torch.cat( (torch.max(x,1)[0].unsqueeze(1), torch.mean(x,1).unsqueeze(1)), dim=1 )
 
class SpatialGate(nn.Module):
    def __init__(self):
        super(SpatialGate, self).__init__()
        kernel_size = 7
        self.compress = ChannelPool()
        self.spatial = BasicConv(2, 1, kernel_size, stride=1, padding=(kernel_size-1) // 2, relu=False)
    def forward(self, x):
        x_compress = self.compress(x)
        print('after x_compress shape {}'.format(x_compress.shape))
        x_out = self.spatial(x_compress)
        print('after x_out shape {}'.format(x_out.shape))
        scale = F.sigmoid(x_out) # broadcasting
        return x * scale
 
class CBAM(nn.Module):
    def __init__(self, gate_channels, reduction_ratio=16, pool_types=['avg', 'max'], no_spatial=False):
        super(CBAM, self).__init__()
        self.ChannelGate = ChannelGate(gate_channels, reduction_ratio, pool_types)
        self.no_spatial=no_spatial
        if not no_spatial:
            self.SpatialGate = SpatialGate()
    def forward(self, x):
        x_out = self.ChannelGate(x)
        print('after channelgate shape {}'.format(x_out.shape))
        if not self.no_spatial:
            x_out = self.SpatialGate(x_out)
        return x_out
 
if __name__ == '__main__':
    bs,c,h,w=2,16,3,3
    x = torch.randn(bs,c,h,w)
    cbam = CBAM(gate_channels=c)
    res = cbam(x)
    print(res.shape)

"""
after channelgate shape torch.Size([2, 16, 3, 3])
after x_compress shape torch.Size([2, 2, 3, 3])
after x_out shape torch.Size([2, 1, 3, 3])
torch.Size([2, 16, 3, 3])
"""

Coordinate Attention for Efficient Mobile Network Design(简称Coordinate Attention)

该网络发表于2021的CVPR,核心是拆成h,w两个维度进行reweight,只用avg pool,最终只reweight一次就可以了,参考下图:

代码摘录自CoordAttention/coordatt.py at main · houqb/CoordAttention · GitHub

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)
        x_w = self.pool_w(x).permute(0, 1, 3, 2)

        y = torch.cat([x_h, x_w], dim=2)
        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

### Convolutional Attention Block in Deep Learning Models: Implementation and Optimization 卷积注意力块(Convolutional Attention BlockCAB)是一种结合了卷积神经网络(CNN)注意力机制的模块,旨在通过动态调整特征图中的重要性来增强模型的表现力[^2]。CAB通常被设计为一个轻量级模块,可以嵌入到现有的深度学习架构中以提高其性能。以下是关于CAB的实现与优化的一些关键点: #### 1. **CAB的基本结构** CAB的核心思想是利用注意力机制对卷积层提取的特征图进行加权调整。这种加权调整可以通过以下方式实现: - **通道注意力**:通过对每个通道的重要性进行建模,突出重要的特征通道。这通常通过全局平均池化(Global Average Pooling, GAP)多层感知机(MLP)实现[^2]。 - **空间注意力**:通过对特征图的空间位置进行加权,强调图像中更重要的区域。这通常通过计算特征图的空间分布并生成一个空间掩码来实现[^2]。 #### 2. **CAB的实现** CAB的实现可以分为以下几个部分: - **输入特征图**:假设输入特征图为 \( F \in \mathbb{R}^{H \times W \times C} \),其中 \( H \)、\( W \) \( C \) 分别表示高度、宽度通道数。 - **通道注意力模块**: ```python import torch import torch.nn as nn 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 // ratio, 1, bias=False) self.relu1 = nn.ReLU() self.fc2 = nn.Conv2d(in_planes // ratio, 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 = self.sigmoid(avg_out + max_out) return out ``` - **空间注意力模块**: ```python 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) ``` #### 3. **CAB的优化** 为了进一步提升CAB的效果,可以考虑以下优化策略: - **参数共享**:在多尺度特征图上应用CAB时,可以共享通道空间注意力模块的参数以减少计算开销。 - **轻量化设计**:通过减少MLP的隐藏层大小或使用更小的卷积核来降低CAB的复杂度[^2]。 - **结合其他模块**:将CAB与其他高效的注意力机制(如SENetCBAM)结合,以进一步提升模型性能[^2]。 #### 4. **应用场景** CAB广泛应用于各种计算机视觉任务中,包括但不限于: - **图像分类**:通过增强特征表示能力,提高分类准确率。 - **目标检测**:通过关注图像中的重要区域,改善检测效果。 - **语义分割**:通过细化特征图的空间信息,提升分割精度。 ```python class ConvolutionalAttentionBlock(nn.Module): def __init__(self, in_planes, ratio=16, kernel_size=7): super(ConvolutionalAttentionBlock, self).__init__() self.channel_attention = ChannelAttention(in_planes, ratio) self.spatial_attention = SpatialAttention(kernel_size) def forward(self, x): ca = self.channel_attention(x) * x # 通道注意力 sa = self.spatial_attention(ca) * ca # 空间注意力 return sa ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值