I'm currently trying to train a image segmentation model with keras. I want my model to return a mask (image with only 0s and 1s) to apply to the input image to get only the interesting part. When I train my model using mse loss, it returns masks with values significantly lower than 1, even though it seems to converge. So I implemented a custom loss function
def loss(y_true, y_pred):
tresholded_pred = tf.where(y_pred >= 0.5, 1.0, 0.0)
sq_diff = tf.square(y_true - tresholded_pred)
return tf.reduce_mean(sq_diff, axis=-1)
However I've got the following error:
ValueError: No gradients provided for any variable
I assume this is because of the non-differentiability of my function. How can I achieve what I want without having such errors ?
I've also tried to implement the tresholding with a lambda layer, and it raised the exact same error. I've been through a lot of similar topics, but the solutions aren't satisfying so far.