tf.metrics.accuracy
返回两个值,第一个值是本次运行前的准确度,第二个值是本次运行后的准确度。
#encoding=utf-8
import numpy as np
import tensorflow as tf
x = tf.placeholder(tf.int32, [5])
y = tf.placeholder(tf.int32, [5])
acc, acc_op = tf.metrics.accuracy(labels=x, predictions=y)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
v = sess.run([acc, acc_op], feed_dict={x: [1, 0, 0, 0, 0], y: [1, 0, 0, 0, 1]})
#共5、共正确4
print(v) #[0.0, 0.8]
v = sess.run([acc, acc_op], feed_dict={x: [1, 1, 1, 1, 0], y: [0, 0, 0, 0, 1]})
#共10、共正确4
print(v) #[0.8, 0.4]
v = sess.run([acc, acc_op], feed_dict={x: [1, 1, 1, 1, 0], y: [1, 1, 1, 1, 0]})
#共15、共正确9
print(v) #[0.4, 0.6]
v = sess.run([acc, acc_op], feed_dict={x: [1, 1, 1, 1, 0], y: [1, 0, 0, 0, 1]})
#共20、共正确10
print(v) #[0.6, 0.5]
mean_iou, update_op = tf.metrics.mean_iou()。其中mean_iou是平均交并比,update_op是不断更新的混淆矩阵。
类别的,。
- TP:true_positive,正样本被预测为正样本的个数。
- FP:false_positive,负样本被预测为正样本的个数。
- TN:true_negative,正样本被预测为负样本的个数。
- FN:false_negative,负样本被预测为负样本的个数。
比如label=[[0, 1, 2, 3], [0, 1, 1, 1]],predic=[[0, 1, 2, 3], [0, 1, 0, 1]]时,对于类别0:
- TP=2:label中的两个0被预测为0(绿色)。
- FP=1:label中有一个非0被预测为0(蓝色)。
- FN=0:label中没有出现0被预测为非0的情况。
因此,同理,,。所以。
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())
mean_iou, conf_mat = sess.run(iou_op)
print
print(conf_mat)
mean_iou, conf_mat = sess.run(iou_op)
print
print(mean_iou)
print
print(conf_mat)