I have a semantic segmentation task to predict 5 channel mask using UNET for example mask shape is (224,244,5).
I'm using this function for IOU :
def mean_iou(y_true, y_pred):
y_pred = tf.round(tf.cast(y_pred, tf.int32))
intersect = tf.reduce_sum(tf.cast(y_true, tf.float32) * tf.cast(y_pred, tf.float32), axis=[1])
union = tf.reduce_sum(tf.cast(y_true, tf.float32),axis=[1]) + tf.reduce_sum(tf.cast(y_pred, tf.float32),axis=[1])
smooth = tf.ones(tf.shape(intersect))
return tf.reduce_mean((intersect + smooth) / (union - intersect + smooth))
def iou_loss(y_true, y_pred):
y_true = tf.reshape(y_true, [-1])
y_pred = tf.reshape(y_pred, [-1])
intersection = tf.reduce_sum(tf.cast(y_true, tf.float32) * tf.cast(y_pred, tf.float32))
score = (intersection + 1.) / (tf.reduce_sum(tf.cast(y_true, tf.float32)) +
tf.reduce_sum(tf.cast(y_pred, tf.float32)) - intersection + 1.)
return 1 - score`
and the output layer of UNET model :
outputs = tf.keras.layers.Conv2D(5, (1, 1), activation='softmax')(c9)
model = tf.keras.Model(inputs=[input_img], outputs=[outputs])
opti = tf.keras.optimizers.Adam(lr=0.003, clipvalue=0.7)
model.compile(optimizer=opti, loss=iou_loss, metrics=['accuracy',mean_iou])
But I'm not sure is the IOU function the correct implementation or not,
could you clarify this.