主要内容

基于深度学习的多光谱图像语义分割

这个例子展示了如何使用U-Net对具有7个通道的多光谱图像进行语义分割。

语义分割包括用类标记图像中的每个像素。语义分割的一个应用是跟踪森林砍伐,即森林覆盖率随时间的变化。环境机构跟踪森林砍伐,以评估和量化一个地区的环境和生态健康。

基于深度学习的语义分割可以从高分辨率航空照片中精确测量植被覆盖。其中一个挑战是区分具有相似视觉特征的类,例如尝试将绿色像素分类为草、灌木或树。为了提高分类精度,一些数据集包含多光谱图像,这些图像提供了关于每个像素的额外信息。例如,哈姆林海滩州立公园的数据集用三个近红外通道补充了彩色图像,从而提供了更清晰的分类。

这个例子首先向您展示了如何使用预训练的U-Net执行语义分割,然后使用分割结果计算植被覆盖的范围。然后,您可以选择使用基于补丁的训练方法在Hamlin Beach State park数据集上训练U-Net网络。

下载数据集

本例使用高分辨率多光谱数据集来训练网络[1]。这组图像是由一架无人机在纽约州哈姆林海滩州立公园上空拍摄的。数据包含带标签的训练集、验证集和测试集,有18个对象类标签。数据文件大小为3.0 GB。

方法下载数据集的mat文件版本downloadHamlinBeachMSIDatahelper函数。该函数作为支持文件附加到示例中。金宝app指定dataDir作为所需的数据位置。

dataDir = fullfile(tempdir,“rit18_data”);downloadHamlinBeachMSIData (dataDir);

加载数据集。

负载(fullfile (dataDir“rit18_data.mat”));谁train_dataval_datatest_data
名称大小字节类属性test_data 7x12446x7654 1333663576 uint16 train_data 7x9393x5642 741934284 uint16 val_data 7x8833x6918 855493716 uint16

多光谱图像数据排列为numChannels——- - - - - -宽度——- - - - - -高度数组。然而,在MATLAB®中,多通道图像被安排为宽度——- - - - - -高度——- - - - - -numChannels数组。若要重塑数据以使通道处于第三维,请使用switchChannelsToThirdPlanehelper函数。该函数作为支持文件附加到示例中。金宝app

train_data = switchChannelsToThirdPlane(train_data);val_data = switchChannelsToThirdPlane(val_data);test_data = switchChannelsToThirdPlane(test_data);

请确认数据结构正确。

train_dataval_datatest_data
名称大小字节类属性test_data 12446x7654x7 1333663576 uint16 train_data 9393x5642x7 741934284 uint16 val_data 8833x6918x7 855493716

将训练数据保存为MAT文件,将训练标签保存为PNG文件。这便于使用类加载训练数据imageDatastore和一个pixelLabelDatastore在培训。

保存(“train_data.mat”“train_data”);imwrite (train_labels“train_labels.png”);

可视化多光谱数据

在这个数据集中,RGB颜色通道是第三、第二和第一图像通道。将训练、验证和测试图像的颜色组件显示为蒙太奇。要使图像在屏幕上显示得更亮,可以使用histeq函数。

图蒙太奇({histeq(train_data(:,:,[3 2 1])),Histeq (val_data(:,:,[31 21 1])),Histeq (test_data(:,:,[3 2 1]))},BorderSize = 10,写成BackgroundColor =“白色”)标题(训练、验证和测试图像的RGB分量(从左到右)

图包含一个轴对象。标题为“训练、验证和测试图像的RGB组件(从左到右)”的轴对象包含一个图像类型的对象。

显示训练数据的最后三个直方图均衡通道作为蒙太奇。这些通道对应于近红外波段,并根据其热特征突出图像的不同组成部分。例如,第二通道图像中心附近的树比其他两个通道中的树显示出更多的细节。

图蒙太奇({histeq (train_data (:: 4)), histeq (train_data (:,:, 5)), histeq (train_data (:,: 6)},BorderSize = 10,写成BackgroundColor =“白色”)标题(训练图像红外通道1、2和3(从左到右)

图包含一个轴对象。标题为Training Image IR Channels 1,2,3(从左到右)的坐标轴对象包含一个Image类型的对象。

通道7是一个掩码,表示有效的分割区域。显示训练、验证和测试图像的掩码。

图蒙太奇({train_data (:: 7), val_data (:,: 7), test_data (:,: 7)},BorderSize = 10,写成BackgroundColor =“白色”)标题(“训练、验证和测试图像遮罩(从左到右)”

图包含一个轴对象。标题为“训练、验证和测试图像(从左到右)的掩码”的轴对象包含一个图像类型的对象。

可视化地面真相标签

标记的图像包含用于分割的地面真实数据,每个像素被分配到18个类中的一个。获取具有相应id的类的列表。

disp(类)
0.其他类/图像边框道路标志树3。建设4。交通工具(汽车、卡车或公共汽车)6人。救生员椅野炊桌黑木板 White Wood Panel 10. Orange Landing Pad 11. Water Buoy 12. Rocks 13. Other Vegetation 14. Grass 15. Sand 16. Water (Lake) 17. Water (Pond) 18. Asphalt (Parking Lot/Walkway)

创建一个类名向量。

classNames = [“路标”“树”“建筑”“汽车”“人”“LifeguardChair”“PicnicTable”“BlackWoodPanel”“WhiteWoodPanel”“OrangeLandingPad”“浮”“石头”“LowLevelVegetation”“Grass_Lawn”“Sand_Beach”“Water_Lake”“Water_Pond”“沥青”];

将标签叠加在直方图均衡的RGB训练图像上。为图像添加一个颜色条。

cmap = jet(numel(classNames));B = labeloverlay(histeq(train_data(:,:,4:6)),train_labels,Transparency=0.8,Colormap=cmap);(B)标题(“培训”标签) N = numel(classNames);ticks = 1/(N*2):1/N:1;colorbar (TickLabels = cellstr(类名),蜱虫=蜱虫,TickLength = 0, TickLabelInterpreter =“没有”);colormap城市规划机构(cmap)

图包含一个轴对象。标题为Training Labels的坐标轴对象包含一个图像类型的对象。

执行语义分割

下载预训练的U-Net网络。

trainedUnet_url =“//www.tatmou.com/金宝appsupportfiles/vision/data/multispectralUnet.mat”;downloadTrainedNetwork (trainedUnet_url dataDir);负载(fullfile (dataDir“multispectralUnet.mat”));

要在训练好的网络上执行语义分割,请使用segmentMultispectralImage带有验证数据的辅助函数。该函数作为支持文件附加到示例中。金宝app的segmentMultispectralImage函数对图像补丁进行分割semanticseg(计算机视觉工具箱)函数。处理补丁是必需的,因为图像的大小阻止一次处理整个图像。

predictPatchSize = [10241024];segmentMultispectralImage(val_data,net,predictPatchSize);

为了只提取分割的有效部分,将分割后的图像乘以验证数据的掩码通道。

segmentedImage = uint8(val_data(:,:,7)~=0) .* segmentedImage;figure imshow(segmenteimage,[]) title()“分割图像”

图包含一个轴对象。标题为Segmented Image的坐标轴对象包含一个Image类型的对象。

语义分割的输出是有噪声的。进行后期图像处理,去除噪声和杂散像素。使用medfilt2函数从分割中去除椒盐噪声。将去噪后的分割图像可视化。

segmentedImage = medfilt2(segmentedImage,[7,7]);imshow (segmentedImage []);标题(“去噪分割图像”

图包含一个轴对象。标题为“去噪分割图像”的坐标轴对象包含一个图像类型的对象。

将分割后的图像叠加在直方图均衡的RGB验证图像上。

B = labeloverlay(histeq(val_data(:,:,[3 2 1])), segmenteimage,透明度=0.8,Colormap=cmap);(B)标题(“标记分割图像”) colorbar (TickLabels = cellstr(类名),蜱虫=蜱虫,TickLength = 0, TickLabelInterpreter =“没有”);colormap城市规划机构(cmap)

图包含一个轴对象。标题为Labeled Segmented Image的坐标轴对象包含一个Image类型的对象。

计算植被覆盖范围

语义分割的结果可以用来回答相关的生态问题。例如,植被覆盖的土地面积的百分比是多少?要回答这个问题,找到标记为植被的像素数。标签id 2(“Trees”)、13(“LowLevelVegetation”)和14(“Grass_Lawn”)是植被类。还可以通过将掩模图像的ROI中的像素相加来找到有效像素的总数。

vegetationClassIds = uint8([2,13,14]);vegetationPixels = ismember(segmenteimage (:),vegetationClassIds);validPixels = (segmenteimage ~=0);numVegetationPixels = sum(vegetationPixels(:));numValidPixels = sum(validPixels(:));

通过将植被像素数除以有效像素数来计算植被覆盖的百分比。

percentVegetationCover = (numVegetationPixels/numValidPixels)*100;流(“植被覆盖的百分比是%3.2 %% %。”, percentVegetationCover);
植被覆盖率为51.72%。

示例的其余部分向您展示了如何在Hamlin Beach数据集上训练U-Net。

为训练创建随机补丁提取数据存储

使用随机补丁提取数据存储将训练数据提供给网络。该数据存储从包含地面真实图像和像素标签数据的图像数据存储和像素标签数据存储中提取多个相应的随机补丁。打补丁是一种常用的技术,可以防止大型图像内存耗尽,并有效地增加可用训练数据的数量。

首先从“train_data.mat”在一个imageDatastore。由于MAT文件格式是非标准的图像格式,因此必须使用MAT文件读取器才能读取图像数据。您可以使用辅助MAT文件阅读器,matRead6Channels,从训练数据中提取前6个通道,并省略包含掩码的最后一个通道。该函数作为支持文件附加到示例中。金宝app

imds = imageDatastore(“train_data.mat”FileExtensions =“.mat”ReadFcn = @matRead6Channels);

创建一个pixelLabelDatastore(计算机视觉工具箱)存储包含18个标记区域的标签补丁。

pixelLabelIds = 1:18;pxds = pixelLabelDatastore(“train_labels.png”一会,pixelLabelIds);

创建一个randomPatchExtractionDatastore从图像数据存储和像素标签数据存储。每个小批量包含16块大小为256 × 256像素的补丁。在历元的每次迭代中提取1000个小批。

dsTrain = randompatchextracactiondatastore (imds,pxds,[256,256],PatchesPerImage=16000);

随机补丁提取数据存储dsTrain在epoch的每次迭代时向网络提供小批量数据。预览数据存储以探索数据。

inputBatch = preview(dsTrain);disp (inputBatch)
InputImage ResponsePixelLabelImage  __________________ _______________________ { 256×256×6 uint16}{256×256分类}{256×256×6 uint16}{256×256分类}{256×256×6 uint16}{256×256分类}{256×256×6 uint16}{256×256分类}{256×256×6 uint16}{256×256分类}{256×256×6 uint16}{256×256分类}{256×256×6 uint16}{256×256分类}{256×256×6 uint16}{256×256分类}

创建U-Net网络层

本例使用U-Net网络的一种变体。在U-Net中,初始的卷积层序列中穿插着最大池化层,依次降低了输入图像的分辨率。这些层之后是一系列的卷积层,其间穿插上采样算子,依次增加输入图像的分辨率[2]。U- net这个名字来源于这样一个事实,即该网络可以用像字母U一样的对称形状来绘制。

这个例子修改了U-Net,在卷积中使用零填充,以便卷积的输入和输出具有相同的大小。使用辅助函数,createUnet,用几个预先选定的超参数创建一个U-Net。该函数作为支持文件附加到示例中。金宝app

inputTileSize = [256,256,6];lgraph = createUnet(inputTileSize);disp (lgraph.Layers)
58×1层数组与层:1“ImageInputLayer”图像输入256×256×6图片2的zerocenter正常化Encoder-Section-1-Conv-1二维卷积64 3×3×6旋转步[1]和填充[1 1 1 1]3‘Encoder-Section-1-ReLU-1 ReLU ReLU 4 Encoder-Section-1-Conv-2二维卷积64 3×3×64旋转步[1]和填充[1 1 1 1]5‘Encoder-Section-1-ReLU-2 ReLU ReLU 6“Encoder-Section-1-MaxPool”二维最大池2×2马克斯池步[2 2]和填充[0 0 0 0]7Encoder-Section-2-Conv-1二维卷积128 3×3×64旋转步[1]和填充(1 1 1)8“Encoder-Section-2-ReLU-1”ReLU ReLU 9 Encoder-Section-2-Conv-2二维卷积128 3×3×128旋转步[1]和填充[1 1 1 1]10 ' Encoder-Section-2-ReLU-2 ReLU ReLU 11“Encoder-Section-2-MaxPool”二维最大池2×2马克斯池步(2 - 2)和填充[0 0 0 0]12“Encoder-Section-3-Conv-1”二维卷积256 3×3×128旋转步[1]和填充[1 1 1 1] 13的Encoder-Section-3-ReLU-1 ReLU ReLU 14 Encoder-Section-3-Conv-2二维卷积256 3×3×256旋转步[1]和填充[1 1 1 1]15 ' Encoder-Section-3-ReLU-2 ReLU ReLU 16“Encoder-Section-3-MaxPool”二维最大池2×2马克斯池步(2 - 2)和填充[0 0 0 0]17 Encoder-Section-4-Conv-1二维卷积512 3×3×256旋转步[1]和填充[1 1 1 1]18 ' Encoder-Section-4-ReLU-1 ReLU ReLU 19 Encoder-Section-4-Conv-2二维卷积512 3×3×512 convolutions with stride [1 1] and padding [1 1 1 1] 20 'Encoder-Section-4-ReLU-2' ReLU ReLU 21 'Encoder-Section-4-DropOut' Dropout 50% dropout 22 'Encoder-Section-4-MaxPool' 2-D Max Pooling 2×2 max pooling with stride [2 2] and padding [0 0 0 0] 23 'Mid-Conv-1' 2-D Convolution 1024 3×3×512 convolutions with stride [1 1] and padding [1 1 1 1] 24 'Mid-ReLU-1' ReLU ReLU 25 'Mid-Conv-2' 2-D Convolution 1024 3×3×1024 convolutions with stride [1 1] and padding [1 1 1 1] 26 'Mid-ReLU-2' ReLU ReLU 27 'Mid-DropOut' Dropout 50% dropout 28 'Decoder-Section-1-UpConv' 2-D Transposed Convolution 512 2×2×1024 transposed convolutions with stride [2 2] and cropping [0 0 0 0] 29 'Decoder-Section-1-UpReLU' ReLU ReLU 30 'Decoder-Section-1-DepthConcatenation' Depth concatenation Depth concatenation of 2 inputs 31 'Decoder-Section-1-Conv-1' 2-D Convolution 512 3×3×1024 convolutions with stride [1 1] and padding [1 1 1 1] 32 'Decoder-Section-1-ReLU-1' ReLU ReLU 33 'Decoder-Section-1-Conv-2' 2-D Convolution 512 3×3×512 convolutions with stride [1 1] and padding [1 1 1 1] 34 'Decoder-Section-1-ReLU-2' ReLU ReLU 35 'Decoder-Section-2-UpConv' 2-D Transposed Convolution 256 2×2×512 transposed convolutions with stride [2 2] and cropping [0 0 0 0] 36 'Decoder-Section-2-UpReLU' ReLU ReLU 37 'Decoder-Section-2-DepthConcatenation' Depth concatenation Depth concatenation of 2 inputs 38 'Decoder-Section-2-Conv-1' 2-D Convolution 256 3×3×512 convolutions with stride [1 1] and padding [1 1 1 1] 39 'Decoder-Section-2-ReLU-1' ReLU ReLU 40 'Decoder-Section-2-Conv-2' 2-D Convolution 256 3×3×256 convolutions with stride [1 1] and padding [1 1 1 1] 41 'Decoder-Section-2-ReLU-2' ReLU ReLU 42 'Decoder-Section-3-UpConv' 2-D Transposed Convolution 128 2×2×256 transposed convolutions with stride [2 2] and cropping [0 0 0 0] 43 'Decoder-Section-3-UpReLU' ReLU ReLU 44 'Decoder-Section-3-DepthConcatenation' Depth concatenation Depth concatenation of 2 inputs 45 'Decoder-Section-3-Conv-1' 2-D Convolution 128 3×3×256 convolutions with stride [1 1] and padding [1 1 1 1] 46 'Decoder-Section-3-ReLU-1' ReLU ReLU 47 'Decoder-Section-3-Conv-2' 2-D Convolution 128 3×3×128 convolutions with stride [1 1] and padding [1 1 1 1] 48 'Decoder-Section-3-ReLU-2' ReLU ReLU 49 'Decoder-Section-4-UpConv' 2-D Transposed Convolution 64 2×2×128 transposed convolutions with stride [2 2] and cropping [0 0 0 0] 50 'Decoder-Section-4-UpReLU' ReLU ReLU 51 'Decoder-Section-4-DepthConcatenation' Depth concatenation Depth concatenation of 2 inputs 52 'Decoder-Section-4-Conv-1' 2-D Convolution 64 3×3×128 convolutions with stride [1 1] and padding [1 1 1 1] 53 'Decoder-Section-4-ReLU-1' ReLU ReLU 54 'Decoder-Section-4-Conv-2' 2-D Convolution 64 3×3×64 convolutions with stride [1 1] and padding [1 1 1 1] 55 'Decoder-Section-4-ReLU-2' ReLU ReLU 56 'Final-ConvolutionLayer' 2-D Convolution 18 1×1×64 convolutions with stride [1 1] and padding [0 0 0 0] 57 'Softmax-Layer' Softmax softmax 58 'Segmentation-Layer' Pixel Classification Layer Cross-entropy loss

选择培训选项

使用随机动量梯度下降(SGDM)优化训练网络。属性指定SGDM的超参数设置trainingOptions(深度学习工具箱)函数。

训练一个深度网络是非常耗时的。通过指定高学习率来加速训练。然而,这可能会导致网络的梯度爆炸或不受控制地增长,从而阻止网络的成功训练。要使渐变保持在一个有意义的范围内,可以通过指定“GradientThreshold”作为0.05,并注明“GradientThresholdMethod”使用梯度的l2范数。

initialLearningRate = 0.05;maxEpochs = 150;minibatchSize = 16;L2reg = 0.0001;options = trainingOptions(“个”InitialLearnRate = initialLearningRate,动量= 0.9,L2Regularization = l2reg,MaxEpochs = MaxEpochs,MiniBatchSize = MiniBatchSize,LearnRateSchedule =“分段”洗牌=“every-epoch”GradientThresholdMethod =“l2norm”GradientThreshold = 0.05,情节=“训练进步”VerboseFrequency = 20);

训练网络或下载预训练网络

要训练网络,请设置doTraining变量的值为真正的。的方法来训练模型trainNetwork(深度学习工具箱)函数。

如果GPU可用,在GPU上进行训练。使用GPU需要Parallel Computing Toolbox™和支持CUDA®的NVIDIA®GPU。有关更多信息,请参见GPU计算要求(并行计算工具箱)。在NVIDIA Titan X上训练大约需要20个小时。

doTraining = false;如果doTraining net = trainNetwork(dsTrain,lgraph,options);modelDateTime = string(日期时间)“现在”格式=“yyyy-MM-dd-HH-mm-ss”));保存(fullfile (dataDir“multispectralUnet——”+ modelDateTime +“.mat”),“净”);结束

评估分割的准确性

分割验证数据。

segmentMultispectralImage(val_data,net,predictPatchSize);

保存分割图像和地面真相标签为PNG文件。该示例使用这些文件来计算精度度量。

imwrite (segmentedImage“results.png”);imwrite (val_labels“gtruth.png”);

加载分割结果和ground truth使用pixelLabelDatastore(计算机视觉工具箱)

pxdsResults = pixelLabelDatastore(“results.png”一会,pixelLabelIds);pxdsTruth = pixelLabelDatastore(“gtruth.png”一会,pixelLabelIds);

度量语义分割的全局精度evaluateSemanticSegmentation(计算机视觉工具箱)函数。

ssm = evaluateSemanticSegmentation(pxdsResults,pxdsTruth,Metrics= .“global-accuracy”);
评估语义分割结果  ---------------------------------------- * 所选指标:全球精度。*处理1图像。*完成……完成了。*数据集指标:GlobalAccuracy ______________ 0.90411

全局精度分数表明,只有超过90%的像素被正确分类。

参考文献

[1] Kemker, R., C. Salvaggio和C. Kanan。用于语义分割的高分辨率多光谱数据集。CoRR, abs/ 173.01918, 2017。

O. Ronneberger, P. Fischer和T. Brox。U-Net:用于生物医学图像分割的卷积网络。, abs / 1505.04597。2015.

b[3] Kemker, Ronald, Carl Salvaggio和Christopher Kanan。基于深度学习的多光谱遥感图像语义分割算法遥感与摄影测量学报,2018,45(11):60-77。https://doi.org/10.1016/j.isprsjprs.2018.04.014。

另请参阅

(深度学习工具箱)|(深度学习工具箱)||(计算机视觉工具箱)|(计算机视觉工具箱)|(计算机视觉工具箱)|||(计算机视觉工具箱)

相关的话题

外部网站