tensorflow均值表示仅用于前景类的二进制语义分割 [英] tensorflow mean iou for just foreground class for binary semantic segmentation

查看:86
本文介绍了tensorflow均值表示仅用于前景类的二进制语义分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tensorflow.metrics.mean_iou()当前在每个类的iou上求平均值.对于二进制语义分割问题,我想只获取iou的前景.

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.

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

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

我将其用作keras fit_generatormetrics,如下所示:

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

我将非常感谢您提供的帮助,因为我尝试了很多事情,甚至自己只是使用keras.backend函数来计算损失,但看起来没有什么是正确的.

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.

推荐答案

如果您使用keras

将keras.backend导入为K

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

这篇关于tensorflow均值表示仅用于前景类的二进制语义分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆