I am trying to implement the weight-cross entropy mentioned in unet paper to counter the class-imbalances. I am not really able to understand how they are exactly implementing the weight-cross entropy. Quoting the paper-

enter image description here

where $\ell: Ω \to {1,...,K}$ is the true label of each pixel and $w : Ω → \mathbb{R}$ is a weight map that we introduced to give some pixels more importance in the training.

I have the calculated weighted function w(x) but I am not really able to understand how the modified cross-entropy is being calculated here.
Is it just by passing now the weighted function instead of the labels in regular cross-entropy or is it by multiplying the weighted function with the output of product of log of target and labels.


1 Answer 1


The standard unweighted cross-entropy loss is $$ \sum_{\mathbf{x} \in \Omega} \log p_{\ell(\mathbf{x})}(\mathbf{x}). $$ The only difference in the weighted loss $E$ is that you have a set of multiplicative coefficients $w(\mathbf{x})$ that rescale the pointwise log-losses. The overall effect is that some pixels are considered more important to correctly classify.

  • $\begingroup$ Thank You, now this may not be the part of the question and I could post another one if you want but how do you suggest I should incorporate w(x) in the loss. I am trying to do in this way - loss = weight_map * criterion(output,target) $\endgroup$
    – Bing
    Commented Dec 9, 2019 at 1:09

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.