-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Dice Loss (and Intersection Over Union) #10890
Comments
Any updates on this? |
For anyone interested in this, I've implemented an IoU metric for evaluating semantic segmentation results here. I'd be happy to merge it into the Keras codebase. |
I can also confirm that these metrics are important to segmentation tasks in the biometric domain. It would be really great to have an official implementation available. |
I can also confirm the need of these metrics and loss functions in the field of medical image segmentation, that often deals with data with great class imbalances. An official implementation would be deeply appreciated! |
After working with the I/U metrics for some time with Keras, I noticed unexpected values compared to reported baselines. After reviewing how metrics are calculated in Keras, I discovered that they are calculated batch-wise and averaged together. This is contrary to how I/U is typically calculated for semantic segmentation tasks, i.e., over an entire subset of data. Batch-wise I/U may be useful in certain contexts and applications, so a simple I/U metric built in may still be worth considering. However, correct semantic segmentation evaluation necessitates a novel solution. Potential solutions I've considered to correctly calculate I/U dataset-wise:
I ended up using tek 3 to implement SegNet and Tiramisu in Keras as it's the simplest solution and I only really needed test time I/U metrics anyway as loss and accuracy are enough for the training process. Anyone interested can find the evaluation code here and the individual metrics that module uses here. EDIT: clarify meaning and some grammar |
I would also really want this; I'm going crazy implementing it. I'll just spam my question here as well: I am trying to perform semantic segmentation in TensorFlow 1.10 with eager execution with the generalized dice loss function:
However, I am struggling to get any meaningful loss which isn't always 1. What am I doing wrong here? After the initial weights (one for each class) are calculated, they contain many
which seems fine to me, though they are pretty small. The numerators (
which also looks reasonable, since they're basically the labels' respective sizes times the network's certainty about them (which is likely low in the beginning of training). The denominators (
These are large, but that is to be expected since the class probabilities of a pixel sum to 1, and therefore the sum of these denominators should more or less equal the amount of pixels with ground truth. However, summing the numerators gives a very small sum (~0.001, though occasionally it's in a single digit range) while the denominator sums to very large values. This results in my final loss being exclusively 1, or something really close to that. Does anyone know how I can mitigate this effect and obtain stable gradients? |
Would it be possible to request this as a loss in |
I propose that Dice Score/Loss (also known as F1-score or Sorensen score) is added as a metric and loss function as these are very commonly used in image segmentation or bounding box problems. Within the medical community, this is an incredibly important function, although I have seen it in other areas like astronomy.
Additionally, the Intersection Over Union (IoU) (also known as Jaccard Index) is another important metric/loss for these same classes of problem. While the Dice and IoU are very similar functions, the Dice Score weights true positives (the intersection) more heavily than false positives and false negatives than IoU (which gives a more even weighting to TP, FP, & FN). In cases where false positives and false negatives can be very detrimental, the IoU will produce a better result than Dice. Andrew Ng (Stanford Prof, Google Brain co-founder, Coursera founder) even devotes an entire video to IoU in his convolutional neural networks (CNN's) course as the metric to use to determine if your bounding box predictions are working.
According to Pull #7032 which sought to add Dice, the end result was the request was closed and that it will only be added if the community continues to bring it up and express interest in adding Dice (See Comment by @fchollet ).
As time has passed and interest in CNN's has skyrocketed, I suggest that we reconsider adding Dice and IoU as they are becoming more and more common place. Dice has been proposed or mentioned in issues and pull requests multiple times (#292, #369, #2115, #2994, #3442, #3457, #3611, #3653, #3977, #5916, #6933, #7032, #8961, #9154, #9275, #9395, #9444, #9671, #10783). Additionally, IoU has also been mentioned a number of times in this repository (#2016, #2185, #6467, #6538, #8225, #8643, #8669, #9367, #10104, #10602, #10783). Keep in mind that those references are only with in this repository... there are plenty of other Github repositories that use Dice and/or IoU as a loss function.
Furthermore, the research community has used Dice or IoU in numerous papers that make use of CNN's. Here are a few that each have over 100 citations according to Google Scholar, though many more exist... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (sorry ahead of time if they are behind a paywall).
I personally have been working in medical research with a U-Net for image segmentation and have found that training the model with binary cross entropy as my loss function at first and then switching to dice loss for additional training has significantly improved my performance over using only binary cross entropy. I have been using a custom loss to use Dice loss, however, it would be great to see an official version of this supported by Keras.
Given that over a year has past since PR #7032, would the Keras team reconsider implementing an official version of Dice and IoU loss functions?
The text was updated successfully, but these errors were encountered: