def dice_coef_theoretical(y_pred, y_true):
"""Define the dice coefficient
Args:
y_pred: Prediction
y_true: Ground truth Label
Returns:
Dice coefficient
"""
y_true_f = tf.cast(tf.reshape(y_true, [-1]), tf.float32)
y_pred_f = tf.nn.sigmoid(y_pred)
y_pred_f = tf.cast(tf.greater(y_pred_f, 0.5), tf.float32)
y_pred_f = tf.cast(tf.reshape(y_pred_f, [-1]), tf.float32)
intersection = tf.reduce_sum(y_true_f * y_pred_f)
union = tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f)
dice = (2. * intersection) / (union + 0.00001)
if (tf.reduce_sum(y_pred) == 0) and (tf.reduce_sum(y_true) == 0):
dice = 1
return dice
评论列表
文章目录