Generalized Sørensen-Dice similarity coefficient for image segmentation
The generalized Dice similarity coefficient measures the overlap between two segmented images. Generalized Dice similarity is based on Sørensen-Dice similarity and controls the contribution that each class makes to the similarity by weighting classes by the inverse size of the expected region. When working with imbalanced data sets, class weighting helps to prevent the more prevalent classes from dominating the similarity score.
calculates the generalized Sørensen-Dice similarity coefficient between test image
similarity = generalizedDice(
X and target image
also specifies the dimension labels,
similarity = generalizedDice(
dataFormat, of unformatted
image data. You must use this syntax when the input are unformatted
dlarray (Deep Learning Toolbox)
Calculate Generalized Dice Similarity
Load a pretrained network.
data = load('triangleSegmentationNetwork'); net = data.net;
Load the triangle image data set using
dataDir = fullfile(toolboxdir('vision'),'visiondata','triangleImages'); testImageDir = fullfile(dataDir,'testImages'); imds = imageDatastore(testImageDir);
Load ground truth labels for the triangle data set using
labelDir = fullfile(dataDir,'testLabels'); classNames = ["triangle" "background"]; pixelLabelID = [255 0]; pxdsTruth = pixelLabelDatastore(labelDir,classNames,pixelLabelID);
Read a sample image and the corresponding ground truth labels.
I = readimage(imds,1); gTruthLabels = readimage(pxdsTruth,1);
Run semantic segmentation on the image.
[predictions,scores] = semanticseg(I,net);
Encode the categorical predictions and targets using the
featureDim = ndims(predictions) + 1; encodedPredictions = onehotencode(predictions,featureDim); encodedGroundTruthLabels = onehotencode(gTruthLabels,featureDim);
Ignore any undefined classes in the encoded data.
encodedPredictions(isnan(encodedPredictions)) = 0; encodedGroundTruthLabels(isnan(encodedGroundTruthLabels)) = 0;
Compute generalized Dice similarity coefficient between the segmented image and the ground truth.
gDice = generalizedDice(encodedPredictions,encodedGroundTruthLabels)
gDice = 0.4008
Calculate Generalized Dice Loss of
Create input data as a formatted
dlarray object containing 32 observations with unnormalized scores for ten output categories.
spatial = 10; numCategories = 10; batchSize = 32; X = dlarray(rand(spatial,numCategories,batchSize),'SCB');
Convert unnormalized scores to probabilities of membership of each of the ten categories.
X = sigmoid(X);
Create target values for membership in the second and sixth category.
targets = zeros(spatial,numCategories,batchSize); targets(:,2,:) = 1; targets(:,6,:) = 1; targets = dlarray(targets,'SCB');
Compute the generalized Dice similarity coefficient between probability vectors X and targets for multi-label classification.
Z = generalizedDice(X,targets); whos Z
Name Size Bytes Class Attributes Z 1x1x32 262 dlarray
Calculate the generalized Dice loss.
loss = 1 - mean(Z,'all')
loss = 1(S) x 1(C) x 1(B) dlarray 1
X — Test image
numeric array |
Test image to be analyzed, specified as one of these values.
A numeric array of any dimension. The last dimension must correspond to classes.
dlarray(Deep Learning Toolbox) object. You must specify the data format using the
dlarrayinput must contain a channel dimension,
'C'and can contain a batch dimension,
dlarray input requires Deep Learning Toolbox™.
dataFormat — Dimension labels
string scalar | character vector
Dimension labels for unformatted
dlarray image input,
specified as a string scalar or character vector. Each character in
dataFormat must be one of these labels:
B— Batch observations
The format must include one channel label. The format cannot include more
than one channel label or batch label. Do not specify the
dataFormat' argument when the input images are
'SSC' indicates that the array has two spatial
dimensions and one channel dimension
'SSCB' indicates that the array has two spatial
dimensions, one channel dimension, and one batch dimension
similarity — Generalized Dice similarity coefficient
numeric scalar |
Generalized Dice similarity coefficient, returned as a numeric scalar or a
dlarray (Deep Learning Toolbox) object with values in the range [0, 1]. A
similarity of 1 means that the segmentations in the
two images are a perfect match.
If the input arrays are numeric images, then
similarityis a numeric scalar.
If the input arrays are
dlarrayobject of the same dimensionality as the input images. The spatial and channel dimensions of
similarityare singleton dimensions. There is one generalized Dice measurement for each element along the batch dimension.
Generalized Dice Similarity
Generalized Dice similarity is based on Sørensen-Dice similarity for measuring overlap between two segmented images.
The generalized Dice similarity function S used by
generalizedDice for the similarity between one image
Y and the corresponding ground truth T is
K is the number of classes, M is the number of elements along the first two dimensions of Y, and wk is a class specific weighting factor that controls the contribution each class makes to the score. This weighting helps counter the influence of larger regions on the generalized Dice score. wk is typically the inverse area of the expected region:
There are several variations of generalized Dice scores , . The
generalizedDice function uses squared terms to ensure that
the derivative is 0 when the two images match .
 Crum, William R., Oscar Camara, and Derek LG Hill. "Generalized overlap measures for evaluation and validation in medical image analysis." IEEE Transactions on Medical Imaging. 25.11, 2006, pp. 1451–1461.
 Sudre, Carole H., et al. "Generalised Dice overlap as a deep learning loss function for highly unbalanced segmentations." Deep Learning in Medical Image Analysis and Multimodal Learning for Clinical Decision Support. Springer, Cham, 2017, pp. 240–248.
 Milletari, Fausto, Nassir Navab, and Seyed-Ahmad Ahmadi. "V-Net: Fully Convolutional Neural Networks for Volumetric Medical Image Segmentation". Fourth International Conference on 3D Vision (3DV). Stanford, CA, 2016: pp. 565–571.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Introduced in R2021a
dlarray (Deep Learning Toolbox) |
onehotencode (Deep Learning Toolbox)