注意力机制之Squeeze-and-Excitation Attention

Squeeze-and-Excitation(SE)Networksintroduceamodulethatimprovesfeaturelearninginconvolutionalneuralnetworks.TheSEmoduleconsistsofaSqueezestep,whichcapturesglobalinformationperchannel,andanExcitationstep,whichadaptivelyrecalibrateschannel-wisefeatureresponses.Byenhancingfeaturerepresentationsthroughthesetwocomponents,SENetsboostthenetworksabilitytodistinguishandprocessfeatureseffectively.

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Squeeze-and-Excitation Attention模块

链接:Squeeze-and-Excitation Networks

  1. 模型结构图

  1. 论文主要核心思想

Squeeze-and-Excitation Networks(SENets)是一种网络架构,旨在改善网络的特征计算。该架构包括两个主要组件:Squeeze(压缩)和Excitation(激发)。Squeeze组件负责通过学习分析每个通道的全局信息,从而提取出每个通道的有用特征。这是通过让每个通道的特征向量通过一个全局池化层,从而将特征向量压缩成一个固定维度的特征表示。Excitation组件负责改变每个通道的特征表示。这是通过将Squeeze组件输出的特征表示与每个通道的特征向量做乘法,从而加权每个通道的特征向量,从而改变每个通道的特征表示。

总的来说,SENets的核心思想是,通过将每个通道的特征向量通过Squeeze组件进行压缩,并且通过Excitation组件来加权每个通道的特征表示,从而提高网络的特征计算能力。

import numpy as np
import torch
from torch import nn
from torch.nn import init


# 定义一个SEAttention类,继承自nn.Module
class SEAttention(nn.Module):
    # 初始化函数,设置默认参数channel=512,reduction=16
    def __init__(self, channel=512,reduction=16):
        super().__init__()
        # 定义一个自适应平均池化层,使用1x1的池化核
        self.avg_pool = nn.AdaptiveAvgPool2d(1)

        #定义全连接层,包括两个线性层,一个ReLu激活函数和一个Sigmoid激活函数
        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 init_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                init.kaiming_normal_(m.weight, mode='fan_out')
                if m.bias is not None:
                    init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                init.constant_(m.weight, 1)
                init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                init.normal_(m.weight, std=0.001)
                if m.bias is not None:
                    init.constant_(m.bias, 0)

    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)


if __name__ == '__main__':
    # 定义一个随机输入
    input=torch.randn(50,512,7,7)
    se = SEAttention(channel=512,reduction=8)
    output=se(input)
    print(output.shape)

### 循环挤压-激励上下文聚合网络在单张图像去雨中的应用 #### 网络结构概述 Recurrent Squeeze-and-Excitation Context Aggregation Network (RESCAN) 是一种专门针对单张图像去雨设计的深度学习模型。该网络旨在通过递归机制有效去除图像中的雨痕,从而提高图像质量[^3]。 #### 特征提取模块 输入含雨图像后,首先利用改进版 DenseNet 提取全局特征。此版本的 DenseNet 移除了转换层(1 Conv + 1 Pooling),因此不会执行下采样操作,有助于保留更多细节信息。 #### 子网工作流程 所提取的整体特征被送入多个并行工作的子网络中。每个子网络负责估算特定尺度下的雨纹图 \( R_i \),并通过累加获得当前阶段总的雨纹图。随后,将原图与所得雨纹图做减法运算得出初步清理后的图片。上述过程会迭代多次,在每一新轮次里以前一轮处理过的影像作为输入继续优化直至最终产出完全无雨斑点的照片。 #### 关键组件分析 - **循环卷积层**:为了更好地捕捉时间序列特性以及增强记忆能力,采用了具备 ResNet 快捷链接特性的循环卷积单元。 - **Squeeze-and-Excitation (SE)**:引入 SE 结构可以自动调整通道间权重分配,使得重要区域获得更多关注资源,进而改善视觉表现力[^5]。 - **未使用 Batch Normalization**:考虑到 BN 可能破坏原有空间关联性,并且增加额外计算负担,故而在本框架内舍弃了这项技术。实验证明这样做不仅提升了性能指标还减少了约40% 的 GPU 显存占用量[^4]。 ```python import torch.nn as nn class RESCAN(nn.Module): def __init__(self, num_stages=4): super(RESCAN, self).__init__() # Define feature extraction module based on modified DenseNet without transition layers # Define recurrent sub-networks using recursive convolutional units with ResNet shortcuts # Implement squeeze-and-excitation mechanism to enhance channel-wise attention def forward(self, x): features = self.feature_extraction(x) rain_maps = [] clean_image = None for stage in range(num_stages): current_rain_map = self.sub_network(features) rain_maps.append(current_rain_map) if stage == 0: clean_image = x - sum(rain_maps) else: clean_image = clean_image - sum(rain_maps[stage:]) return clean_image ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

果粒橙_LGC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值