当前位置: 首页 > news >正文

UNet 改进(2):深入解析带有坐标注意力机制(CA)的UNet网络

UNet是一种经典的编码器-解码器结构神经网络,广泛应用于图像分割任务。本文将详细解析一个改进版的UNet实现,它在传统UNet基础上加入了坐标注意力(Coordinate Attention, CA)机制,能够更好地捕捉空间位置信息,提升分割性能。

一、网络结构概览

这个UNet实现包含以下几个主要组件:

  1. 坐标注意力模块(CA):增强网络对空间位置信息的感知能力

  2. 双卷积模块(DoubleConv):基础卷积单元

  3. 下采样模块(Down):编码器部分,逐步提取高层特征

  4. 上采样模块(Up):解码器部分,逐步恢复空间分辨率

  5. 输出卷积模块(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模块的工作原理:

  1. 分别使用高度和宽度方向的全局平均池化(pool_hpool_w)来捕获空间信息

  2. 将两个方向的池化结果拼接并通过1x1卷积进行特征交互

  3. 使用BN和ReLU进行非线性变换

  4. 分离出高度和宽度注意力图

  5. 将注意力图应用于输入特征,增强重要位置的特征响应

这种设计使网络能够精确地定位感兴趣区域,同时保持较低的计算开销。

三、双卷积模块(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)

上采样模块负责恢复特征图的空间分辨率,支持两种上采样方式:

  1. 双线性插值:计算效率高

  2. 转置卷积:可学习的上采样方式

上采样后会与编码器对应层的特征图进行拼接(跳跃连接),然后通过双卷积模块进行特征融合。

六、输出卷积模块(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结构包含:

  1. 初始双卷积层(inc)

  2. 坐标注意力层(ca)

  3. 4个下采样层(down1-down4)

  4. 4个上采样层(up1-up4)

  5. 输出层(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的对称结构:

  1. 输入图像通过初始卷积和CA模块

  2. 编码器部分逐步下采样提取特征

  3. 解码器部分逐步上采样恢复分辨率

  4. 跳跃连接融合高低层特征

  5. 输出最终分割结果

九、完整代码

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

十、创新点与优势

  1. 坐标注意力机制:增强网络对空间位置信息的感知能力,有助于精确定位目标边界

  2. 双线性上采样选项:可以根据需求选择计算效率高的插值方式或可学习的转置卷积

  3. 对称的编码器-解码器结构:有效结合低层细节和高层语义信息

  4. 跳跃连接:缓解深层网络的梯度消失问题,保留多尺度特征

十一、应用场景

这种带有坐标注意力的UNet变体特别适合以下任务:

  • 医学图像分割(CT/MRI)

  • 遥感图像分割

  • 自动驾驶场景理解

  • 任何需要精确边界定位的图像分割任务

十二、总结

本文详细解析了一个改进的UNet实现,重点介绍了坐标注意力机制如何增强传统UNet的性能。通过将空间注意力与经典的编码器-解码器结构相结合,这个网络能够更有效地捕捉位置敏感的特征,在各类分割任务中表现出色。代码结构清晰,模块化设计使其易于理解和扩展,为图像分割研究和应用提供了有力的工具。

相关文章:

  • go垃圾回收机制
  • Java全栈面试宝典:锁机制与Spring生命周期深度解析
  • edge webview2 runtime跟Edge浏览器软件安装包双击无反应解决方法
  • 探秘JVM内部
  • 流浪动物救助|基于Springboot+vue的流浪动物救助平台设计与实现(源码+数据库+文档)
  • 如何单独指定 Android SDK tools 的 monitor.bat 使用特定 JDK 版本
  • 论伺服电机在轨道式巡检机器人中的优势及应用实践​
  • 《QT从基础到进阶·七十四》Qt+C++开发一个python编译器,能够编写,运行python程序改进版
  • AIDD-深度学习 MetDeeCINE 破译代谢调控机制
  • 达芬奇预设:复古16mm胶片质感老式电影放映机转场过渡+音效
  • 《C++后端开发最全面试题-从入门到Offer》目录
  • WEB安全--XSS--XSS基础
  • ②(PROFINET 转 Modbus TCP)EtherCAT/Ethernet/IP/Profinet/ModbusTCP协议互转工业串口网关
  • 【Linux系统篇】:探索文件系统原理--硬件磁盘、文件系统与链接的“三体宇宙”
  • Deepresearch的MCP实践
  • 接上文,SpringBoot的线程池配置以及JVM监控
  • 初探:简道云平台架构及原理
  • 创建HAL版本MDK工程模板
  • 游戏引擎学习第199天
  • 如何访问和使用Sora:OpenAI视频生成模型的完整指南
  • 如何查询网站服务商/代运营一家店铺多少钱
  • 阆中做网站/一个万能的营销方案
  • 重庆做商城网站建设/网络营销推广的概念
  • 全平台响应式网站建设/推广一般收多少钱
  • 哪网站建设好/抖音搜索引擎推广
  • 上海松江做网站/长沙网络公司排名