跳到主要内容

U-Net — 逐像素的图像变换

这一章讲三件事: 为什么分割任务受不了瓶颈(上一章的优点的反面); 跳连怎么把细节原样送到解码端、顺带救了梯度; 分割怎么被改写成「逐像素的分类」以及怎么量化分割好坏。 主走查是一张 4×4 小图上的 IoU 计算——分割任务的成绩单怎么算出来的。

1. 顶层全景

输入图 1024×1024

┌────▼─────┐ ┌────────┐
│ 编码路径 │ ──┐ ┌───▶ │ 解码路径 │──▶ 逐像素概率图
│ 卷积+池化 │ │ 跳连(抄送) │ │ 卷积+上采样│ (每像素属于每类的概率)
│ 分辨率↓ │ ├─ ─ ─ ─ ─ ─ ─┤ │ 分辨率↑ │
│ 特征数↑ │ ▼ │ └────────┘
└──────────┘ 瓶颈(最细处) ──┘
图说:整体轮廓是 U 形,名字由此而来。
实线是主干(信息被压缩再放大),虚线是跳连——
编码路径每层的特征图被原样抄送到解码路径同尺寸的那层,拼接在一起[^1]。

U-Net 由 Ronneberger 等人在 2015 年为生物医学图像分割提出1

2. 核心原理

2.1 为什么分割受不了瓶颈

先看它修的毛病。编码器-解码器的瓶颈是故意收窄的:压掉噪声的同时难免压掉细节, 这对去噪、降维(把数据压缩得更少、更概括)是合理交换,对分割却是致命的——分割的产出就是每个像素的归属, 丢一个像素级的细节,输出就错一个像素2

**跳连(skip connection)**是对症的结构:把编码路径上每一层的特征图, 抄送到解码路径上尺寸相同的那一层,拼在一起再继续处理3。 效果是双轨制:

  • 走瓶颈的那一路继续回答「这块图像是什么」(经过压缩、抽象后的高层语义——「语义」就是「这块东西属于哪一类」这种信息);
  • 走跳连的那一路原样保留「它在哪里」(未压缩的空间细节);
  • 解码路径把两路拼起来,才能输出「什么在哪个像素」的精确边界。

跳连还有一笔附带收益:它给梯度提供了一条绕过瓶颈的直通路, 训练时底层权重能直接收到输出端的错误信号,缓解深层网络的梯度消失4。 (第 03 章埋的这个病,在架构层的第一味药就是它;2016 年的 ResNet 把这个思想推到极致, 原书在 seminal 里点名5。)

2.2 分割被改写成逐像素分类

语义分割的定义:把图中每个像素的值,换成一个对下游分析更有用的属性—— 最典型的是把「这个像素的亮度」换成「这个像素属于某类结构的概率」6。 术语分层要分清7:

术语回答的问题
语义分割这个像素属于哪?(细胞/背景)
实例分割这个像素属于哪个个体?(第 3 个细胞/第 7 个细胞)
全景分割两者都要

改写的关键一步:U-Net 的最后一层对每个像素做一次 softmax—— 和第 03 章 MNIST 出口的 softmax 完全同款,只不过那时是「一张图 10 个类」, 现在是「每个像素 K 个类」8。损失也照搬交叉熵; 原书还给了省算力的变体(sparse 版,只对真类那一项算对数) 和两个常用的加权技巧(边界像素加权、类不平衡加权)9

2.3 主走查:IoU,分割的成绩单怎么算

分类的成绩单是准确率,分割的成绩单是 IoU(交并比,又叫 Jaccard 指数): 预测区域与真值区域的交集面积除以并集面积,完美分割等于 1.010。 拿一个 4×4 的小图算一遍(下面的格子内容是为演示编的,机制与原书一致):

真值(要分的对象) 预测(网络输出)
· · █ █ · █ █ █
· · █ █ · · █ █
· · · █ · · · █
· · · · · · · ·

交集(两边都算对象)的格子 = 5 个 (第1行的(3,4), 第2行的(3,4), 第3行的(4))
并集(至少一边算对象)的格子 = 7 个 (真值 6 格 ∪ 预测 6 格)
IoU = 5 / 7 ≈ 0.71

IoU 好用的地方在分母:它同时惩罚漏报(该分的没分,交集变小) 和误报(不该分的分了,并集变大),任何一边摆烂都会掉分10

2.4 全流程走查:果蝇神经组织分割

原书的主项目把上面全部零件串起来。数据:果蝇幼虫腹神经索的序列切片透射电镜图 (ssTEM,一种逐层切片、逐层拍照的高分辨显微术),20 张 1024×1024, 每张都由人类专家费力标注了两个类别:细胞内区域、线粒体11

① 划分:80% 训练 / 10% 验证 / 10% 测试(第 04 章的三分制度照用)[^13]
② 训练:U-Net,监督信号=专家标注的分割图;100 个 epoch[^14]
③ 监控:每个 epoch 算训练集和验证集的 IoU 曲线
④ 早停:盯着验证 IoU,取最大方向;连续 5 个 epoch 无改进就停[^15]

损失函数的选择在这个项目里是一等公民:原书让读者对比不同损失下的边界质量, 并用数据增广(翻转、旋转、加噪声)扩充这 20 张图——小数据集上增广不是可选项, 是必需品(这是第 06 章仿真造数据的低配替代:没有仿真器时,就手工变换出「新」样本)。

2.5 两个延伸项目

量子点检测(5A):量子点是纳米级半导体颗粒,荧光图里是一个个亮点。 U-Net 输出的逐像素概率图里,亮点处概率高——找概率峰就找到了量子点位置。 「分割网络当检测器用」是它最常见的第二职业。

细胞计数(5B):数据集 BBBC039v1,200 张荧光视野、约 23,000 个细胞核, 全部有人工标注12。原书没有直接用这 200 张训练,而是仿真生成带真值的训练图 (第 05、06 章的套路),训好 U-Net 分割出每个核,再用连通域计数 (把相邻的属于同一体的像素归为一团,数团数)得出细胞数13。 「分割→连通域→计数」是把像素级输出变成「几个」这种离散答案的标准管道。

3. 作者的判断与证据

  • 有证据的:组织分割项目的 IoU 曲线与早停生效、细胞计数从头到尾跑通、量子点定位可视化。
  • 作者的归因:U-Net 成功的两要素——多尺度特征同时参与、原论文的损失加权(给边界和小结构更高权重)14
  • 结构性伏笔:原书在小结里明说,U-Net 会在第 9、10 章的 GAN 和扩散模型里再登场当骨干网络15—— 「判别结构」和「生成结构的骨架」共用同一套 U 形积木,这是全书架构选型的暗线。

4. 边界与局限

边界说明
跳连救不了「训练分布外」组织种类、染色方式换了照样要重训/微调
语义分割不数个体要「第几个细胞」得上实例分割(方法在下一章的自监督里再碰)
小数据集依赖增广/仿真20 张电镜图靠增广撑;没有增广,过拟合立现
IoU 对小物体敏感一个小结构的漏报就能把 IoU 拉低一大截;类别极不平衡时要配加权损失
边界像素天然难分两个结构相接处,「属于谁」本就含糊;加权损失是缓解不是消除

5. 可带走的

  1. 瓶颈省细节 vs 分割要细节——U-Net 的跳连是为这对矛盾发明的;
  2. 双轨制:瓶颈管「是什么」,跳连管「在哪里」,解码端拼接两路;
  3. 跳连附带给梯度开直通车(ResNet 的思想源头之一);
  4. 分割=逐像素分类:出口 softmax、损失交叉熵,和图像分类共用全套零件,只是作用在像素上;
  5. IoU=交/并:同时罚漏报和误报;手算一遍就再也不会忘;
  6. 早停盯验证 IoU:连续数轮无改进即停——把「什么时候停」从经验变成监控项;
  7. 分割→连通域→计数:像素输出变离散答案的标准管道;
  8. U 形骨架是可复用的:分割、检测、去噪、生成的骨干都是它(第 09–12 章会回来)。

6. 原文地图

主题原书章原文位置
瓶颈的取舍(动机)Introducing U-Netstext/43-fm-introducing-u-nets.txt:5(搜「intentionally restrict the complexity」)
Ronneberger 2015Introducing U-Netstext/43-fm-introducing-u-nets.txt:7(搜「Olaf Ronneberger」)
跳连定义与 U 形Introducing U-Netstext/43-fm-introducing-u-nets.txt:11(搜「skip connections」)
拼接同尺寸特征图Introducing U-Netstext/43-fm-introducing-u-nets.txt:13(搜「concatenating feature maps」)
跳连缓解梯度消失Introducing U-Netstext/43-fm-introducing-u-nets.txt:15(搜「vanishing gradient problem」)
语义分割定义Understanding Semantic Segmentation with U-Netstext/44-fm-understanding-semantic-segmentation-with-u-nets.txt:3(搜「converting the values of each pixel」)
实例/全景分割Understanding Semantic Segmentation with U-Netstext/44-fm-understanding-semantic-segmentation-with-u-nets.txt:7(搜「instance segmentation」)
逐像素分类+softmaxUnderstanding Semantic Segmentation with U-Netstext/44-fm-understanding-semantic-segmentation-with-u-nets.txt:13(搜「pixel-wise multi-class classification」)
sparse 交叉熵与加权Understanding Semantic Segmentation with U-Netstext/44-fm-understanding-semantic-segmentation-with-u-nets.txt:27(搜「sparse categorical cross-entropy」) · text/44-fm-understanding-semantic-segmentation-with-u-nets.txt:33(搜「weight the pixels at the borders」)
ssTEM 数据集Segmenting Images of Biological Tissuestext/45-fm-segmenting-images-of-biological-tissues.txt:3(搜「serial-section transmission electron microscopy」) · text/45-fm-segmenting-images-of-biological-tissues.txt:5(搜「20 high-quality ssTEM images」)
80/10/10 划分Segmenting Images of Biological Tissuestext/45-fm-segmenting-images-of-biological-tissues.txt:30(搜「0.8, 0.1, 0.1」)
IoU/Jaccard 定义Segmenting Images of Biological Tissuestext/45-fm-segmenting-images-of-biological-tissues.txt:125(搜「intersection over union」)
100 epochsSegmenting Images of Biological Tissuestext/45-fm-segmenting-images-of-biological-tissues.txt:182(搜「100 epochs」)
早停配置Segmenting Images of Biological Tissuestext/45-fm-segmenting-images-of-biological-tissues.txt:250(搜「EarlyStopping」)
量子点Project 5A: Detecting Quantum Dots in Fluorescence Imagestext/46-fm-project-5a-detecting-quantum-dots-in-fluorescenc.txt:3(搜「quantum dots」)
BBBC039v1 数据Project 5B: Counting Cellstext/47-fm-project-5b-counting-cells.txt:9(搜「23,000」)
连通域计数Project 5B: Counting Cellstext/47-fm-project-5b-counting-cells.txt:104(搜「connected components method」)
仿真造带真值数据Summarytext/48-fm-summary.txt:9(搜「simulations to generate datasets」)
U-Net 伏笔(ch9/10)Summarytext/48-fm-summary.txt:11(搜「Chapters 9 and 10」)
多尺度与损失加权归因Understanding Semantic Segmentation with U-Netstext/44-fm-understanding-semantic-segmentation-with-u-nets.txt:11(搜「loss weighting」)
U-Net 谱系(nnU-Net)Seminal Works and Further Readingtext/49-fm-seminal-works-and-further-reading.txt:3(搜「Ronneberger」) · text/49-fm-seminal-works-and-further-reading.txt:7(搜「nnU-Net」)

Footnotes

  1. 出处:「Introducing U-Nets」第 7 段(text/43-fm-introducing-u-nets.txt:7,搜「Olaf Ronneberger」)。

  2. 出处:「Introducing U-Nets」第 5 段(text/43-fm-introducing-u-nets.txt:5,搜「intentionally restrict the complexity」)。

  3. 出处:「Introducing U-Nets」第 11-13 段(text/43-fm-introducing-u-nets.txt:11,搜「skip connections」)。

  4. 出处:「Introducing U-Nets」第 15 段(text/43-fm-introducing-u-nets.txt:15,搜「vanishing gradient problem」)。

  5. 出处:「Seminal Works and Further Reading」第 11 段(text/33-fm-seminal-works-and-further-reading.txt:11,搜「ResNet」)。残差连接在第 3 章 seminal 一节,本章跳连是其思想源头这一判断是我们的,原书只并列呈现。

  6. 出处:「Understanding Semantic Segmentation with U-Nets」第 3 段(text/44-fm-understanding-semantic-segmentation-with-u-nets.txt:3,搜「converting the values of each pixel」)、第 9 段(text/44-fm-understanding-semantic-segmentation-with-u-nets.txt:9,搜「probability of each pixel」)。

  7. 出处:「Understanding Semantic Segmentation with U-Nets」第 7 段(text/44-fm-understanding-semantic-segmentation-with-u-nets.txt:7,搜「instance segmentation」)。

  8. 出处:「Understanding Semantic Segmentation with U-Nets」第 13 段(text/44-fm-understanding-semantic-segmentation-with-u-nets.txt:13,搜「pixel-wise multi-class classification」)、第 15-19 段(text/44-fm-understanding-semantic-segmentation-with-u-nets.txt:15,搜「probability distribution」)。

  9. 出处:「Understanding Semantic Segmentation with U-Nets」第 27 段(text/44-fm-understanding-semantic-segmentation-with-u-nets.txt:27,搜「sparse categorical cross-entropy」)、第 33 段(text/44-fm-understanding-semantic-segmentation-with-u-nets.txt:33,搜「weight the pixels at the borders」)。

  10. 出处:「Segmenting Images of Biological Tissues」第 125 段(text/45-fm-segmenting-images-of-biological-tissues.txt:125,搜「intersection over union」)。 2

  11. 出处:「Segmenting Images of Biological Tissues」第 3 段(text/45-fm-segmenting-images-of-biological-tissues.txt:3,搜「serial-section transmission electron microscopy」)、第 5 段(text/45-fm-segmenting-images-of-biological-tissues.txt:5,搜「20 high-quality ssTEM images」)。

  12. 出处:「Project 5B: Counting Cells」第 9 段(text/47-fm-project-5b-counting-cells.txt:9,搜「23,000」)。

  13. 出处:「Project 5B: Counting Cells」第 104 段(text/47-fm-project-5b-counting-cells.txt:104,搜「connected components method」)。

  14. 出处:「Understanding Semantic Segmentation with U-Nets」第 11 段(text/44-fm-understanding-semantic-segmentation-with-u-nets.txt:11,搜「loss weighting」)。

  15. 出处:「Summary」第 11 段(text/48-fm-summary.txt:11,搜「Chapters 9 and 10」)。