更多数学原理小文请关注公众号:未名方略
IOU = true_positive / (true_positive + false_positive + false_negative)
Calculate per-step mean Intersection-Over-Union
tf.contrib.metrics.streaming_mean_iou
tf.metrics.mean_iou
方法1
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
p = np.array(([0,1,2,3],[0,1,0,1]))
l = np.array(([0,1,2,3],[0,1,1,1]))
label = tf.reshape(l,[2,4])
predicts = tf.reshape(p, [2, 4])
iou_op = tf.metrics.mean_iou(label,predicts,4)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sess.run(iou_op)
mean_iou,conf_mat = sess.run(iou_op)
print(mean_iou)
方法2
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
p = np.array(([0,1,2,3],[0,1,0,1]))
l = np.array(([0,1,2,3],[0,1,1,1]))
predicts = tf.placeholder(tf.int32, shape=[2, 4])
label=tf.placeholder(tf.int32, shape=[2, 4])
iou_op = tf.metrics.mean_iou(label,predicts,4)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sess.run(iou_op,feed_dict={label:l,predicts:p})
mean_iou,conf_mat=sess.run(iou_op,feed_dict={label:l,predicts:p})
方法3
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
p = np.array(([0,1,2,3],[0,1,0,1]))
l = np.array(([0,1,2,3],[0,1,1,1]))
label = tf.reshape(l,[2,4])
predicts = tf.reshape(p, [2, 4])
with tf.Session() as sess:
iou,conf_mat = tf.metrics.mean_iou(label, predicts, num_classes=4)
sess.run(tf.local_variables_initializer())
conf_mat = sess.run([conf_mat])
mean_iou = sess.run([iou])
print('mean_iou = ',mean_iou)
方法4
import tensorflow as tf
tf.reset_default_graph()
p = tf.constant(([0,1,2,3],[0,1,0,1]))
l = tf.constant(([0,1,2,3],[0,1,1,1]))
iou_op = tf.metrics.mean_iou(l,p,4)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
_,conf_mat = sess.run(iou_op)
mean_iou,_ = sess.run(iou_op)
print('mean_iou = ',mean_iou)
输出:('mean_iou = ', [0.85416669])
1次update_op
2次updata_op
Each row of the matrix represents the instances in a predicted class while each column represents the instances in an actual class (or vice versa).
Assuming a sample of 27 animals — 8 cats, 6 dogs, and 13 rabbits, the resulting confusion matrix could look like the table below:
In this confusion matrix, of the 8 actual cats, the system predicted that three were dogs, and of the six dogs, it predicted that one was a rabbit and two were cats. We can see from the matrix that the system in question has trouble distinguishing between cats and dogs, but can make the distinction between rabbits and other types of animals pretty well. All correct predictions are located in the diagonal of the table (highlighted in bold), so it is easy to visually inspect the table for prediction errors, as they will be represented by values outside the diagonal.