0

I am trying to understand why tf.metrics.mean_iou returns different values in every trials. If input is same over for loop, I guess the output also should be the same. I expected miou = 0.061, but the following code returns unexpected values some times even if the inputs in every loop are the same. Could someone help me please?

import numpy as np
import tensorflow as tf

"""
iou=TP/(TP + FP + FN)
"""

num_iteration = 10
num_classes = 3
l = np.array([[0, 1, 2, 0],
              [1, 0, 1, 0],
              [0, 1, 1, 1],
              [0, 2, 2, 0]])

tf_label = tf.constant(l, dtype=tf.int32)

p0 = np.array([[0.0, 0.1, 0.7, 0.3],
               [0.6, 0.3, 0.4, 0.9],
               [0.3, 0.6, 0.3, 0.1],
               [0.1, 0.2, 0.3, 0.4]])
p1 = np.array([[0.6, 0.1, 0.1, 0.3],
               [0.6, 0.3, 0.4, 0.9],
               [0.3, 0.6, 0.3, 0.1],
               [0.1, 0.5, 0.5, 0.4]])


p2 = 1 - p0 - p1
p = np.stack((p0, p1, p2), axis=2)

tf_logit = tf.constant(p, dtype=tf.float32)
tf_prediction = tf.argmax(tf_logit, axis=2)

miou = tf.metrics.mean_iou(labels=tf_label, predictions=tf_prediction, num_classes=num_classes)
sv = tf.train.Supervisor(logdir=None, summary_op=None)
with sv.managed_session() as sess:
    for i in range(num_iteration):
        label, prediction = sess.run([tf_label, tf_prediction])
        mean_iou, update_op = sess.run(miou)
        print(mean_iou)

the above code returns different values in trials

P-Gn
  • 23,115
  • 9
  • 87
  • 104
Troy
  • 95
  • 1
  • 4

1 Answers1

0

There seems to be a bug in tensorflow when using tf.constants with running metrics; see this post.

P-Gn
  • 23,115
  • 9
  • 87
  • 104