Main Content


(To be removed) Create pixel classification layer using generalized Dice loss for semantic segmentation

Since R2019b

The DicePixelClassificationLayer object will be removed in a future release. Use the trainnet (Deep Learning Toolbox) function and specify the loss using the generalizedDice function. For more information, see Compatibility Considerations.


A Dice pixel classification layer provides a categorical label for each image pixel or voxel using generalized Dice loss.

The layer uses generalized Dice loss to alleviate the problem of class imbalance in semantic segmentation problems. Generalized Dice loss controls the contribution that each class makes to the loss by weighting classes by the inverse size of the expected region.




layer = dicePixelClassificationLayer creates a Dice pixel classification output layer for semantic image segmentation networks. The layer outputs the categorical label for each image pixel or voxel processed by a CNN. The layer automatically ignores undefined pixel labels during training.

layer = dicePixelClassificationLayer(Name,Value) returns a Dice pixel classification output layer using name-value arguments to set the optional Classes and Name properties. You can specify multiple name-value arguments.

For example, dicePixelClassificationLayer("Name","pixclass") creates a Dice pixel classification layer with the name "pixclass".


expand all

Classes of the output layer, specified as a categorical vector, string array, cell array of character vectors, or "auto". If Classes is "auto", then the software automatically sets the classes at training time. If you specify the string array or cell array of character vectors str, then the software sets the classes of the output layer to categorical(str,str).

Data Types: char | categorical | string | cell

This property is read-only.

The output size of the layer. The value is 'auto' prior to training, and is specified as a numeric value at training time.

This property is read-only.

Loss function used for training, specified as 'generalizedDiceLoss'.

Layer name, specified as a character vector or a string scalar. For Layer array input, the trainnet (Deep Learning Toolbox) and dlnetwork (Deep Learning Toolbox) functions automatically assign names to layers with the name "".

The DicePixelClassificationLayer object stores this property as a character vector.

Data Types: char | string

This property is read-only.

Number of inputs to the layer, returned as 1. This layer accepts a single input only.

Data Types: double

This property is read-only.

Input names, returned as {'in'}. This layer accepts a single input only.

Data Types: cell


collapse all

Predict the categorical label of every pixel in an input image using a generalized Dice loss function.

layers = [
      imageInputLayer([480 640 3])
layers = 
  6x1 Layer array with layers:

     1   ''   Image Input                       480x640x3 images with 'zerocenter' normalization
     2   ''   2-D Convolution                   16 3x3 convolutions with stride [2  2] and padding [1  1  1  1]
     3   ''   ReLU                              ReLU
     4   ''   2-D Transposed Convolution        4 2x2 transposed convolutions with stride [2  2] and cropping [0  0  0  0]
     5   ''   Softmax                           softmax
     6   ''   Dice Pixel Classification Layer   Generalized Dice loss 

More About

expand all


[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 R2019b

collapse all

R2024a: dicePixelClassificationLayer will be removed

The dicePixelClassificationLayer object will be removed in a future release. Follow these steps to update your code:

  • Define your network as a dlnetwork (Deep Learning Toolbox) object. You can use functions such as addLayers (Deep Learning Toolbox) and connectLayers (Deep Learning Toolbox) to build the network. Do not include output layers in the network.

  • Define a custom loss function that uses the generalizedDice function. Here is a sample loss function appropriate for Dice pixel classification:

    function loss = modelLoss(Y,T)
      z = generalizedDice(Y,T); 
      loss = 1 - mean(z,"all"); 
  • Train the network using the trainnet (Deep Learning Toolbox) function with the custom loss function. For example, this code trains a dlnetwork network called net using the training data images and the custom loss function modelLoss.

    netTrained = trainnet(images,net,@modelLoss,options);