Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use tensorflow tf.metrics.mean_iou?

I am trying to use the inbuilt mean_iou function of tensorflow to compute the IoU score for semantic segmentation.

My code is:

#y_mask.shape == [batch_size, h * w, n_classes]
#y_mask.shape == [batch_size, h * w, n_classes]
iou = tf.metrics.mean_iou(tf.argmax(y_mask,2), tf.argmax(mask_,2), n_classes)

However I am getting the following error trace:

tensorflow.python.framework.errors_impl.FailedPreconditionError: 

Attempting to use uninitialized value mean_iou/total_confusion
_matrix
         [[Node: mean_iou/AssignAdd = AssignAdd[T=DT_DOUBLE, _class=["loc:@mean_iou/total_confusion_matrix"], use_locking=false
, _device="/job:localhost/replica:0/task:0/cpu:0"](mean_iou/total_confusion_matrix, mean_iou/confusion_matrix/SparseTensorDense
Add)]]

Caused by op u'mean_iou/AssignAdd', defined at:
  File "sample_tf_ynet.py", line 207, in <module>
    trainSeg()
  File "sample_tf_ynet.py", line 166, in trainSeg
    iou, cm_op = tf.metrics.mean_iou(tf.argmax(y_mask,2), tf.argmax(mask_,2), n_classes)
  File "/home/meetshah1995/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/metrics_impl.py", line 782, in mean_iou
    update_op = state_ops.assign_add(total_cm, current_cm)
  File "/home/meetshah1995/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_state_ops.py", line 75, in assign_ad
d
    use_locking=use_locking, name=name)
  File "/home/meetshah1995/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in a
pply_op
    op_def=op_def)
  File "/home/meetshah1995/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2395, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/meetshah1995/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1264, in __init__
    self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value mean_iou/total_confusion_matrix
         [[Node: mean_iou/AssignAdd = AssignAdd[T=DT_DOUBLE, _class=["loc:@mean_iou/total_confusion_matrix"], use_locking=false
, _device="/job:localhost/replica:0/task:0/cpu:0"](mean_iou/total_confusion_matrix, mean_iou/confusion_matrix/SparseTensorDense
Add)]]

Please guide me on the correct usage of this for semantic segmentation.

like image 507
stochastic_zeitgeist Avatar asked Dec 23 '22 20:12

stochastic_zeitgeist


1 Answers

I solved it by calling

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
like image 108
stochastic_zeitgeist Avatar answered Dec 30 '22 21:12

stochastic_zeitgeist