-
Notifications
You must be signed in to change notification settings - Fork 417
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
DICE coefficient loss function #99
Comments
Hi Alex,
I think the example in Keras is for a binary segmentation task. In that
case you will not use categorical crossentropy and therefore never run
into the argmax problem. The output of the network is simply the pseudo
probability for a pixel being part of the foreground (some valve in
medical images I believe).
Since you use the argmax theano is not able to differentiate through
your loss. Therefore it does not learn.
Cheers,
Fabian
…On 02.02.2017 06:11, Alex wrote:
@FabianIsensee <https://github.com/FabianIsensee>
I am trying to modify the categorical_crossentropy loss function to
dice_coefficient loss function in the Lasagne Unet example. I found
this
<https://github.com/jocicmarko/ultrasound-nerve-segmentation/blob/master/train.py#L21-L29>
implementation in Keras and I modified it for Theano like below:
def dice_coef(y_true, y_pred):
smooth = 1.0
y_true_f = T.flatten(y_true)
y_pred_f = T.flatten(T.argmax(y_pred,-1))
intersection = T.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (T.sum(y_true_f) +
T.sum(y_pred_f) + smooth)
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
In the Lasagne UNet example the output of the flattened layer has
shape (None, 2) where 2 is the number of classes. Hence there is
argmax above. My problem is that the training loss and validation loss
are very way larger (larger than 7000) compared to Keras
implementation (e.g. smaller than 1.0). See below. Any thoughts?
Suggestions?
Started Experiment
Epoch: 0
train accuracy: 0.895438 train loss: 21183.5203804
val accuracy: 0.827439 val loss: 7791.16928308 val AUC score:
0.0277577581283
Epoch: 1
train accuracy: 0.904572 train loss: 26913.4683549
val accuracy: 0.827439 val loss: 7791.16889587 val AUC score:
0.0278832780929
Epoch: 2
train accuracy: 0.902974 train loss: 26987.0089202
val accuracy: 0.827439 val loss: 7791.16909262 val AUC score:
0.493287245499
Epoch: 3
train accuracy: 0.901824 train loss: 23508.2736464
val accuracy: 0.827439 val loss: 7791.16787591 val AUC score:
0.0279265130406
Epoch: 4
train accuracy: 0.898711 train loss: 22958.4583263
val accuracy: 0.827439 val loss: 7791.16905814 val AUC score:
0.0279360519976
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#99>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AKx1FMKOz0eHFhCppgAFHk7PQLsYfe1aks5rYWV9gaJpZM4L0uep>.
|
Hi Fabian, Your answer makes sense. My bad, argmax is not differentiable. (How does Theano even run though?Shouldn't it break? Or is it trying to approximate it?) I will try with another solution to convert the target images into one-hot ones, so that no argmax is required. The reason I am insisting with dice coefficienct is that I think that it could be better than cross entropy for segmentation problems. -Alex |
Hi Alex,
good Idea, I am doing something similar right now. Works quite well.
You can look into the code of the theano argmax function. I think it
returns constant 0, but i am not 100% sure.
Cheers,
Fabian
…On 04.02.2017 01:34, Alex wrote:
Hi Fabian,
Your answer makes sense. My bad, argmax is not differentiable. (How
does Theano even run though?Shouldn't it break? Or is it trying to
approximate it?)
I will try with another solution to convert the target images into
one-hot ones, so that no argmax is required. The reason I am insisting
with dice coefficienct is that I think that it could be better than
cross entropy for segmentation problems.
-Alex
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#99 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AKx1FECQdYyALwnXKpDfw1tg1TouDo65ks5rY8exgaJpZM4L0uep>.
|
@FabianIsensee I implemented the Dice score based on our discussion here. You can see the gist here: https://gist.github.com/mongoose54/71e174587fbec8c2fe970e8a1c14eff4 Although it is not complaining when using as a metric I am getting some weird numbers some times. Have you been able to implement it? Would it be possible to share? |
Hi Alex, sure. It's not like it's a secret or anything ;-)
Make sure that you input shapes are correct and that you use one hot encoding. My implementation will return a soft dice score for each class (output shape is (num_classes, )). I got some decent results with it (same as state of the art on BraTS 2015 train data). Note that this implementation ignores that there may be more than one sample in the batch. You should be able to modify my implementation so that it can deal with batches > 1 as well. If you have any suggestions for improvements please let me know. Cheers, |
IIUC, Should we do a negative of weighted sum of the dice scores for computing the loss to back propagate for a multi class problem? @FabianIsensee |
Hi FabianIsensee, |
The way Fabian implemented it each class will be treated as a binary problem, and you will get a score per class in the end. For training, you can try using the negative sum of the scores as the total loss to minimize. |
@f0k,
|
Your implementation looks complicated; can't you directly translate Fabian's version to numpy? |
@f0k |
Hi, sorry for being late to the party. I would strongly suggest to directly translate my implementation to numpy because that is much easier to read. I have it somewhere as well (will try to find it). edit: I saw a mistake in dice_coef_multi_class (and thus probably the gradient as well). You want a loss function, so something that gets lower the better the network is. Therefore, like Jan mentioned previously, |
Could not find the numpy implementation, made a new one instead. This one will correctly work with batches (which the previous one did not) and is compatible with nd tensors (2d, 3d, etc segmentations):
|
@mongoose54 Should the output of the dice_coef_loss be negated ? |
Yes. It's a loss function and lasagne minimizes the loss. In order to maximize the dice, you need to minimize the negative dice loss |
According to this dissertation (page 72) in which the author discusses their paper, V-Net: Fully Convolutional Neural Networks for Volumetric Medical Image Segmentation, the values in the denominator should be squared. I've adapted @FabianIsensee 's solution in this Gist and welcome critique/discussion of the changes. |
Hi Jeremy, |
Oh I see, thanks! :) |
Hello,@FabianIsensee |
Hi, |
thank you for reply! def dice_coef_loss(y_true, y_pred): |
It is really unfair to call it a design choice. The squared formulation is better because it has an obvious mathematical geometric meaning. Consider the cosine law from high school: Immediate from the cosine law, the squared formulation is the cosine of the prediction and the target, viewed as vectors. The non-squared version is merely "not wrong" per-se, but it takes some mental gymnastics to make it into something meaningful mathematically. |
Hi @liuyipei , I must admit I am probably not as good of a mathematician as I would like to be - there may certainly be theoretical advantages of squaring vs not squaring. I am a man of results though and at least in my experiments squaring does not perform as well. That could be to a number of reasons (hyperparameters tuned for non-squaring), so I am not saying that this observation is going to be true for everyone. But since my results are performing really well on several segmentation leaderboards I am confident that not squaring is a non-issue in practice :-) |
@FabianIsensee
I am trying to modify the categorical_crossentropy loss function to dice_coefficient loss function in the Lasagne Unet example. I found this implementation in Keras and I modified it for Theano like below:
def dice_coef(y_pred,y_true):
smooth = 1.0
y_true_f = T.flatten(y_true)
y_pred_f = T.flatten(T.argmax(y_pred, axis=1))
intersection = T.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (T.sum(y_true_f) + T.sum(y_pred_f) + smooth)
def dice_coef_loss(y_pred, y_true):
return dice_coef(y_pred, y_true)
I am not sure if there is problem with my implementation or Dice coefficient is not robust:. See output during training validation. In comparison when I use categorical crossentropy I get like AUC > 0.98. I was wondering if anyone has played with Dice on UNet.
Started Experiment
Epoch: 0
train accuracy: 0.129538 train loss: -0.146859272992
val accuracy: 0.209342 val loss: -0.282476756789 val AUC score: 0.776537015996
Epoch: 1
train accuracy: 0.418164 train loss: -0.110509629949
val accuracy: 0.820385 val loss: -0.00156800820105 val AUC score: 0.5
Epoch: 2
train accuracy: 0.375172 train loss: -0.129266330856
val accuracy: 0.790744 val loss: -0.00923636992406 val AUC score: 0.5
Epoch: 3
train accuracy: 0.581028 train loss: -0.0889976615506
val accuracy: 0.194278 val loss: -0.279695818208 val AUC score: 0.5
The text was updated successfully, but these errors were encountered: