Main Content

用Tversky损失定义自定义像素分类层

This example shows how to define and create a custom pixel classification layer that uses Tversky loss.

This layer can be used to train semantic segmentation networks. To learn more about creating custom deep learning layers, see定义自定义深度学习层

Tversky Loss

The Tversky loss is based on the Tversky index for measuring overlap between two segmented images [1]。Tversky索引 ti c 在一个图像之间 Y 和相应的地面真理 T 是(谁)给的

ti c = m = 1 M Y cm T cm m = 1 M Y cm T cm + α m = 1 M Y cm T c m + β m = 1 M Y c m T cm

  • c corresponds to the class and c 对应于不上课 c

  • M is the number of elements along the first two dimensions of Y

  • α β 是控制每个班级对损失的假阳性和假否定的贡献的加权因素。

The loss L over the number of classes C 是(谁)给的

L = c = 1 C 1 - ti c

Classification Layer Template

Copy the classification layer template into a new file in MATLAB®. This template outlines the structure of a classification layer and includes the functions that define the layer behavior. The rest of the example shows how to complete thetverskypixelClassificationlayer

ClassDeftverskypixelClassificationlayer< nnet.layer.ClassificationLayer特性% Optional propertiesendmethods功能loss = forwardLoss(layer, Y, T)%层转发损失函数是她eendendend

声明层属性

默认情况下,自定义输出层具有以下属性:

  • 姓名– Layer name, specified as a character vector or a string scalar. To include this layer in a layer graph, you must specify a nonempty unique layer name. If you train a series network with this layer and姓名is set to'', then the software automatically assigns a name at training time.

  • Description– One-line description of the layer, specified as a character vector or a string scalar. This description appears when the layer is displayed in aLayerarray. If you do not specify a layer description, then the software displays the layer class name.

  • Type– Type of the layer, specified as a character vector or a string scalar. The value ofTypeappears when the layer is displayed in aLayerarray. If you do not specify a layer type, then the software displays'Classification layer'或者“回归层”

Custom classification layers also have the following property:

  • 课程– Classes of the output layer, specified as a categorical vector, string array, cell array of character vectors, or'auto'。如果课程is'auto',然后该软件在培训时间自动设置类。如果指定字符串阵列或字符向量的单元格数组str,然后该软件将输出层的类设置为分类(str,str)。The default value is'auto'

如果该图层没有其他属性,则可以省略特性section.

The Tversky loss requires a small constant value to prevent division by zero. Specify the property,Epsilon, to hold this value. It also requires two variable properties Alphabetathat control the weighting of false positives and false negatives, respectively.

ClassDeftverskypixelClassificationlayer< nnet.layer.ClassificationLayer特性(Constant)%较小的常数以防止划分为零。Epsilon = 1e-8;end特性% Default weighting coefficients for false positives and false negativesalpha = 0.5;beta = 0.5;end...end

Create Constructor Function

Create the function that constructs the layer and initializes the layer properties. Specify any variables required to create the layer as inputs to the constructor function.

Specify an optional input argument name to assign to the姓名创建的属性。

功能layer = tverskyPixelClassificationLayer(name, alpha, beta)%layer = tverskypixelClassificationLayer(名称)创建一个Tversky%像素分类层带有指定名称。% Set layer namelayer.name = name;%设置层属性layer.Alpha = alpha; layer.Beta = beta;% Set layer descriptionlayer.description ='Tversky loss';end

创建前向损失功能

Create a function namedforwardLoss这返回了网络的预测与训练目标之间的加权交叉熵损失。语法forwardLossisloss = forwardLoss(layer,Y,T), whereY是上一层的输出,Trepresents the training targets.

对于语义细分问题,Tmatch the dimension ofY, whereY是一个四维数组的大小H-by-W-by-K-by-N, whereKis the number of classes, andNis the mini-batch size.

的大小Y取决于上一层的输出。为了保证Yis the same size asT,您必须包括一个在输出层之前输出正确大小的图层。例如,确保Yis a 4-D array of prediction scores forK类,您可以包括大小的完全连接的层K或带有卷积层Kfilters followed by a softmax layer before the output layer.

功能loss = forwardLoss(layer, Y, T)% loss = forwardLoss(layer, Y, T) returns the Tversky loss between% the predictions Y and the training targets T.Pcnot = 1-Y; Gcnot = 1-T; TP = sum(sum(Y.*T,1),2); FP = sum(sum(Y.*Gcnot,1),2); FN = sum(sum(Pcnot.*T,1),2); numer = TP + layer.Epsilon; denom = TP + layer.Alpha*FP + layer.Beta*FN + layer.Epsilon;% Compute Tversky indexlosstic = 1 -numer./denom;lossti = sum(losstic,3);% Return average Tversky index lossn =大小(y,4);损失= sum(lossti)/n;end

Backward Loss Function

作为forwardLoss功能fully supports automatic differentiation, there is no need to create a function for the backward loss.

For a list of functions that support automatic differentiation, seeList of Functions with dlarray Support

完整的层

The completed layer is provided intverskypixelClassificationlayer。m

ClassDeftverskypixelClassificationlayer< nnet.layer.ClassificationLayer% This layer implements the Tversky loss function for training% semantic segmentation networks.% References%Salehi,Seyed Sadegh Mohseni,Deniz Erdogmus和Ali Gholipour。% "Tversky loss function for image segmentation using 3D fully%卷积深网。”机器国际研讨会在医学成像中学习的%。春季,CHAM,2017年。%------------特性(Constant)%较小的常数以防止划分为零。Epsilon = 1e-8;end特性% Default weighting coefficients for False Positives and False% Negativesalpha = 0.5;beta = 0.5;endmethods功能layer = tverskyPixelClassificationLayer(name, alpha, beta)% layer = tverskyPixelClassificationLayer(name, alpha, beta) creates a Tversky%像素分类层带有指定名称和属性alpha和beta。% Set layer name.layer.name = name;layer.Alpha = alpha; layer.Beta = beta;%设置图层描述。layer.description ='Tversky loss';end功能loss = forwardLoss(layer, Y, T)% loss = forwardLoss(layer, Y, T) returns the Tversky loss between% the predictions Y and the training targets T.Pcnot = 1-Y; Gcnot = 1-T; TP = sum(sum(Y.*T,1),2); FP = sum(sum(Y.*Gcnot,1),2); FN = sum(sum(Pcnot.*T,1),2); numer = TP + layer.Epsilon; denom = TP + layer.Alpha*FP + layer.Beta*FN + layer.Epsilon;% Compute tversky indexlosstic = 1 -numer./denom;lossti = sum(losstic,3);% Return average tversky index loss.n =大小(y,4);损失= sum(lossti)/n;endendend

GPU Compatibility

The MATLAB functions used inforwardLossintverskypixelClassificationlayerall supportgpuarrayinputs, so the layer is GPU compatible.

检查输出层有效性

Create an instance of the layer.

layer = tverskyPixelClassificationLayer('tversky', 0.7, 0.3);

Check the validity of the layer by usingcheckLayer。指定有效的输入大小为对图层的典型输入的单个观察大小。该层期望H-by-W-by-K-by-Narray inputs, whereKis the number of classes, andN是迷你批次中的观察次数。

numClasses = 2; validInputSize = [4 4 numClasses]; checkLayer(layer,validInputSize,'ObservationDimension',4)
Skipping GPU tests. No compatible GPU device found. Skipping code generation compatibility tests. To check validity of the layer for code generation, specify the 'CheckCodegenCompatibility' and 'ObservationDimension' options. Running nnet.checklayer.TestOutputLayerWithoutBackward ........ Done nnet.checklayer.TestOutputLayerWithoutBackward __________ Test Summary: 8 Passed, 0 Failed, 0 Incomplete, 2 Skipped. Time elapsed: 0.70266 seconds.

The test summary reports the number of passed, failed, incomplete, and skipped tests.

在语义分割网络中使用自定义层

创建语义分割网络,该网络使用tverskypixelClassificationlayer

layers = [imageInputlayer([32 32 1])卷积2Dlayer(3,64,'Padding',1) batchNormalizationLayer reluLayer maxPooling2dLayer(2,'Stride',2)卷积2Dlayer(3,64,,'Padding',1) reluLayer transposedConv2dLayer(4,64,'Stride',2,'Cropping',1)卷积2Dlayer(1,2)Softmaxlayer TverskypixelClassificationaler('tversky',0.3,0.7)];

Load training data for semantic segmentation usingimageDatastorepixelLabelDatastore

dataSetDir = fullfile(toolboxdir('vision'),'VisionData','triangleImages');ImageDir = fullfile(datasetDir,'trainingImages');labelDir = fullfile(dataSetDir,'trainingLabels');imds = imageDatastore(imageDir); classNames = ["triangle""background"]; labelIDs = [255 0]; pxds = pixelLabelDatastore(labelDir, classNames, labelIDs);

Associate the image and pixel label data by using datastore结合

ds = combine(imds,pxds);

设置培训选项并培训网络。

选项=训练('亚当',...'InitialLearnRate',1E-3,...“ maxepochs”,100,...'LearnRateDropFactor',5e-1,...'LearnRatedRopperiod',20,...'LearnRateSchedule',“分段”,...“ MINIBATCHSIZE”,50); net = trainNetwork(ds,layers,options);
对单个CPU进行培训。初始化输入数据归一化。| ================================================================================= ||时代|迭代|时间经过的时间|迷你批次|迷你批次|基础学习| | | | (hh:mm:ss) | Accuracy | Loss | Rate | |========================================================================================| | 1 | 1 | 00:00:01 | 50.32% | 1.2933 | 0.0010 | | 13 | 50 | 00:00:15 | 98.83% | 0.0991 | 0.0010 | | 25 | 100 | 00:00:33 | 99.33% | 0.0549 | 0.0005 | | 38 | 150 | 00:00:48 | 99.37% | 0.0465 | 0.0005 | | 50 | 200 | 00:01:02 | 99.48% | 0.0400 | 0.0003 | | 63 | 250 | 00:01:14 | 99.47% | 0.0385 | 0.0001 | | 75 | 300 | 00:01:28 | 99.54% | 0.0349 | 0.0001 | | 88 | 350 | 00:01:43 | 99.51% | 0.0353 | 6.2500e-05 | | 100 | 400 | 00:01:55 | 99.56% | 0.0331 | 6.2500e-05 | |========================================================================================| Training finished: Max epochs completed.

通过分割测试图像并显示分割结果来评估训练的网络。

I = imread('triangleTest.jpg');[C,scores] = semanticseg(I,net); B = labeloverlay(I,C); montage({I,B})

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

References

[1] Salehi, Seyed Sadegh Mohseni, Deniz Erdogmus, and Ali Gholipour. "Tversky loss function for image segmentation using 3D fully convolutional deep networks."International Workshop on Machine Learning in Medical Imaging。春季,CHAM,2017年。

See Also

|||(Computer Vision Toolbox)|(Computer Vision Toolbox)

相关话题