Tensorflow mean_iou gives error

I'm using tensorflow 1.13 for semantic segmentation, and I wanted to use IoU metric which is already defined in Tensorflow Metrics. Why am I getting this issue?

Caused by op 'mean_iou/total_confusion_matrix/read', defined at:
  File "/home/user/Development/train.py", line 57, in <module>
    IoU, IoU_Op = tf.metrics.mean_iou(labels=y_true, predictions=pred, num_classes=2)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/metrics_impl.py", line 1134, in mean_iou
    num_classes, weights)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/metrics_impl.py", line 256, in _streaming_confusion_matrix
    [num_classes, num_classes], dtypes.float64, name='total_confusion_matrix')
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/metrics_impl.py", line 85, in metric_variable
    name=name)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 213, in __call__
    return cls._variable_v1_call(*args, **kwargs)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 176, in _variable_v1_call
    aggregation=aggregation)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 155, in <lambda>
    previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/variable_scope.py", line 2495, in default_variable_creator
    expected_shape=expected_shape, import_scope=import_scope)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 217, in __call__
    return super(VariableMetaclass, cls).__call__(*args, **kwargs)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 1395, in __init__
    constraint=constraint)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 1557, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper
    return target(*args, **kwargs)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 81, in identity
    ret = gen_array_ops.identity(input, name=name)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3890, in identity
    "Identity", input=input, name=name)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
    op_def=op_def)
  File "/home/user/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1801, in __init__
    self._traceback = tf_stack.extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value mean_iou/total_confusion_matrix
	 [[node mean_iou/total_confusion_matrix/read (defined at /home/user/Development/train.py:57) ]]
	 [[node mean_iou/confusion_matrix/zeros (defined at /home/user/Development/train.py:57) ]]
tensorflowioutensorflow-metrics
4 votesJW234.00
1 Answers
JO220.00
2

Just use this function. It works for me, and does not give any error.

import tensorflow as tf

def IoU(y_pred, y_true):
    I = tf.reduce_sum(y_pred * y_true, axis=(1, 2))
    U = tf.reduce_sum(y_pred + y_true, axis=(1, 2)) - I
    return tf.reduce_mean(I / U)
Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?