《深入理解并实现Att U-Net》

Introduction

       《Attention U-Net: Learning Where to Look for the Pancreas》发表于2018CVPR,相对于定位+分割的级联方式实现的精准语义分割,通过添加attention gate的方式隐式的学习对于胰腺分割重要的特征,省略不重要的特征。

Network Architecture

       网络结构类似于U-net,区别是U-net在解码器和编码器同层级只进行拼接操作,此处则加入了attention gate,用于将同层级的encoder部分编码进行加权;

 

Attention gate

Attention的计算过程为:首先将decoder部分对应的输出g进行上采样+卷积(作用和反卷积类似),加卷积的目的是使两部分的channel数一致;然后分别使用1维卷积将g和x(来自于同层次的encoder)进行降维,channel数变为原来的1/2。然后两部分结果相加,经过激活函数,一维卷积将channel数降至1,然后经过sigmod函数,得到和x大小相同的1维的attention map,和原来的x做element-wise的乘法,得到加权后的向量;

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来解答你的问题。 首先,我们需要先了解一下Attention U-Net模型的结构。它是一种基于U-Net的图像分割模型,它利用了注意力机制来增强模型的分割能力。具体来说,Attention U-Net模型包含了编码器和解码器两个部分,其中编码器部分采用了类似U-Net的结构,而解码器部分则引入了注意力机制。 下面是使用PyTorch实现Attention U-Net模型的代码: ```python import torch import torch.nn as nn class ConvBlock(nn.Module): def __init__(self, in_channels, out_channels): super(ConvBlock, self).__init__() self.conv = nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1), nn.BatchNorm2d(out_channels), nn.ReLU(inplace=True), nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1), nn.BatchNorm2d(out_channels), nn.ReLU(inplace=True) ) def forward(self, x): return self.conv(x) class UpConvBlock(nn.Module): def __init__(self, in_channels, out_channels): super(UpConvBlock, self).__init__() self.up = nn.Sequential( nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True), nn.Conv2d(in_channels, out_channels, kernel_size=1), ) self.conv = ConvBlock(out_channels*2, out_channels) def forward(self, x, x_down): x_up = self.up(x) x_concat = torch.cat([x_down, x_up], dim=1) return self.conv(x_concat) class AttentionBlock(nn.Module): def __init__(self, in_channels): super(AttentionBlock, self).__init__() self.conv = nn.Conv2d(in_channels, in_channels//2, kernel_size=1) self.activation = nn.Sigmoid() def forward(self, x): x_gap = nn.AdaptiveAvgPool2d((1, 1))(x) x_conv = self.conv(x_gap) x_activation = self.activation(x_conv) x_attention = x * x_activation return x_attention class AttentionUNet(nn.Module): def __init__(self, in_channels, out_channels): super(AttentionUNet, self).__init__() self.down1 = ConvBlock(in_channels, 64) self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) self.down2 = ConvBlock(64, 128) self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) self.down3 = ConvBlock(128, 256) self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2) self.down4 = ConvBlock(256, 512) self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2) self.bridge = ConvBlock(512, 1024) self.up1 = UpConvBlock(1024, 512) self.att1 = AttentionBlock(512) self.up2 = UpConvBlock(512, 256) self.att2 = AttentionBlock(256) self.up3 = UpConvBlock(256, 128) self.att3 = AttentionBlock(128) self.up4 = UpConvBlock(128, 64) self.out = nn.Conv2d(64, out_channels, kernel_size=1) def forward(self, x): x1 = self.down1(x) x_pool1 = self.pool1(x1) x2 = self.down2(x_pool1) x_pool2 = self.pool2(x2) x3 = self.down3(x_pool2) x_pool3 = self.pool3(x3) x4 = self.down4(x_pool3) x_pool4 = self.pool4(x4) x_bridge = self.bridge(x_pool4) x_up1 = self.up1(x_bridge, x4) x_att1 = self.att1(x_up1) x_up2 = self.up2(x_att1, x3) x_att2 = self.att2(x_up2) x_up3 = self.up3(x_att2, x2) x_att3 = self.att3(x_up3) x_up4 = self.up4(x_att3, x1) x_out = self.out(x_up4) return x_out ``` 在上面的代码中,我们定义了ConvBlock、UpConvBlock、AttentionBlock以及AttentionUNet这几个模块。ConvBlock和UpConvBlock分别用于构建编码器和解码器中的卷积块和上采样卷积块,AttentionBlock用于实现注意力机制,AttentionUNet则是整个Attention U-Net模型的实现。 在AttentionUNet中,我们首先定义了编码器部分,它由4个卷积块和4个池化层组成。接着是桥接器,它是一个卷积块,用于将编码器的输出转换为解码器的输入。解码器部分由4个上采样卷积块和4个注意力块组成,注意力块用于增强模型的分割能力。最后,我们通过一个卷积层将模型的输出转换为与分割目标相同大小的二进制掩模。 希望这个代码能够对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值