I am doing multi class segmentation using UNet. My input to the model isHxWxC and my output is,
outputs = layers.Conv2D(n_classes, (1, 1), activation='sigmoid')(decoder0)UsingSparseCategoricalCrossentropy 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 / denominatorHowever, I am actually getting an increasing loss instead of decreasing loss. I have checked multiple sources but all the material I find uses dice loss for binary classification and not multiclass. So my question is there a problem with the implementation.
- @DavidS thanks that did fix the problemHamza Yerlikaya– Hamza Yerlikaya2020-12-05 14:09:02 +00:00CommentedDec 5, 2020 at 14:09
4 Answers4
The problem is that your dice loss doesn't address the number of classes you have but rather assumes binary case, so it might explain the increase in your loss.
You should implement generalized dice loss that accounts for all the classes and return the value for all of them.
Something like the following:
def dice_coef_9cat(y_true, y_pred, smooth=1e-7): ''' Dice coefficient for 10 categories. Ignores background pixel label 0 Pass to model as metric during compile statement ''' y_true_f = K.flatten(K.one_hot(K.cast(y_true, 'int32'), num_classes=10)[...,1:]) y_pred_f = K.flatten(y_pred[...,1:]) intersect = K.sum(y_true_f * y_pred_f, axis=-1) denom = K.sum(y_true_f + y_pred_f, axis=-1) return K.mean((2. * intersect / (denom + smooth)))def dice_coef_9cat_loss(y_true, y_pred): ''' Dice loss to minimize. Pass to model as loss during compile statement ''' return 1 - dice_coef_9cat(y_true, y_pred)This snippet is taken fromhttps://github.com/keras-team/keras/issues/9395#issuecomment-370971561
This is for 9 categories, while you should adjust to the number of categories you have.
1 Comment
SparseCategoricalCrossentropy. I think the problem isy_pred is not converted to one hot when converted loss calculates as expected how ever it results in aValueError: No gradients provided for any variable: errorIf you are doing multi-class segmentation, the 'softmax' activation function should be used.
I would recommend using one-hot encoded ground-truth masks. This needs to be done outside of the loss calculation code.
The generalized dice loss and others were implemented in the following link:
https://github.com/NifTK/NiftyNet/blob/dev/niftynet/layer/loss_segmentation.py
2 Comments
You can use Dice Loss fromsegmentation-models-pytorch library which supports multi-class segmentation. You could install the library by:
pip install -U segmentation-models-pytorchThis is the reference:
1 Comment
Target size (torch.Size([5, 1, 512, 512])) must be the same as input size (torch.Size([5, 2, 512, 512])) but no documentation on constructing the right shape (maybe one-hot in the target?) The package seems not to be maintained anymore either.Not sure why but the last layer has "sigmoid" as activation function.For Multiclass segmentation it has to be "softmax" not "sigmoid".
Also, the loss you are considering is SparseCategoricalCrossentropy along with a multichannel output. If the last layer would have just 1 channel (when doing multi class segmentation), then using SparseCategoricalCrossentropy makes sense but when you have multiple channels as your output the loss which is to be considered is "CategoricalCrossentropy".
Your loss is increasing as the activation and output channels aren't matching (as mentioned above).
change
outputs = layers.Conv2D(n_classes, (1, 1), activation='sigmoid')(decoder0)to
outputs = layers.Conv2D(n_classes, (1, 1), activation='softmax')(decoder0)Comments
Explore related questions
See similar questions with these tags.






