【问题标题】:tensorflow mean iou for just foreground class for binary semantic segmentationtensorflow mean iou for just foreground class for binary semantic segmentation
【发布时间】:2018-04-08 06:39:24
【问题描述】:

tensorflow.metrics.mean_iou() 当前是每个类的 iou 的平均值。我想为我的二元语义分割问题获得仅前景的iou

我尝试将weights 用作tf.constant([0.0, 1.0]),但tf.constant([0.01, 0.99])mean_iou 看起来仍然溢出,如下所示:

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

我将其用作metrics for keras fit_generator,如下所示:

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

我非常感谢任何帮助,因为我已经尝试了很多事情,甚至只使用 keras.backend 函数自己计算损失,但看起来都不正确。

【问题讨论】:

    标签: tensorflow keras image-segmentation


    【解决方案1】:

    如果你使用 keras

    将 keras.backend 导入为 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
    

    【讨论】:

    • seen_classes 也可以固定为 class_number=2。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2021-10-21
    • 2022-12-02
    • 2023-01-11
    • 2023-02-16
    • 1970-01-01
    相关资源
    最近更新 更多