frankkramer-lab / MIScnn

A framework for Medical Image Segmentation with Convolutional Neural Networks and Deep Learning
GNU General Public License v3.0
402 stars 116 forks source link

Dice coefficient #79

Open ramchandracheke opened 3 years ago

ramchandracheke commented 3 years ago

Hi,

Thanks for your amazing code. Does it support calculation of dice score without background?

Thanks, Ram

muellerdo commented 3 years ago

Hey @ramchandracheke,

thank you for your kind words.

Does it support calculation of dice score without background?

No, currently MIScnn only provides custom metrics listed in here: https://github.com/frankkramer-lab/MIScnn/blob/master/miscnn/neural_network/metrics.py

However, if you find/implement a keras compatible metric, you can simply pass it to the MIScnn Neural Network object.

But, personally I wouldn't recommend a loss function ignoring any class (including background). For strongly imbalanced datasets (99% background, 1% ROI), I would recommend utilizing some kind of focal loss function with dynamic focussing/weighting of classes.

Sadly, MIScnn hasn't integrated an official focal loss implementation to its metric library. However, a student research assistant of mine has already implemented and successfully tested it. He is currently working on code cleanup for creating a pull request to integrate it in the MIScnn dev branch, soon.

Focal Loss Publication: https://arxiv.org/abs/1708.02002 Third-party TF/Keras metric implementation which may be helpful: https://github.com/umbertogriffo/focal-loss-keras

Hope that I was able to help you.

Cheers, Dominik

muellerdo commented 3 years ago

PS: Wait do you need it for model training or for evaluation purposes?

If you need it for evaluation then I would recommend to check out this work of us for 3D covid-19 segmentation: https://github.com/frankkramer-lab/covid19.MIScnn/blob/master/scripts/run_evaluation.py

In this evaluation script, I loaded the ground truth and prediction via MIScnn, and then compute the DSC (as well as sensitivity, etc) for each class. You can simply exclude the background results then.

ramchandracheke commented 3 years ago

Hi @muellerdo

Thank you for your guidance. I am training a model using model.evaluate function. Yes, one of my friend used MISCNN framework for his masters project. I found it very helpful rather than coding from scratch. I am trying to recreate a scenario where the background is ignored and avg dice score for 2 classes were calculated per epoch. Since background is always start with high dice score, this is little bit misleading in terms of comparing result with their implementation.

Thanks for your suggestion on Focal loss, that's interesting! Please let me know once that implement goes live. I am happy to try it.

https://docs.monai.io/en/latest/losses.html#diceloss They have included (include_background=True,) term in dice loss. Thanks, Ram

ramchandracheke commented 3 years ago

Hi,

I have written a function for dice coefficient with No background. It assume network has BHWD[N] Batch, Height, Width, Depth, No of channels.

def dice_coefficient_nb(y_true, y_pred, smooth=0.00001): y_true=y_true[:,:,:,:,1:] y_pred=y_pred[:,:,:,:,1:] y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f y_pred_f) return (2. intersection + smooth) / \ (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)

def dice_coefficient_loss_nb(y_true, y_pred): return 1-dice_coefficient_nb(y_true, y_pred)