How to use TensorFlow metrics in Keras : <p>There seem to be several threads/issues on this already but it doesn't appear to me that this has been solved:</p>

<p><a href="https://stackoverflow.com/questions/43158719/how-can-i-use-tensorflow-metric-function-within-keras-models">How can I use tensorflow metric function within keras models?</a></p>

<p><a href="https://github.com/fchollet/keras/issues/6050" rel="noreferrer">https://github.com/fchollet/keras/issues/6050</a></p>

<p><a href="https://github.com/fchollet/keras/issues/3230" rel="noreferrer">https://github.com/fchollet/keras/issues/3230</a></p>

<p>People seem to either run into problems around variable initialization or the metric being 0.</p>

<p>I need to calculate different segmentation metrics and would like to include <a href="https://www.tensorflow.org/api_docs/python/tf/metrics/mean_iou" rel="noreferrer">tf.metric.mean_iou</a> in my Keras model. This is the best I have been able to come up with so far:</p>

<pre><code>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])
</code></pre>

<p>This code does not throw any errors but mean_iou always returns 0. I believe this is because <em>up_opt</em> is not evaluated. I have seen that prior to TF 1.3 <a href="https://github.com/fchollet/keras/issues/6050#issuecomment-301684525" rel="noreferrer">people have suggested</a> to use something along the lines of <em>control_flow_ops.with_dependencies([up_opt], score)</em> to achieve this. This does not seem possible in TF 1.3 anymore.</p>

<p>In summary, how do I evaluate TF 1.3 metrics in Keras 2.0.6? This seems like quite an important feature.</p>
