如何在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:
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.我的Keras模型中的mean_iou .这是迄今为止我能想到的最好的方法:
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屋!