UNet 改进(2):深入解析带有坐标注意力机制(CA)的UNet网络
UNet是一种经典的编码器-解码器结构神经网络,广泛应用于图像分割任务。本文将详细解析一个改进版的UNet实现,它在传统UNet基础上加入了坐标注意力(Coordinate Attention, CA)机制,能够更好地捕捉空间位置信息,提升分割性能。
一、网络结构概览
这个UNet实现包含以下几个主要组件:
-
坐标注意力模块(CA):增强网络对空间位置信息的感知能力
-
双卷积模块(DoubleConv):基础卷积单元
-
下采样模块(Down):编码器部分,逐步提取高层特征
-
上采样模块(Up):解码器部分,逐步恢复空间分辨率
-
输出卷积模块(OutConv):生成最终分割结果
二、坐标注意力机制(CA)
坐标注意力是一种轻量级的注意力机制,能够有效地捕捉空间位置信息:
class CA(nn.Module):
def __init__(self, in_channels, reduction=16):
super(CA, self).__init__()
self.pool_h = nn.AdaptiveAvgPool2d((None, 1))
self.pool_w = nn.AdaptiveAvgPool2d((1, None))
mip = max(8, in_channels // reduction)
self.conv1 = nn.Conv2d(in_channels, mip, kernel_size=1, stride=1, padding=0)
self.bn1 = nn.BatchNorm2d(mip)
self.act = nn.ReLU(inplace=True)
self.conv_h = nn.Conv2d(mip, in_channels, kernel_size=1, stride=1, padding=0)
self.conv_w = nn.Conv2d(mip, in_channels, kernel_size=1, stride=1, padding=0)
CA模块的工作原理:
-
分别使用高度和宽度方向的全局平均池化(
pool_h
和pool_w
)来捕获空间信息 -
将两个方向的池化结果拼接并通过1x1卷积进行特征交互
-
使用BN和ReLU进行非线性变换
-
分离出高度和宽度注意力图
-
将注意力图应用于输入特征,增强重要位置的特征响应
这种设计使网络能够精确地定位感兴趣区域,同时保持较低的计算开销。
三、双卷积模块(DoubleConv)
class DoubleConv(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.double_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)
)
DoubleConv是UNet的基础构建块,包含两个连续的3x3卷积层,每个卷积层后都有批量归一化和ReLU激活。这种设计能够有效提取局部特征,同时保持特征的稳定性。
四、下采样模块(Down)
class Down(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.maxpool_conv = nn.Sequential(
nn.MaxPool2d(2),
DoubleConv(in_channels, out_channels)
)
下采样模块由最大池化层和双卷积模块组成,实现特征图的空间下采样和通道扩展。在UNet的编码器部分,通过多个下采样模块逐步提取高层语义特征。
五、上采样模块(Up)
class Up(nn.Module):
def __init__(self, in_channels, out_channels, bilinear=True):
super().__init__()
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
else:
self.up = nn.ConvTranspose2d(in_channels // 2, in_channels // 2, kernel_size=2, stride=2)
self.conv = DoubleConv(in_channels, out_channels)
上采样模块负责恢复特征图的空间分辨率,支持两种上采样方式:
-
双线性插值:计算效率高
-
转置卷积:可学习的上采样方式
上采样后会与编码器对应层的特征图进行拼接(跳跃连接),然后通过双卷积模块进行特征融合。
六、输出卷积模块(OutConv)
class OutConv(nn.Module):
def __init__(self, in_channels, out_channels):
super(OutConv, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)
简单的1x1卷积,将特征图映射到目标类别数,生成最终的分割结果。
七、UNet整体架构
class UNet(nn.Module):
def __init__(self, n_channels, n_classes, bilinear=True):
super(UNet, self).__init__()
self.inc = DoubleConv(n_channels, 64)
self.ca = CA(64)
self.down1 = Down(64, 128)
self.down2 = Down(128, 256)
self.down3 = Down(256, 512)
factor = 2 if bilinear else 1
self.down4 = Down(512, 1024 // factor)
self.up1 = Up(1024, 512 // factor, bilinear)
self.up2 = Up(512, 256 // factor, bilinear)
self.up3 = Up(256, 128 // factor, bilinear)
self.up4 = Up(128, 64, bilinear)
self.outc = OutConv(64, n_classes)
完整的UNet结构包含:
-
初始双卷积层(inc)
-
坐标注意力层(ca)
-
4个下采样层(down1-down4)
-
4个上采样层(up1-up4)
-
输出层(outc)
八、前向传播过程
def forward(self, x):
x = self.inc(x)
x = self.ca(x)
x1 = x
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
logits = self.outc(x)
return logits
前向传播流程清晰展示了UNet的对称结构:
-
输入图像通过初始卷积和CA模块
-
编码器部分逐步下采样提取特征
-
解码器部分逐步上采样恢复分辨率
-
跳跃连接融合高低层特征
-
输出最终分割结果
九、完整代码
import torch
import torch.nn as nn
# Coordinate Attention (CA)
class CA(nn.Module):
def __init__(self, in_channels, reduction=16):
super(CA, self).__init__()
self.pool_h = nn.AdaptiveAvgPool2d((None, 1))
self.pool_w = nn.AdaptiveAvgPool2d((1, None))
mip = max(8, in_channels // reduction)
self.conv1 = nn.Conv2d(in_channels, mip, kernel_size=1, stride=1, padding=0)
self.bn1 = nn.BatchNorm2d(mip)
self.act = nn.ReLU(inplace=True)
self.conv_h = nn.Conv2d(mip, in_channels, kernel_size=1, stride=1, padding=0)
self.conv_w = nn.Conv2d(mip, in_channels, kernel_size=1, stride=1, padding=0)
def forward(self, x):
identity = x
b, c, h, w = x.size()
x_h = self.pool_h(x)
x_w = self.pool_w(x).permute(0, 1, 3, 2)
y = torch.cat([x_h, x_w], dim=2)
y = self.conv1(y)
y = self.bn1(y)
y = self.act(y)
x_h, x_w = torch.split(y, [h, w], dim=2)
x_w = x_w.permute(0, 1, 3, 2)
a_h = self.conv_h(x_h).sigmoid()
a_w = self.conv_w(x_w).sigmoid()
out = identity * a_w * a_h
return out
class DoubleConv(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.double_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.double_conv(x)
class Down(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.maxpool_conv = nn.Sequential(
nn.MaxPool2d(2),
DoubleConv(in_channels, out_channels)
)
def forward(self, x):
return self.maxpool_conv(x)
class Up(nn.Module):
def __init__(self, in_channels, out_channels, bilinear=True):
super().__init__()
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
else:
self.up = nn.ConvTranspose2d(in_channels // 2, in_channels // 2, kernel_size=2, stride=2)
self.conv = DoubleConv(in_channels, out_channels)
def forward(self, x1, x2):
x1 = self.up(x1)
diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]
x1 = nn.functional.pad(x1, [diffX // 2, diffX - diffX // 2,
diffY // 2, diffY - diffY // 2])
x = torch.cat([x2, x1], dim=1)
return self.conv(x)
class OutConv(nn.Module):
def __init__(self, in_channels, out_channels):
super(OutConv, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)
def forward(self, x):
return self.conv(x)
class UNet(nn.Module):
def __init__(self, n_channels, n_classes, bilinear=True):
super(UNet, self).__init__()
self.n_channels = n_channels
self.n_classes = n_classes
self.bilinear = bilinear
self.inc = DoubleConv(n_channels, 64)
self.ca = CA(64)
self.down1 = Down(64, 128)
self.down2 = Down(128, 256)
self.down3 = Down(256, 512)
factor = 2 if bilinear else 1
self.down4 = Down(512, 1024 // factor)
self.up1 = Up(1024, 512 // factor, bilinear)
self.up2 = Up(512, 256 // factor, bilinear)
self.up3 = Up(256, 128 // factor, bilinear)
self.up4 = Up(128, 64, bilinear)
self.outc = OutConv(64, n_classes)
def forward(self, x):
x = self.inc(x)
x = self.ca(x)
x1 = x
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
logits = self.outc(x)
return logits
十、创新点与优势
-
坐标注意力机制:增强网络对空间位置信息的感知能力,有助于精确定位目标边界
-
双线性上采样选项:可以根据需求选择计算效率高的插值方式或可学习的转置卷积
-
对称的编码器-解码器结构:有效结合低层细节和高层语义信息
-
跳跃连接:缓解深层网络的梯度消失问题,保留多尺度特征
十一、应用场景
这种带有坐标注意力的UNet变体特别适合以下任务:
-
医学图像分割(CT/MRI)
-
遥感图像分割
-
自动驾驶场景理解
- 任何需要精确边界定位的图像分割任务
十二、总结
本文详细解析了一个改进的UNet实现,重点介绍了坐标注意力机制如何增强传统UNet的性能。通过将空间注意力与经典的编码器-解码器结构相结合,这个网络能够更有效地捕捉位置敏感的特征,在各类分割任务中表现出色。代码结构清晰,模块化设计使其易于理解和扩展,为图像分割研究和应用提供了有力的工具。