ELA(Efficient Local Attention)注意力机制

本文介绍了一种新的随插随用注意力机制ELA,针对CA模块的不足进行改进。文章详细介绍了ELA的不同变体(ELA-T,ELA-B,ELA-S,ELA-L),并提供了其在不同网络结构中的适用性。代码展示了如何在PyTorch中实现ELA模块。

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

2024年出了一个新的随插随用的注意力机制ELA(Efficient Local Attention)。

具体结构:

(a)SE模块

(b)CA模块

(c)ELA模块

文中总结了CA模块的不足之处并针对CA的缺点提出了ELA模块。

为了优化 ELA 的性能,同时考虑参数数量(参数为:conv1d中的Kernel_size,groups,GN中的num_groups),文中引入了四种方案:

ELA-Tiny(ELA-T), ELA-Base(ELAB), ELA-Small(ELA-S), ELA-Large(ELA-L)

代码实现:

import torch
import torch.nn as nn


class ELA(nn.Module):
    def __init__(self, in_channels, phi):
        super(ELA, self).__init__()
        '''
        ELA-T 和 ELA-B 设计为轻量级,非常适合网络层数较少或轻量级网络的 CNN 架构
        ELA-B 和 ELA-S 在具有更深结构的网络上表现最佳
        ELA-L 特别适合大型网络。
        '''
        Kernel_size = {'T': 5, 'B': 7, 'S': 5, 'L': 7}[phi]
        groups = {'T': in_channels, 'B': in_channels, 'S': in_channels//8, 'L': in_channels//8}[phi]
        num_groups = {'T': 32, 'B': 16, 'S': 16, 'L': 16}[phi]
        pad = Kernel_size//2
        self.con1 = nn.Conv1d(in_channels, in_channels, kernel_size=Kernel_size, padding=pad, groups=groups, bias=False)
        self.GN = nn.GroupNorm(num_groups, in_channels)
        self.sigmoid = nn.Sigmoid()

    def forward(self, input):
        b, c, h, w = input.size()
        x_h = torch.mean(input, dim=3, keepdim=True).view(b,c,h)
        x_w = torch.mean(input, dim=2, keepdim=True).view(b,c,w)
        x_h = self.con1(x_h)    # [b,c,h]
        x_w = self.con1(x_w)    # [b,c,w]
        x_h = self.sigmoid(self.GN(x_h)).view(b, c, h, 1)   # [b, c, h, 1]
        x_w = self.sigmoid(self.GN(x_w)).view(b, c, 1, w)   # [b, c, 1, w]
        return x_h * x_w * input


if __name__ == "__main__":

    # 创建一个形状为 [batch_size, channels, height, width] 的虚拟输入张量
    input = torch.randn(2, 256, 40, 40)
    ela = ELA(in_channels=256, phi='T')
    output = ela(input)
    print(output.size())

### 将ELA集成到ResNet模型中用于图像处理或伪造检测 #### 背景介绍 为了提高图像篡改检测的效果,可以考虑将误差级别分析(ELA)技术与深度卷积神经网络相结合。具体来说,通过将ELA应用于输入图像并将其作为额外通道提供给预训练好的ResNet模型来增强特征表示能力。 #### 方法描述 1. **准备数据集** 对于每张原始图片,先应用JPEG压缩再解压得到重建后的版本;接着计算两者之间的绝对差异值矩阵,并按比例缩放至[0,255]区间形成灰度图形式的ELA Map[^1]。 2. **构建多模态输入管道** 修改标准RGB三通道结构,在原有基础上附加一个由上述过程产生的单通道ELA映射构成四维张量作为新的输入格式喂入后续层继续训练优化流程。 3. **调整ResNet架构适应新输入维度** 鉴于增加了额外的信息源,需相应修改初始卷积核大小以及步幅参数确保能够有效提取融合特征而不丢失重要细节。 4. **微调超参数配置** 结合实际应用场景需求合理设置批量尺寸、迭代次数等关键因素以期获得最佳性能表现指标。 ```python import torch from torchvision import models class ResNetWithELA(torch.nn.Module): def __init__(self, num_classes=1000): super(ResNetWithELA, self).__init__() # Load pretrained resnet model without fully connected layer base_model = models.resnet50(pretrained=True) modules = list(base_model.children())[:-1] self.feature_extractor = torch.nn.Sequential(*modules) # Adjust the input channels from 3(RGB) to 4(RGB+ELA) self.conv1 = torch.nn.Conv2d( in_channels=4, out_channels=64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False ) # Replace original conv1 with modified one self.feature_extractor[0] = self.conv1 # Add custom classifier head self.classifier = torch.nn.Linear(in_features=2048, out_features=num_classes) def forward(self, x_ela): features = self.feature_extractor(x_ela) flattened = torch.flatten(features, start_dim=1) output = self.classifier(flattened) return output ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值