如何在 Keras 中使用 TensorFlow 指标 [英] How to use TensorFlow metrics in Keras
问题描述
似乎已经有几个线程/问题,但在我看来这并没有解决:
There seem to be several threads/issues on this already but it doesn't appear to me that this has been solved:
如何在 keras 中使用 tensorflow 度量函数模型?
https://github.com/fchollet/keras/issues/6050
https://github.com/fchollet/keras/issues/3230
人们似乎在变量初始化或指标为 0 方面遇到问题.
People seem to either run into problems around variable initialization or the metric being 0.
我需要计算不同的细分指标,并希望包含 tf.metric.mean_iou 在我的 Keras 模型中.这是迄今为止我能想到的最好的:
I need to calculate different segmentation metrics and would like to include tf.metric.mean_iou in my Keras model. This is the best I have been able to come up with so far:
def mean_iou(y_true, y_pred):
score, up_opt = tf.metrics.mean_iou(y_true, y_pred, NUM_CLASSES)
K.get_session().run(tf.local_variables_initializer())
return score
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=[mean_iou])
这段代码不会抛出任何错误,但 mean_iou 总是返回 0.我相信这是因为 up_opt 没有被评估.我已经看到在 TF 1.3 人们建议之前使用一些东西control_flow_ops.with_dependencies([up_opt], score) 的行来实现这一点.这在 TF 1.3 中似乎不再可能了.
This code does not throw any errors but mean_iou always returns 0. I believe this is because up_opt is not evaluated. I have seen that prior to TF 1.3 people have suggested to use something along the lines of control_flow_ops.with_dependencies([up_opt], score) to achieve this. This does not seem possible in TF 1.3 anymore.
总而言之,我如何评估 Keras 2.0.6 中的 TF 1.3 指标?这似乎是一个非常重要的功能.
In summary, how do I evaluate TF 1.3 metrics in Keras 2.0.6? This seems like quite an important feature.
推荐答案
你仍然可以使用control_dependencies
def mean_iou(y_true, y_pred):
score, up_opt = tf.metrics.mean_iou(y_true, y_pred, NUM_CLASSES)
K.get_session().run(tf.local_variables_initializer())
with tf.control_dependencies([up_opt]):
score = tf.identity(score)
return score
这篇关于如何在 Keras 中使用 TensorFlow 指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!