主要内容

本页采用了机器翻译。点击此处可查看英文原文。

unet

创建用于语义分割的 U-Net 卷积神经网络

自 R2024a 起

说明

unetNetwork = unet(imageSize,numClasses) 返回一个 U-Net 网络。

使用 unet 创建 U-Net 架构。您必须使用 Deep Learning Toolbox™ 函数 trainnet (Deep Learning Toolbox) 来训练该网络。

示例

[unetNetwork,outputSize] = unet(imageSize,numClasses) 还会返回 U-Net 网络的输出尺寸。

___ = unet(imageSize,numClasses,Name=Value) 使用一个或多个名称-值参量指定选项。例如,unet(imageSize,numClasses,NumFirstEncoderFilters=64) 指定了第一级编码器的输出通道数为 64

示例

全部折叠

创建一个编码器-解码器深度为 3 的 U-Net 网络。

imageSize = [480 640 3];
numClasses = 5;
encoderDepth = 3;
unetNetwork = unet(imageSize,numClasses,EncoderDepth=encoderDepth)
unetNetwork = 
  dlnetwork with properties:

         Layers: [48×1 nnet.cnn.layer.Layer]
    Connections: [53×2 table]
     Learnables: [36×3 table]
          State: [0×3 table]
     InputNames: {'encoderImageInputLayer'}
    OutputNames: {'FinalNetworkSoftmax-Layer'}
    Initialized: 1

  View summary with summary.

显示网络。

plot(unetNetwork)

Figure contains an axes object. The axes object contains an object of type graphplot.

将训练图像和像素标签加载到工作区中。

dataSetDir = fullfile(toolboxdir("vision"),"visiondata","triangleImages");
imageDir = fullfile(dataSetDir,"trainingImages");
labelDir = fullfile(dataSetDir,"trainingLabels");

创建一个 imageDatastore 对象来存储训练图像。

imds = imageDatastore(imageDir);

定义类名及其对应的标签 ID。

classNames = ["triangle","background"];
labelIDs   = [255 0];

创建一个 pixelLabelDatastore 对象,用于存储训练图像的真实值像素标签。

pxds = pixelLabelDatastore(labelDir,classNames,labelIDs);

创建 U-Net 神经网络。

imageSize = [32 32];
numClasses = 2;
unetNetwork = unet(imageSize, numClasses)
unetNetwork = 
  dlnetwork with properties:

         Layers: [61×1 nnet.cnn.layer.Layer]
    Connections: [68×2 table]
     Learnables: [46×3 table]
          State: [0×3 table]
     InputNames: {'encoderImageInputLayer'}
    OutputNames: {'FinalNetworkSoftmax-Layer'}
    Initialized: 1

  View summary with summary.

创建一个用于训练神经网络的数据集。

ds = combine(imds,pxds);

设置训练选项。

options = trainingOptions("sgdm", ...
    InitialLearnRate=1e-3, ...
    MaxEpochs=20, ...
    VerboseFrequency=10);

训练网络。

net = trainnet(ds,unetNetwork,"crossentropy",options)
    Iteration    Epoch    TimeElapsed    LearnRate    TrainingLoss
    _________    _____    ___________    _________    ____________
            1        1       00:00:05        0.001          3.2975
           10       10       00:00:48        0.001          0.6778
           20       20       00:01:36        0.001         0.27066
Training stopped: Max epochs completed
net = 
  dlnetwork with properties:

         Layers: [61×1 nnet.cnn.layer.Layer]
    Connections: [68×2 table]
     Learnables: [46×3 table]
          State: [0×3 table]
     InputNames: {'encoderImageInputLayer'}
    OutputNames: {'FinalNetworkSoftmax-Layer'}
    Initialized: 1

  View summary with summary.

输入参数

全部折叠

网络输入图像大小,指定为:

  • 形式为 [height, width] 的 2 元素向量。

  • 形式为 [height, width, depth] 的 3 元素向量。depth 表示图像通道数。对于 RGB 图像,将 depth 设置为 3;对于灰度图像,设置为 1;对于多光谱和高光谱图像,则设置为通道数。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

语义分割中的类数,指定为大于 1 的整数。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

名称-值参数

全部折叠

将可选参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。

示例: EncoderDepth=3 将编码器深度指定为 3。

unet 用作编码器的编码器网络,指定为一个 dlnetwork (Deep Learning Toolbox) 对象。您可以指定一个预训练网络或自定义编码器网络。要使用预训练网络,请使用 pretrainedEncoderNetwork 函数创建该网络。

注意

如果指定了 EncoderNetwork,则 unet 函数不会使用这些名称-值参量来配置网络:

编码器深度,指定为一个正整数。U-Net 由一个编码器子网络和一个相应的解码器子网络组成。这些网络的深度决定了输入图像在处理过程中被下采样或上采样的次数。编码器网络将输入图像以 2D 的倍数进行降采样,其中 DEncoderDepth 的值。解码器网络将编码器网络的输出以 2D 的倍数进行上采样。

注意

如果还指定了 EncoderNetwork,请根据 EncoderNetwork 输入的深度来指定 EncoderDepth 的值。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

第一级编码器的输出通道数,指定为一个正整数或正整数向量。在随后的每个编码器级中,输出通道的数量都会翻倍。unet 函数将每个解码器级别的输出通道数设置为与相应编码器级别的通道数一致。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

卷积层的滤波器大小,指定为一个正奇数,或由正奇数组成的 2 元素向量。典型值在 [3, 7] 范围内。

FilterSize描述
标量滤波器为正方形。
2 个元素的行向量

该滤镜的尺寸为 [height width]。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

填充类型,指定为 "same""valid"。填充类型指定了编码器和解码器子网络中 convolution2dLayer (Deep Learning Toolbox) 的填充样式。输出特征图的空间尺寸取决于填充类型。如果您将填充类型指定为:

  • "same" - 对卷积层的输入进行零填充,以确保输出特征图和输入特征图的大小相同。

  • "valid" - 卷积层的输入不进行零填充。卷积层仅返回未进行零填充计算的卷积值。输出特征图比输入特征图小。

注意

为确保最大池化层输入的 heightwidth 数量为偶数,请将网络输入图像尺寸设置为符合以下任一条件:

  • 如果您将 ConvolutionPadding 指定为 "same",那么输入图像中的 heightwidth 必须是 2D 的倍数。

  • 如果您将 ConvolutionPadding 指定为 "valid",那么输入图像中的 heightwidth 必须选择得使得 12D(heighti=1D2i(fh1))12D(widthi=1D2i(fw1)) 是 2 D 的倍数。

    其中,fhfw 分别表示二维卷积核的高度和宽度。D 表示编码器的深度。

数据类型: char | string

输出参量

全部折叠

表示 U-Net 网络架构的层,以 dlnetwork (Deep Learning Toolbox) 对象的形式返回。

网络输出图像尺寸,以形式为 [height, width, channels] 的三元素向量返回。channels 是输出通道数,其值等于输入中指定的类别数。网络输出图像中的 heightwidth 取决于填充卷积的类型。

  • 如果您将 ConvolutionPadding 指定为 "same",那么网络输出图像中的 heightwidth 将与网络输入图像中的相同。

  • 如果您将 ConvolutionPadding 指定为 "valid",那么网络输出图像中的 heightwidth 就会小于网络输入图像中的相应值。

数据类型: double

详细信息

全部折叠

提示

  • 在卷积层中使用 'same' 填充,以保持从输入到输出的数据大小一致,并支持多种输入图像尺寸。

  • 采用基于补丁的方法,对大尺寸图像进行无缝分割。您可以使用 randomPatchExtractionDatastore 函数提取图像片段。

  • 在使用基于补丁的分割方法时,请使用 'valid' 填充以防止出现边界伪影。

  • 在使用 trainnet (Deep Learning Toolbox) 进行训练后,您可以将使用 unet 函数创建的网络用于 GPU 代码生成。有关详细信息和示例,请参阅生成代码并部署深度神经网络 (Deep Learning Toolbox)

参考

[1] Ronneberger, O., P. Fischer, and T. Brox. "U-Net: Convolutional Networks for Biomedical Image Segmentation." Medical Image Computing and Computer-Assisted Intervention (MICCAI). Vol. 9351, 2015, pp. 234–241.

[2] He, K., X. Zhang, S. Ren, and J. Sun. "Delving Deep Into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification." Proceedings of the IEEE International Conference on Computer Vision. 2015, 1026–1034.

扩展功能

全部展开

版本历史记录

在 R2024a 中推出