A commonly loss function used for semantic segmentation is the dice loss function. (see the image below. It resume how I understand it)

enter image description here

Using it with a neural network, the output layer can yield label with a softmax or probability with a sigmoid. But how the dice loss works with a probility output ?

The numerator multiply each label (1 or 0) of the predicted and ground truth. Both pixels need to be set to 1 to be in the green area. What is the result with a probability like 0.7 ? Does the numerator result in a floating number (ie with ground truth = [1, 1] and predicted = [0.7, 0], the "green" area of the numerator would be 0.7) ?


2 Answers 2


I think there is a bit of confusion here. The dice coefficient is defined for binary classification. Softmax is used for multiclass classification.

Softmax and sigmoid are both interpreted as probabilities, the difference is in what these probabilities are. For binary classification they are basically equivalent, but for multiclass classification there is a difference.

When you apply sigmoid, you make sure that all output neurons are between 0 and 1.

When you apply softmax, you make sure that all output neurons are between 0 and 1 AND that they sum up to 1.

This means, when the output is sigmoid, the input data can be in several classes at the same time. For softmax, you force the network to pick one of the classes.

The code you posted here is correct for binary classification, but for multiclass, there is some intricacy when it comes to combining the classes.

Since in your example the target consists of two pixels, each labeled either 0 or 1, you are dealing with binary classification and should use pixel-wise sigmoid in the first place, i.e. the probabilities from your model should be e.g. [0.7, 0.8] or something like that.

Pixel-wise softmax should only be used if each pixel could be in only one of many classes and softmax over all pixels does not make much sense, as this would force the model to pick one out of many pixels to label as 1.

  • $\begingroup$ Thank you for clarified that up. Based on your comment and my mistake about how softmax works apart, you are telling that dice loss does indeed works with a denominator = (2 * predicted_probability_map * ground_thrue_label_per_class) ? $\endgroup$
    – Chopin
    Commented Jun 10, 2020 at 11:52
  • $\begingroup$ I guess you meant the numerator = ..., but yes, in particular the code you posted in the linked question seems to be correct. $\endgroup$
    – matthiaw91
    Commented Jun 10, 2020 at 12:12

The Dice coefficient tells you how well your model is performing when it comes to detecting boundaries with regards to your ground truth data. The loss is computed with 1 - Dice coefficient where the the dice coefficient is between 0-1.

Over every epoch the loss will determine the acceleration of learning and the updates of weights to reduce the loss as much as possible. The dice coefficient also takes into account global and local composition of pixels, thereby providing better boundary detection than a weighted cross entropy.

  • $\begingroup$ I know what is dice loss and how it works. My question is about the probability map outputed by the neural network in regards to the dice loss computation. $\endgroup$
    – Chopin
    Commented Jun 10, 2020 at 8:50
  • $\begingroup$ Apologize for not understanding the question right. How can the predicted be a probability map? Based on the threshold for probabilities, it either marks a pixel as a boundary or not. There would be a conversion from probability to an actual class. $\endgroup$
    – Nischal Hp
    Commented Jun 10, 2020 at 9:01
  • $\begingroup$ when using a sigmoid (rather than a softmax), the output is a probability map where each pixels is given a probability to be labeled. One can use post processing with a threshold >0.5 to obtaint a labeled map. Using softmax with dice loss is common and works. I'm wondering if my interpretation is correct. $\endgroup$
    – Chopin
    Commented Jun 10, 2020 at 9:03
  • $\begingroup$ I definitely think that interpretation is correct. Simply because ground truth does not have probabilities to compare and Dice loss needs discrete comparisons between predicted and GT in order to identify the similarities. $\endgroup$
    – Nischal Hp
    Commented Jun 10, 2020 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.