我想使用tensorflow mean_iou函数并编写以下示例代码;但这给了我错误信息
尝试使用未初始化的值mean_iou_5 / total_confusion_matrix [[{{node mean_iou_5 / total_confusion_matrix / read}}]]
有人可以告诉我如何使用tensorflow的mean_iou函数吗?
谢谢。
labels1 = tf.convert_to_tensor([[3,1,2],[2,3,1]],tf.int32)
pred = tf.convert_to_tensor ([[3,1,2],[2,3,1]],tf.int32)
test,conf_mat = tf.metrics.mean_iou(labels = labels1, predictions = pred, num_classes = 3)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
init_op.run()
print('test',sess.run(test))
答案 0 :(得分:1)
从StackOverflow答案中获取:https://stackoverflow.com/a/49326455/9820369
# 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)