[541a77]: / ModelArchitecture / DiceLoss.py

Download this file

16 lines (11 with data), 487 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import keras.backend as K
import tensorflow as tf
def dice_metric_loss(ground_truth, predictions, smooth=1e-6):
ground_truth = K.cast(ground_truth, tf.float32)
predictions = K.cast(predictions, tf.float32)
ground_truth = K.flatten(ground_truth)
predictions = K.flatten(predictions)
intersection = K.sum(predictions * ground_truth)
union = K.sum(predictions) + K.sum(ground_truth)
dice = (2. * intersection + smooth) / (union + smooth)
return 1 - dice