如何使用tensorflow tf.metrics.mean_iou?

时间:2017-03-07 08:14:38

标签: tensorflow deep-learning conv-neural-network

我正在尝试使用tensorflow的内置mean_iou函数来计算语义分割的IoU分数。

我的代码是:

#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)

但是我收到以下错误跟踪:

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)]]

请指导我正确使用它进行语义分割。

2 个答案:

答案 0 :(得分:4)

我通过调用

解决了这个问题
 String groupColumn=request.getParameter("column");
    switch(groupColumn){
    case "clientName":
        doSumByClientName();
    case "adName":
        doSumByAdName();
    case "spec1":
        doSumBySubStringAdName():
        ....
    }

答案 1 :(得分:3)

最简单的表格我可以提出(3个班级):

# y_pred and y_true are np.arrays of shape [1, size, channels]
with tf.Session() as sess:
    ypredT = tf.constant(np.argmax(y_pred, axis=-1))
    ytrueT = tf.constant(np.argmax(y_true, axis=-1))
    iou,conf_mat = tf.metrics.mean_iou(ytrueT, ypredT, num_classes=3)
    sess.run(tf.local_variables_initializer())
    sess.run([conf_mat])
    miou = sess.run([iou])
    print(miou)

打印:

[0.6127908]