I am working on image segmentation and I have a problem regarding the loss functions I am using dice loss as the loss metric. I found that if the y_pred is not decimal between 0 and 1, the dice coeff seems to have a problem. To show an example: lets define dice_coef as follow. And I randomly generate a 2,2,2 tensor with floating points between 0,1 I thought if I input the same tensor to dice_coef, the output should be 1 since both are identical. However it is not the case in the output. Then I try using another tensor with only 0 and 1. The output is 1 as expected.
import tensorflow as tf
def dice_coef(y_true,y_pred):
intersection = tf.reduce_sum(y_true * y_pred) #first intersect before reduce_sum
denominator = tf.reduce_sum(y_true + y_pred)
return (2*intersection)/(denominator)
float_x= np.random.random((2,2,2))
print(float_x)
#[[[0.59969584 0.04508543]
[0.88194256 0.9227732 ]]
[[0.37010776 0.95773481]
[0.04879277 0.47606463]]]
print(dice_coef(float_x,float_x))
# tf.Tensor(0.6299142707152602, shape=(), dtype=float64)
int_x = np.random.randint(2, size=(2, 2, 2))
print(dice_coef(int_x,int_x))
# tf.Tensor(1.0, shape=(), dtype=float64)
It seems that dice_coef only works in integer 0 and 1 only? So if the output of the segmentation model during training is floating point 0-1, should I be thresholding the value before optimizing using loss function?
Thank you