Main Content

generalizedDice

Generalized Sørensen-Dice similarity coefficient for image segmentation

Since R2021a

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 = generalizedDice(X,target) calculates the generalized Sørensen-Dice similarity coefficient between test image X and target image target.

example

similarity = generalizedDice(X,target,'DataFormat',dataFormat) 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

collapse all

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)
gDice = 
0.9346

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

Input Arguments

collapse all

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. The dlarray input must contain a channel dimension, 'C' and can contain a batch dimension, 'B'.

dlarray input requires Deep Learning Toolbox™.

Target image, specified as a numeric array or a dlarray (Deep Learning Toolbox) object. The size and format of target must match the size and format of the test image, X. dlarray input requires Deep Learning Toolbox.

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:

  • S — Spatial

  • C — Channel

  • 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 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

collapse all

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 similarity is a numeric scalar.

  • If the input arrays are dlarray objects, then similarity is a dlarray object of the same dimensionality as the input images. The spatial and channel dimensions of similarity are singleton dimensions. There is one generalized Dice measurement for each element along the batch dimension.

More About

collapse all

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 given by:

S=2k=1Kwkm=1MYkmTkmk=1Kwkm=1MYkm2+Tkm2

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:

wk=1(m=1MTkm)2

There are several variations of generalized Dice scores [1], [2]. The generalizedDice 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

Version History

Introduced in R2021a

See Also

| (Deep Learning Toolbox) | | (Deep Learning Toolbox)