generalizedDice - Generalized Sørensen-Dice similarity coefficient for image
segmentation - MATLAB ([original](https://www.mathworks.com/help/vision/ref/generalizeddice.html)) ([raw](?raw))
Generalized Sørensen-Dice similarity coefficient for image segmentation
Since R2021a
Syntax
Description
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.
[similarity](#mw%5F5fe43569-2f4a-4ae2-9876-049c79b62798) = generalizedDice([X](#mw%5F368b7d5b-8bc0-44c0-8f3a-1021de54668d),[target](#mw%5F1c238fe8-74bc-422c-8334-617012ae7688))
calculates the generalized Sørensen-Dice similarity coefficient between test imageX
and target image target
.
[similarity](#mw%5F5fe43569-2f4a-4ae2-9876-049c79b62798) = generalizedDice([X](#mw%5F368b7d5b-8bc0-44c0-8f3a-1021de54668d),[target](#mw%5F1c238fe8-74bc-422c-8334-617012ae7688),'DataFormat',[dataFormat](#mw%5Fa1edbc3d-5aa1-4bc5-acfe-be5df25d5944))
also specifies the dimension labels, dataFormat
, of unformatted image data. You must use this syntax when the input are unformatted dlarray (Deep Learning Toolbox) objects.
Examples
Calculate Generalized Dice Similarity
Load a pretrained network.
data = load("triangleSegmentationNetwork"); net = data.net;
Load the triangle image data set using imageDatastore
.
dataDir = fullfile(toolboxdir("vision"),"visiondata","triangleImages"); testImageDir = fullfile(dataDir,"testImages"); imds = imageDatastore(testImageDir);
Load ground truth labels for the triangle data set using pixelLabelDatastore
.
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,Classes=classNames);
Encode the categorical predictions and targets using the onehotencode
function.
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)
Calculate Generalized Dice Loss of dlarray
Input
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.
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(S) x 1(C) x 1(B) dlarray
1
Input Arguments
X
— Test image
numeric array | dlarray
object
Test image to be analyzed, specified as one of these values.
- A numeric array of any dimension. The last dimension must correspond to classes.
- An unformatted dlarray (Deep Learning Toolbox) object. You must specify the data format using the dataFormat argument.
- A formatted
dlarray
object. Thedlarray
input must contain a channel dimension,'C'
and can contain a batch dimension,'B'
.
dlarray
input requires Deep Learning Toolbox™.
target
— Target image
numeric array | dlarray
object
Target image, specified as a numeric array or a dlarray (Deep Learning Toolbox) object. The size and format oftarget
must match the size and format of the test image, X. 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 indataFormat
must be one of these labels:
S
— SpatialC
— ChannelB
— 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 formatted dlarray
objects.
Example: 'SSC'
indicates that the array has two spatial dimensions and one channel dimension
Example: 'SSCB'
indicates that the array has two spatial dimensions, one channel dimension, and one batch dimension
Output Arguments
similarity
— Generalized Dice similarity coefficient
numeric scalar | dlarray
object
Generalized Dice similarity coefficient, returned as a numeric scalar or adlarray (Deep Learning Toolbox) object with values in the range [0, 1]. Asimilarity
of 1 means that the segmentations in the two images are a perfect match.
- If the input arrays are numeric images, then
similarity
is a numeric scalar. - If the input arrays are
dlarray
objects, thensimilarity
is adlarray
object of the same dimensionality as the input images. The spatial and channel dimensions ofsimilarity
are singleton dimensions. There is one generalized Dice measurement for each element along the batch dimension.
More About
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 bygeneralizedDice
for the similarity between one image_Y_ and the corresponding ground truth T is given by:
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 [1], [2]. ThegeneralizedDice
function uses squared terms to ensure that the derivative is 0 when the two images match [3].
References
[1] 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.
[2] 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.
[3] 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.
Extended Capabilities
GPU Arrays
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).
Version History
Introduced in R2021a