imatge-upc / liverseg-2017-nipsws

Detection-aided Liver Lesion Segmentation
https://imatge-upc.github.io/liverseg-2017-nipsws/
MIT License
97 stars 52 forks source link

use "if" may not work tf has solution tf.cond #21

Closed TX-Yeager closed 5 years ago

TX-Yeager commented 5 years ago

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 the pictures' pixel is zero that means that is nothing masked in the ct
# so the dice is 1.
y_pred_sum = tf.reduce_sum(y_pred)
y_true_sum = tf.reduce_sum(y_true)
# if (y_pred_sum == 0) and (y_true_sum == 0):
#     dice = 1

def f1(): return 1.0
def f2(): return dice
result = tf.cond(tf.equal(y_pred_sum,y_true_sum),f1,f2)
dice = result
result = tf.cond(tf.less(y_pred_sum,500),f1,f2)
dice = result
return dice, y_pred_sum, y_true_sum