tensorflow意味着只用于二进制语义分割的前景类

时间:2018-04-08 06:39:24

标签: tensorflow keras image-segmentation

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用于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函数计算损失,但看起来没什么。

1 个答案:

答案 0 :(得分: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