0

I am trying to use tf.boolean_mask to get a masked mean difference for image segmentation:

def custom_loss(image):
    def loss(predicted_y, target_y):
        pred_mask = tf.math.greater(predicted_y,0.5)
        target_mask = tf.math.greater(target_y,0.5)
        mean_diff = (tf.reduce_mean(tf.boolean_mask(image,pred_mask)) - tf.reduce_mean(tf.boolean_mask(image,target_mask))) ** 2
        return mean_diff
return loss

Unfortunately, I am getting a ValueError: No gradients provided for any variable, which would logically be caused by the tf.boolean_mask. Any way to do this on Tensorflow 2.0?

Thanks a lot!

illan
  • 163
  • 1
  • 13
  • No, it's not possible. Losses need "continuous" and "differentiable" variables. – Daniel Möller Feb 17 '20 at 18:03
  • Are you sure you won't go well with an `'mse'` or a `'binary_crossentropy'`? – Daniel Möller Feb 17 '20 at 18:07
  • This will be used for an application that calculates the quantitative mean value within the mask. Crossentropy does okay in terms of area overlap with the target mask. But I need to further improve the accuracy/spread of measuring the mean value within the mask. – illan Feb 17 '20 at 19:10

0 Answers0