# Implementing Multiclass Dice Loss Function

I am doing multi class segmentation using UNet. My input to the model is HxWxC and my output is,

outputs = layers.Conv2D(n_classes, (1, 1), activation='sigmoid')(decoder0)


Using SparseCategoricalCrossentropy I can train the network fine. Now I would like to also try dice coefficient as the loss function. Implemented as follows,

def dice_loss(y_true, y_pred, smooth=1e-6):
y_true = tf.cast(y_true, tf.float32)
y_pred = tf.math.sigmoid(y_pred)

numerator = 2 * tf.reduce_sum(y_true * y_pred) + smooth
denominator = tf.reduce_sum(y_true + y_pred) + smooth

return 1 - numerator / denominator


However I am actually getting an increasing loss instead of decreasing loss. I have checked multiple sources but all the material I find use diceloss for binary classification and not multiclass. So my question is is is there a problem with the implementation.

• Can you please put your main specific question in the title? – nbro Dec 3 '20 at 19:07