tensorflow mean iou for just foreground class for binary semantic segmentation

不想你离开。 提交于 2019-12-24 14:13:34

问题


tensorflow.metrics.mean_iou() currently averages over the iou of each class. I want to get the iou of only foreground in for my binary semantic segmentation problem.

I tried using weights as tf.constant([0.0, 1.0]) but that tf.constant([0.01, 0.99]) but the mean_iou looks still overflowed as following:

(500, 1024, 1024, 1)
119/5000 [..............................] - ETA: 4536s - loss: 0.3897 - mean_iou: -789716217654962048.0000 - acc: 0.9335

I am using this as metrics for keras fit_generator as following:

def mean_iou(y_true, y_pred):
    y_pred = tf.to_int32(y_pred > 0.5)
    score, up_opt = tf.metrics.mean_iou(y_true, y_pred, 2, weights = tf.constant([0.01, 0.99]))
    keras.get_session().run(tf.local_variables_initializer())
    with tf.control_dependencies([up_opt]):
        score = tf.identity(score)
    return score

I will really appreciate any help as I have tried many things, even calculating loss myself using just keras.backend functions but nothing looks correct.


回答1:


if you use keras

import keras.backend as K

def switch_mean_iou(labels, predictions):
    """
    labels,prediction with shape of [batch,height,width,class_number=2]
    """
    mean_iou = K.variable(0.0)
    seen_classes = K.variable(0.0)

    for c in range(2):
        labels_c = K.cast(K.equal(labels, c), K.floatx())
        pred_c = K.cast(K.equal(predictions, c), K.floatx())

        labels_c_sum = K.sum(labels_c)
        pred_c_sum = K.sum(pred_c)

        intersect = K.sum(labels_c*pred_c)
        union = labels_c_sum + pred_c_sum - intersect
        iou = intersect / union
        condition = K.equal(union, 0)
        mean_iou = K.switch(condition,
                            mean_iou,
                            mean_iou+iou)
        seen_classes = K.switch(condition,
                                seen_classes,
                                seen_classes+1)

    mean_iou = K.switch(K.equal(seen_classes, 0),
                        mean_iou,
                        mean_iou/seen_classes)
    return mean_iou


来源:https://stackoverflow.com/questions/49715192/tensorflow-mean-iou-for-just-foreground-class-for-binary-semantic-segmentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!