Skip to content
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 coef in keras #13085

Closed
panxiaobai opened this issue Jul 10, 2019 · 2 comments
Closed

dice coef in keras #13085

panxiaobai opened this issue Jul 10, 2019 · 2 comments

Comments

@panxiaobai
Copy link

I want to implement 3D U-net in keras for medical image segmention.My data(LiTS Dataset,3 classes,background\liver\tumor) ara very imbalance,so crossentropy loss will only predict background.So I try the dice loss for it.I had tried many implement of dice loss,it always got high dice coef,but only predict background either.Here are my implement,shape of tensor is (batch,width,height,depth,num of classes).

def dice_coef_fun_mean(smooth=1):
    def dice_coef_mean(y_true, y_pred):
        intersection = K.sum(y_true * y_pred, axis=(1,2,3,4))
        union = K.sum(y_true, axis=(1,2,3,4)) + K.sum(y_pred, axis=(1,2,3,4))
        sample_dices=(2. * intersection + smooth) / (union + smooth)
        dices=K.mean(sample_dices,axis=0)
        return dices 
    return dice_coef_mean

def dice_coef_fun_flatten(smooth=1):
    def dice_coef_flatten(y_true, y_pred):
        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)
    return dice_coef_flatten

def dice_coef_fun_cate_mean(smooth=1):
    def dice_coef_cate_mean(y_true, y_pred):
        intersection = K.sum(y_true * y_pred, axis=(1, 2, 3))
        union = K.sum(y_true, axis=(1, 2, 3)) + K.sum(y_pred, axis=(1, 2, 3))
        dices = K.mean((2. * intersection + smooth) / (union + smooth), axis=0)  
        return K.mean(dices) 
    return dice_coef_cate_mean

So I dicide to see the 3rd class's dice coef during the train(because the 3rd class(tumor) is so small).I use the follow implement for metrics.

def dice_coef_intersection(y_true, y_pred,smooth=1.):
    intersection=K.sum(y_true[:,:,:,:,2]*y_pred[:,:,:,:,2])
    return intersection

def dice_coef_union(y_true, y_pred,smooth=1.):
    union=K.sum(y_true[:,:,:,:,2]) + K.sum(y_pred[:,:,:,:,2])
    return union

def dice_coef_dice(y_true, y_pred,smooth=1.):
    intersection = dice_coef_intersection(y_true=y_true,y_pred=y_pred)
    union = dice_coef_union(y_true=y_true,y_pred=y_pred)
    print(intersection)
    print(union)
    dices =(2.* intersection+smooth)/(union+smooth)
    return dices

def TP_fun(y_true,y_pred):
    return K.sum(y_true * y_pred, axis=(0,1,2,3,4))

def T_fun(y_true,y_pred):
    return K.sum(y_true[:,:,:,:,1:2],axis=(0,1,2,3,4))

def P_fun(y_true,y_pred):
    return K.sum(y_pred[:,:,:,:,1:2],axis=(0,1,2,3,4))

Strange things happen here
1、My implement of dice coef,dice_coef_fun_mean and dice_coef_fun_flatten get the same result,but it have difference in mathematics
2、After training for a while,categorical_accuracy wil get the same result with dice_coef_fun_mean and dice_coef_fun_flatten
3、dice_coef_dice=(2*dice_coef_intersection+smooth)/(dice_coef_union+smooth) is mathematically correct,but the result is wrong

Here are the begining log of train:

Epoch 1/100
1/104 [..............................] - ETA: 20:06 - loss: 0.9723 - dice_coef_cate_mean: 0.0243 - dice_coef_flatten: 0.0277 - dice_coef_mean: 0.0277 - categorical_accuracy: 0.0272 - P_fun: 854577.4375 - T_fun: 37844.0000 - TP_fun: 32640.9238 - dice_coef_intersection: 25990.6289 - dice_coef_union: 344839.7812 - dice_coef_dice: 0.1507
2/104 [..............................] - ETA: 10:43 - loss: 0.9657 - dice_coef_cate_mean: 0.0254 - dice_coef_flatten: 0.0343 - dice_coef_mean: 0.0343 - categorical_accuracy: 0.0317 - P_fun: 678586.1250 - T_fun: 26536.0000 - TP_fun: 40451.7412 - dice_coef_intersection: 12995.3145 - dice_coef_union: 486811.3906 - dice_coef_dice: 0.0754
3/104 [..............................] - ETA: 7:34 - loss: 0.9674 - dice_coef_cate_mean: 0.0232 - dice_coef_flatten: 0.0326 - dice_coef_mean: 0.0326 - categorical_accuracy: 0.0304 - P_fun: 752765.1042 - T_fun: 17690.6667 - TP_fun: 38460.7233 - dice_coef_intersection: 8663.5430 - dice_coef_union: 405889.6562 - dice_coef_dice: 0.0502
4/104 [>.............................] - ETA: 6:00 - loss: 0.9684 - dice_coef_cate_mean: 0.0222 - dice_coef_flatten: 0.0316 - dice_coef_mean: 0.0316 - categorical_accuracy: 0.0296 - P_fun: 788469.2656 - T_fun: 22684.0000 - TP_fun: 37301.4839 - dice_coef_intersection: 6497.6572 - dice_coef_union: 366879.4102 - dice_coef_dice: 0.0377
5/104 [>.............................] - ETA: 5:02 - loss: 0.9698 - dice_coef_cate_mean: 0.0229 - dice_coef_flatten: 0.0302 - dice_coef_mean: 0.0302 - categorical_accuracy: 0.0282 - P_fun: 707264.8625 - T_fun: 120655.4000 - TP_fun: 35631.1871 - dice_coef_intersection: 5198.1258 - dice_coef_union: 447026.8281 - dice_coef_dice: 0.0302
6/104 [>.............................] - ETA: 4:24 - loss: 0.9643 - dice_coef_cate_mean: 0.0258 - dice_coef_flatten: 0.0357 - dice_coef_mean: 0.0357 - categorical_accuracy: 0.0338 - P_fun: 730923.0417 - T_fun: 107003.5000 - TP_fun: 42139.7445 - dice_coef_intersection: 4331.7715 - dice_coef_union: 414780.6458 - dice_coef_dice: 0.0251

The dice_coef_intersection,dice_coef_union and dice_coef_dice of the first batch ara correct,but after that they went all wrong.
Here are the log After training for a while:

Epoch 9/100
1/104 [..............................] - ETA: 1:33 - loss: 0.0293 - dice_coef_cate_mean: 0.7713 - dice_coef_flatten: 0.9707 - dice_coef_mean: 0.9707 - categorical_accuracy: 0.9707 - P_fun: 0.0103 - T_fun: 34518.0000 - TP_fun: 1145129.0000 - dice_coef_intersection: 0.0000e+00 - dice_coef_union: 1.0167 - dice_coef_dice: 0.4959
2/104 [..............................] - ETA: 1:32 - loss: 0.0388 - dice_coef_cate_mean: 0.8267 - dice_coef_flatten: 0.9612 - dice_coef_mean: 0.9612 - categorical_accuracy: 0.9612 - P_fun: 0.0053 - T_fun: 45827.5000 - TP_fun: 1133819.8750 - dice_coef_intersection: 0.0000e+00 - dice_coef_union: 0.6776 - dice_coef_dice: 0.6215
3/104 [..............................] - ETA: 1:32 - loss: 0.0260 - dice_coef_cate_mean: 0.8493 - dice_coef_flatten: 0.9740 - dice_coef_mean: 0.9740 - categorical_accuracy: 0.9740 - P_fun: 0.0035 - T_fun: 30642.3333 - TP_fun: 1149005.1667 - dice_coef_intersection: 0.0000e+00 - dice_coef_union: 0.5540 - dice_coef_dice: 0.6694
4/104 [>.............................] - ETA: 1:32 - loss: 0.0574 - dice_coef_cate_mean: 0.8103 - dice_coef_flatten: 0.9426 - dice_coef_mean: 0.9426 - categorical_accuracy: 0.9426 - P_fun: 0.1008 - T_fun: 67638.5000 - TP_fun: 1111989.2812 - dice_coef_intersection: 1.1482e-15 - dice_coef_union: 20.2002 - dice_coef_dice: 0.5052
5/104 [>.............................] - ETA: 1:30 - loss: 0.0481 - dice_coef_cate_mean: 0.8273 - dice_coef_flatten: 0.9519 - dice_coef_mean: 0.9519 - categorical_accuracy: 0.9519 - P_fun: 0.0806 - T_fun: 56743.0000 - TP_fun: 1122888.7750 - dice_coef_intersection: 9.1855e-16 - dice_coef_union: 16.2105 - dice_coef_dice: 0.5639
6/104 [>.............................] - ETA: 1:29 - loss: 0.0579 - dice_coef_cate_mean: 0.8367 - dice_coef_flatten: 0.9421 - dice_coef_mean: 0.9421 - categorical_accuracy: 0.9421 - P_fun: 0.0672 - T_fun: 68296.8333 - TP_fun: 1111337.6458 - dice_coef_intersection: 7.6546e-16 - dice_coef_union: 13.5310 - dice_coef_dice: 0.6170
7/104 [=>............................] - ETA: 1:29 - loss: 0.0829 - dice_coef_cate_mean: 0.8351 - dice_coef_flatten: 0.9171 - dice_coef_mean: 0.9171 - categorical_accuracy: 0.9171 - P_fun: 0.0577 - T_fun: 97727.4286 - TP_fun: 1081908.9554 - dice_coef_intersection: 6.5611e-16 - dice_coef_union: 11.6336 - dice_coef_dice: 0.6432
8/104 [=>............................] - ETA: 1:28 - loss: 0.0869 - dice_coef_cate_mean: 0.8415 - dice_coef_flatten: 0.9131 - dice_coef_mean: 0.9131 - categorical_accuracy: 0.9131 - P_fun: 0.0505 - T_fun: 102518.0000 - TP_fun: 1077119.8359 - dice_coef_intersection: 5.7409e-16 - dice_coef_union: 10.1877 - dice_coef_dice: 0.6800

System information

  • Have I written custom code (as opposed to using example directory):
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 16.04.3 LTS
  • TensorFlow backend (yes / no): yes
  • TensorFlow version: 1.12
  • Keras version: 2.2.4
  • Python version: 3.6
  • CUDA/cuDNN version: 9.0
  • GPU model and memory: gtx 1080ti 12G

So what's wrong with my code?or it's a bug in keras?

#10890
#2994
#3611
#9395

@JulianKlug
Copy link

shouldn't it be
intersection = K.sum(K.abs(y_true * y_pred), axis=-1)

@kaushikramabhotla
Copy link

kaushikramabhotla commented Jun 26, 2021

shouldn't it be
intersection = K.sum(K.abs(y_true * y_pred), axis=-1)

Hello sir, getting this error while training the model with the dice loss

def updated_dice(y_true, y_pred):
  smooth = 1.
  y_true_f = K.flatten(y_true)
  y_pred_f = K.flatten(y_pred)
  intersection = K.sum(K.abs(y_true * y_pred), axis=-1)
  score = (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth) 
  return score

ERROR :
TypeError: in user code:

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:855 train_function  *
    return step_function(self, iterator)
<ipython-input-143-bac88b53cf03>:5 updated_dice  *
    intersection = K.sum(K.abs(y_true * y_pred), axis=-1)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py:1250 binary_op_wrapper
    raise e
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py:1234 binary_op_wrapper
    return func(x, y, name=name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py:1575 _mul_dispatch
    return multiply(x, y, name=name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
    return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py:530 multiply
    return gen_math_ops.mul(x, y, name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/gen_math_ops.py:6250 mul
    "Mul", x=x, y=y, name=name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/op_def_library.py:558 _apply_op_helper
    inferred_from[input_arg.type_attr]))

TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type uint8 of argument 'x'.

Please help me resolve this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants