Unet改进4:在不同位置添加GAMAttention注意力机制,保留信息以增强通道-空间相互作用

本文内容:在不同位置添加GAMAttention注意力机制

论文简介

为了提高各种计算机视觉任务的性能,人们研究了各种注意机制。然而,以往的方法忽略了同时保留通道和空间方面的信息对增强跨维交互的重要性。因此,我们提出了一种全局注意机制,通过减少信息约简和放大全局交互表征来提高深度神经网络的性能。我们引入了带有多层感知器的3d排列,用于通道注意以及卷积空间注意子模块。在CIFAR-100和ImageNet-1K上对所提出的图像分类机制进行的评估表明,我们的方法在ResNet和轻量级MobileNet上稳定地优于最近的几种注意力机制。

1.步骤一

新建blocks/GAM.py文件,添加如下代码:

import torch
import torch.nn as nn

#######GAM
class GAMAttention(nn.Module):
    # https://paperswithcode.com/paper/global-attention-mechanism-retain-information
    def __init__(self, c1, c2, group=True, rate=4):
        super(GAMAttention, self).__init__()

        self.channel_attention = nn.Sequential(
            nn.Linear(c1, int(c1 / rate)),
            nn.ReLU(in
  • 17
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
UNet模型中添加注意力机制的代码通常放在模型的构建部分。具体来说,可以在UNet的编码器和解码器之间添加注意力模块,以便在特定部分增强模型的分割性能。 以下是一个示例代码片段,其中添加注意力机制: ``` import torch import torch.nn as nn class Attention(nn.Module): def __init__(self, in_channels): super(Attention, self).__init__() self.conv = nn.Conv2d(in_channels=in_channels, out_channels=1, kernel_size=1) self.sigmoid = nn.Sigmoid() def forward(self, x): x = self.conv(x) x = self.sigmoid(x) return x class UNet(nn.Module): def __init__(self, in_channels=3, out_channels=1, features=[64, 128, 256, 512]): super(UNet, self).__init__() self.encoder = nn.ModuleList() self.decoder = nn.ModuleList() self.pool = nn.MaxPool2d(kernel_size=2, stride=2) # Encoder for feature in features: self.encoder.append( nn.Sequential( nn.Conv2d(in_channels, feature, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(feature, feature, kernel_size=3, padding=1), nn.ReLU(inplace=True) ) ) in_channels = feature # Attention self.attention = Attention(features[-1]) # Decoder for feature in reversed(features): self.decoder.append( nn.Sequential( nn.Conv2d(feature*2, feature, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(feature, feature, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.ConvTranspose2d(feature, feature//2, kernel_size=2, stride=2), ) ) self.decoder.append( nn.Sequential( nn.Conv2d(feature, feature//2, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(feature//2, feature//2, kernel_size=3, padding=1), nn.ReLU(inplace=True), ) ) feature //= 2 # Output self.output = nn.Conv2d(64, out_channels, kernel_size=1) def forward(self, x): skip_connections = [] # Encoder for encoder in self.encoder: x = encoder(x) skip_connections.append(x) x = self.pool(x) # Attention x = self.attention(x) # Decoder for i in range(0, len(self.decoder), 2): decoder = self.decoder[i] skip_connection = skip_connections[-(i//2+2)] x = decoder(x) x = torch.cat([x, skip_connection], dim=1) x = self.decoder[i+1](x) # Output x = self.output(x) return x ``` 在这个例子中,注意力模块被添加UNet模型的最后一个编码器之后。具体来说,我们定义了一个名为Attention的新模块,并在UNet的初始化函数中创建了一个实例。在前向传递中,我们将编码器输出传递给Attention模块,并将其输出乘以编码器的最终输出。这样,注意力机制可以集中模型的注意力在最相关的特征图上,从而提高分割性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AICurator

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

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

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

打赏作者

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

抵扣说明:

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

余额充值