Attention UNet

article/2025/10/15 0:37:08

Attention UNet论文解析 - 知乎Attention UNet论文地址: https://arxiv.org/pdf/1804.03999.pdf 代码地址: https://github.com/ozan-oktay/Attention-Gated-NetworksAttention UNet在UNet中引入注意力机制,在对编码器每个分辨率上的特征与解…https://zhuanlan.zhihu.com/p/480384295

图像分割UNet系列------Attention Unet详解_gz7seven的博客-CSDN博客_attention unet图像分割unet系列------Attention Unet详解1、Attention Unet主要目标2、Attention Unet网络结构    Attention Unet发表于2018年中期(比Res-UNet要早一些),它也是UNet非常重要的改进版本之一。当然,Attention Unet同样也是应用在医学图像分割领域,不过文章主要是以胰脏图像分割进行实验与论证的。1、Attention Unet主要目标    作者在摘要与简介中很清楚的表明了要解决的问题以及要达到的目标。具体如下所示:https://gz7seven.blog.csdn.net/article/details/119612308?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~Rate-6-119612308-blog-118300513.pc_relevant_multi_platform_whitelistv3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~Rate-6-119612308-blog-118300513.pc_relevant_multi_platform_whitelistv3&utm_relevant_index=10

一、Attention Unet主要目标

  1. 抑制输入图像中的不相关区域,同时突出特定局部区域的显著特征
  2. 用soft-attention 代替hard-attention的思路(注意:sorf-attention可微,可以微分的attention就可以通过神经网络算出梯度并且前向传播和后向反馈来学习得到attention的权重);
  3. 集成到标准UNet网络结构中时要简单方便、计算开销小,最重要的是提高模型的灵敏度和预测的精度。

二、Attention Unet网络结构

Attention UNet在UNet中引入注意力机制,在对编码器每个分辨率上的特征与解码器中对应特征进行拼接之前,使用了一个注意力模块,重新调整了编码器的输出特征。该模块生成一个门控信号,用来控制不同空间位置处特征的重要性,如下图中红色圆圈所示

三、Attention Gate

Attention Gate:AG通常用于自然图像分析、知识图和语言处理(NLP),用于图像字幕、机器翻译和分类任务。最初的工作是通过解释输出类分数相对于输入图像的梯度来探索注意图。另一方面,可训练的注意力是由设计强制执行的,并被分为hard-attention and soft-attention。

结合图1与图2可以很清楚的了解到Attention UNet网络结构的主要特点。从图1可以很清楚的看到解码部分feature map与其上一层的编码部分feature map作为AG的输入,经过AG后将结果cat上采样的解码部分feature map。

 四、Attention Gate的本质

AG是如何做到提高局部(感兴趣区域ROI)特征抑制某些非感兴趣区域的呢?

 

五、代码实现

# python3
# @File: AttentionUNet3D.py
# --coding:utf-8--
# @Author:axjing
# 说明: For 3D Data Train
import torch
import torch.nn as nndef maxpool2x2(x):mp = nn.MaxPool3d(kernel_size=2, stride=2)x = mp(x)return x
class EncoderBlock(nn.Module):def __init__(self, in_channels, out_channels):super(EncoderBlock, self).__init__()self.encoder_block = nn.Sequential(nn.Conv3d(in_channels, out_channels, 3, padding=1, bias=False),nn.BatchNorm3d(num_features=out_channels),nn.ReLU(inplace=True),nn.Conv3d(out_channels, out_channels, 3, padding=1, bias=False),nn.BatchNorm3d(num_features=out_channels),nn.ReLU(inplace=True),# 原论文中是每层有两个卷积核进行下采样 https://arxiv.org/abs/1505.04597nn.Conv3d(out_channels, out_channels, 3, padding=1, bias=False),nn.BatchNorm3d(num_features=out_channels),nn.ReLU(inplace=True),)def forward(self, x):x = self.encoder_block(x)return xclass CenterBlock(nn.Module):def __init__(self, in_channels, out_channels):super(CenterBlock, self).__init__()mid_channels = int(in_channels * 2)self.center_block = nn.Sequential(nn.Conv3d(in_channels, mid_channels, 3, padding=1, bias=False),nn.BatchNorm3d(num_features=mid_channels),nn.ReLU(inplace=True),nn.Conv3d(int(in_channels * 2), mid_channels, 3, padding=1, bias=False),nn.BatchNorm3d(num_features=mid_channels),nn.ReLU(inplace=True),nn.ConvTranspose3d(mid_channels, out_channels, 4, stride=2, padding=1),nn.BatchNorm3d(num_features=out_channels),nn.ReLU(inplace=True),)def forward(self, x):x = self.center_block(x)return xclass DecoderBlock(nn.Module):def __init__(self, in_channels, out_channels):super(DecoderBlock, self).__init__()mid_channels = int(in_channels / 2)self.decoder_block = nn.Sequential(nn.Conv3d(in_channels, mid_channels, 3, padding=1, bias=False),nn.BatchNorm3d(num_features=mid_channels),nn.ReLU(inplace=True),nn.Conv3d(mid_channels, mid_channels, 3, padding=1, bias=False),nn.BatchNorm3d(num_features=mid_channels),nn.ReLU(inplace=True),nn.ConvTranspose3d(mid_channels, out_channels, 4, stride=2, padding=1),nn.BatchNorm3d(num_features=out_channels),nn.ReLU(inplace=True),)def forward(self, x):x = self.decoder_block(x)return xclass FinalBlock(nn.Module):def __init__(self, in_channels, out_channels):super(FinalBlock, self).__init__()mid_channels = int(in_channels / 2)self.final_block = nn.Sequential(nn.Conv3d(in_channels, mid_channels, 3, padding=1, bias=False),nn.BatchNorm3d(num_features=mid_channels),nn.ReLU(inplace=True),nn.Conv3d(mid_channels, mid_channels, 3, padding=1, bias=False),nn.BatchNorm3d(num_features=mid_channels),nn.ReLU(inplace=True),nn.Conv3d(mid_channels, out_channels, 1, bias=False),)def forward(self, x):x = self.final_block(x)return xclass AttentionGates3D(nn.Module):def __init__(self, F_g, F_l, F_int):super(AttentionGates3D, self).__init__()self.W_g = nn.Sequential(nn.Conv3d(F_l, F_int, kernel_size=1, stride=1, padding=0, bias=True),nn.BatchNorm3d(F_int))self.W_x = nn.Sequential(nn.Conv3d(F_g, F_int, kernel_size=1, stride=1, padding=0, bias=True),nn.BatchNorm3d(F_int))self.psi = nn.Sequential(nn.Conv3d(F_int, 1, kernel_size=1, stride=1, padding=0, bias=True),nn.BatchNorm3d(1),nn.Sigmoid())self.relu = nn.ReLU(inplace=True)def forward(self, g, x):g1 = self.W_g(g)x1 = self.W_x(x)psi = self.relu(g1 + x1)psi = self.psi(psi)out = x * psireturn outclass AttentionUNet3D(nn.Module):def __init__(self, in_channels, out_channels):super(AttentionUNet3D, self).__init__()# Encoder Partself.encoder1 = EncoderBlock(in_channels=in_channels, out_channels=64)self.encoder2 = EncoderBlock(in_channels=64, out_channels=128)self.encoder3 = EncoderBlock(in_channels=128, out_channels=256)self.encoder4 = EncoderBlock(in_channels=256, out_channels=512)# Center part.self.center = CenterBlock(in_channels=512, out_channels=512)# Decoder part.self.decoder4 = DecoderBlock(in_channels=1024, out_channels=256)self.decoder3 = DecoderBlock(in_channels=512, out_channels=128)self.decoder2 = DecoderBlock(in_channels=256, out_channels=64)# Final part.self.final = FinalBlock(in_channels=128, out_channels=out_channels)# Attention Gateself.att4 = AttentionGates3D(F_g=512, F_l=512, F_int=256)self.att3 = AttentionGates3D(F_g=256, F_l=256, F_int=128)self.att2 = AttentionGates3D(F_g=128, F_l=128, F_int=64)self.att1 = AttentionGates3D(F_g=64, F_l=64, F_int=32)def forward(self, x):# Encoding, compressive pathway.out_encoder1 = self.encoder1(x)out_endocer1_mp = maxpool2x2(out_encoder1)out_encoder2 = self.encoder2(out_endocer1_mp)out_endocer2_mp = maxpool2x2(out_encoder2)out_encoder3 = self.encoder3(out_endocer2_mp)out_endocer3_mp = maxpool2x2(out_encoder3)out_encoder4 = self.encoder4(out_endocer3_mp)# Decoding, expansive pathway.out_endocer4_mp = maxpool2x2(out_encoder4)out_center = self.center(out_endocer4_mp)out_att4 = self.att4(g=out_center, x=out_encoder4)out_decoder4 = self.decoder4(torch.cat((out_center, out_att4), 1))out_att3 = self.att3(g=out_decoder4, x=out_encoder3)out_decoder3 = self.decoder3(torch.cat((out_decoder4, out_att3), 1))out_att2 = self.att2(g=out_decoder3, x=out_encoder2)out_decoder2 = self.decoder2(torch.cat((out_decoder3, out_att2), 1))out_att1 = self.att1(g=out_decoder2, x=out_encoder1)out_final = self.final(torch.cat((out_decoder2, out_att1), 1))return out_finalif __name__ == '__main__':data_=torch.rand(1,1,64,64,64)model=AttentionUNet3D(in_channels=1,out_channels=2)print(model)out_data=model(data_)print(out_data.shape)print("*" * 30 + "\n |\t\tEnd Of Program\t\t|\n" + "*" * 30)

六、Attention Unet注意力机制是空间注意力还是通道注意力?

通道注意力和空间注意力,分别捕捉通道间的依赖关系和空间上的像素级关系,同时使用这两类注意力机制可以达到更好的效果(例如CBAM),但无疑增加了更多的计算量。

CBAM(convolutional block attention modules)是一个卷积块注意力模块,作用于输入图像,按照顺序将注意力机制应用于通道,然后是空间维度。CBAM的结果是一个加权的特征图,考虑了输入图像的通道和空间区域。


http://chatgpt.dhexx.cn/article/5F1DE8eu.shtml

相关文章

attention

文章目录 Attention基本的Attention原理参考 Hierarchical Attention原理实践参考 Self Attentionother Attention Attention Attention是一种机制,可以应用到许多不同的模型中,像CNN、RNN、seq2seq等。Attention通过权重给模型赋予了区分辨别的能力&am…

史上最小白之Attention详解

1.前言 在自然语言处理领域,近几年最火的是什么?是BERT!谷歌团队2018提出的用于生成词向量的BERT算法在NLP的11项任务中取得了非常出色的效果,堪称2018年深度学习领域最振奋人心的消息。而BERT算法又是基于Transformer&#xff0…

一文看懂 Attention(本质原理+3大优点+5大类型)

Attention 正在被越来越广泛的得到应用。尤其是 BERT 火爆了之后。 Attention 到底有什么特别之处?他的原理和本质是什么?Attention都有哪些类型?本文将详细讲解Attention的方方面面。 Attention 的本质是什么 Attention(注意力&a…

史上最直白之Attention详解(原理+代码)

目录 为什么要了解Attention机制Attention 的直观理解图解深度学习中的Attention机制总结 为什么要了解Attention机制 在自然语言处理领域,近几年最火的是什么?是BERT!谷歌团队2018提出的用于生成词向量的BERT算法在NLP的11项任务中取得了非常…

关于Attention的超详细讲解

文章目录 一、动物的视觉注意力二、快速理解Attention思想三、从Encoder-Decoder框架中理解为什么要有Attention机制四、Attention思想步骤五、Self-Attention5.1 Self-Attention的计算步骤5.2 根据代码进一步理解Q、K、V5.3 再来一个例子理解 六、缩放点积中为什么要除以根号d…

浅聊古代————汉朝

文章目录 西汉西汉建立:汉高祖刘邦西汉灭亡:王莽篡汉新朝建立:王莽篡汉改新新朝灭亡:绿林赤眉起义 东汉东汉建立:起义军刘秀东汉灭亡:曹丕篡汉 西汉 西汉建立:汉高祖刘邦 刘邦被项羽封为汉王。…

XNU简介

XNU内核比较庞大复杂,因此这里只是进行主要内容的介绍 XNU据说是一个无限递归的缩写:XNU’s Not UNIX XNU内核是Mac和iOS的核心,有三个主要部分组成的一个分层体系结构;内核XNU是Darwin的核心,也是整个OS X的核心。 …

函数

1.编写函数,完成下面的程序,将一个字符串中的字母排序后输出。输出要求:字母从小到大排序(包括大小写)后的字符串。例如: 输入:Hello World! 输出:!HWdellloor 程序中的必要代码为: main() …

HangOver

How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We’re assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang t…

❤️对比PyWinAuto和uiautomation实现微信联系人自动采集❤️

大家好,我是小小明。昨天我在《UI自动化工具轻松实现微信消息的自动收发和朋友圈爬取》一文中演示了UIAutomation的三个使用示例,链接:https://blog.csdn.net/as604049322/article/details/119899542 由于昨天对UIAutomation的API了解还不够全面,个别代码优化空间还较大。…

适用于winform程序的UI自动化实现(python+uiautomation)

适用于winform程序的UI自动化实现(pythonuiautomation) 一、使用的工具和库 定位工具:inspect(提取码:kvwx)、UISpy(提取码:ab0e) python库使用:subprocess(内置库&…

WPF UIAutomation测试套件开发

目录 介绍 目标 测试方案 功能测试(黑匣子) 测试示例 设计 记录测试并生成测试方法 更新测试方法并构建 测试用例规范 测试方法 初始化方法 实现自定义TestContext 测试配置 关于CodedUI的常见问题 不要手动更改* .Designer.cs文件中的代…

安卓手机 Python 自动化( uiautomation、uiautomation2、weditor )

其他自动化工具或者框架: Airtest:https://airtest.readthedocs.io/zh_CN/latest/autojs:Auto.js快速入门实战教程:https://zhuanlan.zhihu.com/p/90065914appium:https://blog.csdn.net/freeking101/article/details/…

UIAutomation识别UI元素

MS UI Automation(Microsoft User Interface Automation:UIA)是随.net framework3.0一起发布的,虽然在如今这个几乎每天都有各种新名词、新技术出来的所谓的21世纪,它显得已经有些过时了。前些日子,正好一个项目,可以用…

uiautomation实现自动化

1.下载UI SPY 下载地址:https://download.csdn.net/download/yangzhichao_csdn/85389759 2.CMD指令导入uiautomation库 pip install uiautomation 3.打开计算器,然后打开UISpy 如图ControlType为”ControlType.Window” 那么捕获窗口就用:uiautomation.WindowCo…

Python uiautomation初探,测试Win10计算器

在网上找 Python 客户端自动化测试的库,一般有 pywinauto、pywin32、pyautogui、uiautomation。本文仅对 uiautomation 做简单的了解。 MS UI Automation 是 MSAA 技术的一个替代品:即让控件和应用程序具有更好的可达性(accessible&#xff…