在深网设计师中训练简单的语义分割网络
此示例显示了如何使用Deep Network Designer创建和训练简单的语义分割网络。
语义分割描述了将图像的每个像素与类标签关联的过程(例如花,,,,person,,,,路,,,,天空,,,,海洋, 或者car). Applications for semantic segmentation include road segmentation for autonomous driving and cancer cell segmentation for medical diagnosis. To learn more, seeGetting Started with Semantic Segmentation Using Deep Learning(计算机视觉工具箱)。
预处理培训数据
训练一个语义分割网络,你需要a collection of images and its corresponding collection of pixel-labeled images. A pixel-labeled image is an image where every pixel value represents the categorical label of that pixel. This example uses a simple data set of 32-by-32 images of triangles for illustration purposes. You can interactively label pixels and export the label data for computer vision applications using图像标签(计算机视觉工具箱)。有关为语义细分应用创建培训数据的更多信息,请参见标签语义分割的像素(计算机视觉工具箱)。
Load the training data.
datafolder = fullfile(toolboxDir(toolboxDir)('想象'),...'VisionData',,,,'triangleImages');ImageDir = fullfile(datafolder,“训练图”);labelDir = fullfile(dataFolder,“训练标签”);
创建一个成像
containing the images.
imds = imagedatastore(ImageDir);
创建一个PixelLabelDatastore
包含地面真象像素标签。该数据集有两个类:“三角形”
and"background"
。
classNames = [“三角形”,,,,"background"];labelids = [255 0];pxds = pixellabeldatastore(labeldir,classNames,labelids);
将图像数据存储和像素标签数据存储组合到一个组合的datastore
对象使用combine
功能。一个组合的数据存储在基础数据存储中的一对图像之间保持均等。
cds = combine(imds,pxds);
构建网络
打开深网设计师。
DeepNetworkDesigner
在Deep Network Designer中,您可以构建,编辑和培训深度学习网络。暂停Blank Network然后单击New。
创建一个semantic segmentation network by dragging layers from the图层库至theDesignerpane.
Connect the layers in this order:
ImageInputlayer
和InputSize
调成32,32,1
convolution2dLayer
和FilterSize
调成3,3
,,,,NumFilters
调成64
,,,,and填充
调成1,1,1,1
Relulayer
maxpooling2dlayer
和PoolSize
调成2,,,,2
,,,,大步
调成2,,,,2
,,,,and填充
调成0,0,0,0
convolution2dLayer
和FilterSize
调成3,3
,,,,NumFilters
调成64
,,,,and填充
调成1,1,1,1
Relulayer
transposedConv2dLayer
和FilterSize
调成4,4
,,,,NumFilters
调成64
,,,,大步
调成2,,,,2
,,,,和种植
调成1,1,1,1
convolution2dLayer
和FilterSize
调成1,1
,,,,NumFilters
调成2
,,,,and填充
调成0,0,0,0
SoftMaxlayer
PixelClassificationLayer
您还可以在命令行中创建此网络,然后使用网络将网络导入到深层网络设计器中DeepNetworkDesigner(层)
。
layers = [imageInputlayer([32 32 1])卷积2Dlayer([3,3],64,'填充',[1,1,1,1])relulayer maxpooling2dlayer([2,2],,“大步”,[2,2])卷积2Dlayer([3,3],64,,,'填充',[1,1,1,1])relulayer transposedconv2dlayer([4,4],64,“大步”,,,,[2,2],'Cropping',,,,[1,1,1,1]) convolution2dLayer([1,1],2) softmaxLayer pixelClassificationLayer ];
该网络是一个基于下采样和上采样设计的简单语义分割网络。有关构建语义分割网络的更多信息,请参见创建一个Semantic Segmentation Network(计算机视觉工具箱)。
导入数据
To import the training datastore, on theData选项卡,选择导入数据>导入数据存储。选择组合的datastore
目的CD
作为培训数据。对于验证数据,选择None
。进口the training data by clicking进口。
Deep Network Designer显示导入的语义分割数据的预览。预览显示训练图像和地面真象像素标签。该网络需要输入图像(左),并将每个像素的分类返回为三角形或背景(右)。
火车网络
Set the training options and train the network.
在训练选项卡,单击训练Options。SetInitialLearnRate至0.001
,,,,MaxEpochs至100
,,,,andMiniBatchSize至64
。Set the training options by clicking关。
单击训练网络Train。
Once training is complete, clickExport将训练有素的网络导出到工作区。训练有素的网络存储在变量中trainedNetwork_1
。
Test Network
Make predictions using test data and the trained network.
使用semanticseg
。Display the labels over the image by using theLabeloverlay
功能。
imgtest = imread('triangletest.jpg');testSeg = semanticseg(imgTest,trainedNetwork_1); testImageSeg = labeloverlay(imgTest,testSeg);
显示结果。
figure imshow(testImageSeg)
The network successfully labels the triangles in the test image.
The semantic segmentation network trained in this example is very simple. To construct more complex semantic segmentation networks, you can use the Computer Vision Toolbox functionssegnetLayers
(计算机视觉工具箱),,,,deeplabv3pluslayers
(计算机视觉工具箱),,,,and未扎layerers
(计算机视觉工具箱)。为了显示如何使用deeplabv3pluslayers
function to create a DeepLab v3+ network, see深度学习语义细分(计算机视觉工具箱)。
也可以看看
Deep Network Designer|图像标签(计算机视觉工具箱)|pixelLabelDatastore
(计算机视觉工具箱)|semanticseg
(计算机视觉工具箱)|Labeloverlay
(Image Processing Toolbox)|PixelClassificationLayer
(计算机视觉工具箱)|trainingOptions
|deeplabv3pluslayers
(计算机视觉工具箱)|segnetLayers
(计算机视觉工具箱)|未扎layerers
(计算机视觉工具箱)