Attention机制中SEnet CBAM以及Dual pooling的pytorch实现

本来自己写了,关于SENet的注意力截止,但是在准备写其他注意力机制代码的时候,看到一篇文章总结的很好,所以对此篇文章进行搬运,以供自己查阅,并加上自己的理解。

[TOC]

1.SENET中的channel-wise加权的实现

实现代码参考自:senet.pytorch
senet
代码如下:
SEnet 模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 from torch import nn
class SELayer(nn.Module):
    def __init__(self, channel, reduction=16):
        super(SELayer, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            nn.Linear(channel, channel // reduction, bias=False),
            nn.ReLU(inplace=True),
            nn.Linear(channel // reduction, channel, bias=False),
            nn.Sigmoid()
        )

    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)
        return x * y. (x)

senet2
以上代码设计到的API:

  • AdaptiveAvgPool2d: 自适应平均池化,参数为(n,m)则将原来的feature(w,h)通过pooling得到(n,m)的feature,如果是(n),则将原来的feature从(w,h)通过pooling得到(n,n)
  • Sequential: torch容器,存放网络层等内容。
  • Linear: 线性层,参数为(in, out),将原有的in个feature转为out个feature
  • ReLU: 激活层, inplace进行原地操作,节省内存
  • Sigmoid: 激活层,将输入压缩到0-1
    分析forward进行模型的构建:
  • x是输入的feature,一般各个通道意义如下:(batch size,channel, width , height),这里获取了batch(b), channel
  • x通过AdaptiveAvgPool2d(1)以后将得到(batch size, channel, 1, 1), 然后view(b,c)意思是按照b,c进行展开

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    In [1]: import torch
    In [2]:  x = torch.zeros((16,256,256,256))
    In [3]:  import torch.nn as nn
    In [4]: avg_pool = nn.AdaptiveAvgPool2d(1)
    In [5]: avg_pool(x).shape
    Out[5]: torch.Size([16, 256, 1, 1])
    In [6]: avg_pool(x).view((16,256)).shape
    Out[6]: torch.Size([16, 256])
    In [7]: avg_pool(x).squeeze().shape # squeeze()函数也可以将所有通道个数为1的进行挤压
    Out[7]: torch.Size([16, 256])
    
  • 然后形状为【16, 256】的tensor经过fc:

  • (1) Linear: from 256(channel) to 256/16
  • (2) R
SENetCBAM都是用于图像分类的注意力机制模块,可以提高模型的性能。 SENet(Squeeze-and-Excitation Networks)通过引入一个称为“Squeeze-and-Excitation block”的模块来增强卷积神经网络的表示能力。SE block包含两个步骤:第一步是全局平均池化,将每个通道的特征图转换为一个标量;第二步是使用两个全连接层来学习每个通道的权重,以增强有用信息并削减噪声。SENet的优化是通过提高通道相关性来实现的。 CBAM(Convolutional Block Attention Module)是另一种注意力机制模块,它通过在通道和空间维度上使用注意力来提高卷积神经网络的性能。CBAM模块包含两个分支:一个分支用于学习通道级别的注意力,另一个分支用于学习空间级别的注意力。通过这种方式,CBAM可以提高模型的表示能力并消除噪声。 以下是SENetCBAMPytorch实现代码: SENet: ```python import torch.nn as nn import torch class SEBlock(nn.Module): def __init__(self, channel, reduction=16): super(SEBlock, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.fc = nn.Sequential( nn.Linear(channel, channel // reduction, bias=False), nn.ReLU(inplace=True), nn.Linear(channel // reduction, channel, bias=False), nn.Sigmoid() ) 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) return x * y.expand_as(x) ``` CBAM: ```python import torch.nn as nn import torch class ChannelAttention(nn.Module): def __init__(self, channel, reduction=16): super(ChannelAttention, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.max_pool = nn.AdaptiveMaxPool2d(1) self.fc1 = nn.Conv2d(channel, channel // reduction, 1, bias=False) self.relu1 = nn.ReLU() self.fc2 = nn.Conv2d(channel // reduction, channel, 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) class CBAM(nn.Module): def __init__(self, channel, reduction=16, kernel_size=7): super(CBAM, self).__init__() self.ChannelGate = ChannelAttention(channel, reduction=reduction) self.SpatialGate = SpatialAttention(kernel_size=kernel_size) def forward(self, x): x_out = self.ChannelGate(x) * x x_out = self.SpatialGate(x_out) * x_out return x_out ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值