My network for binary semantic segmentation has a sigmoid activation in the last layer, so all predictions are scaled between 0-1.
I want to use the metric tf.keras.metrics.MeanIoU(num_classes) which compares classified predictions (0 or 1) with validation (0 or 1).
The problem is: tf.keras.metrics.MeanIoU works when you give values like 0 or 1, not float values which are given by the sigmoid layers. All values that are not 1 become 0 if I directly apply meanIoU.
So I have to find a way to binarize my values, that come from the output of my network. Morever, tf.keras.metrics.MeanIoU computes the IOU for each semantic class and then computes the average over classes. But I have only one class, which is the foreground, and I can't put num_classes = 1 because the metric needs at least num_class 2 in order to compute the confusion matrix.
So far, I am using
class MyMeanIOU(tf.keras.metrics.MeanIoU):
def update_state(self, y_true, y_pred, sample_weight=None):
th = 0.5
y_pred_binary = tf.cast(y_pred > th, tf.int32)
return super().update_state(y_true, y_pred_binary, sample_weight)
Which I think (but I am not sure) solves the problem about the thresholding, so I obtain all 0 and 1, but I am still confused about the two class part (which, in my case, would be foreground and background I think).
If it's any help: as an input for my neural network, I give images of cells and the ground truth in black and white (0: background, 1: foreground).
Do you have a suggestion on how to do this?
For reference: https://www.tensorflow.org/api_docs/python/tf/keras/metrics/MeanIoU
Thank you!