|
a |
|
b/drunet/loss.py |
|
|
1 |
import tensorflow.keras.backend as K |
|
|
2 |
import tensorflow.keras as keras |
|
|
3 |
import tensorflow as tf |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
def dice_loss(y_true, y_pred): |
|
|
7 |
def dice_coeff(): |
|
|
8 |
smooth = 1 |
|
|
9 |
y_true_f = tf.reshape(y_true, [-1]) |
|
|
10 |
y_pred_f = tf.reshape(y_pred, [-1]) |
|
|
11 |
intersection = tf.reduce_mean(y_true_f * y_pred_f) |
|
|
12 |
score = (2. * intersection + smooth) / (tf.reduce_mean(y_true_f) + tf.reduce_mean(y_pred_f) + smooth) |
|
|
13 |
return score |
|
|
14 |
|
|
|
15 |
return 1 - dice_coeff() |
|
|
16 |
|
|
|
17 |
|
|
|
18 |
def bce_dice_loss(y_true, y_pred): |
|
|
19 |
losses = keras.losses.binary_crossentropy(y_true, y_pred) + dice_loss(y_true, y_pred) |
|
|
20 |
return losses |
|
|
21 |
|
|
|
22 |
|