混淆矩阵与tf.metrics.mean_iou()函数使用举例

更多数学原理小文请关注公众号:未名方略

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.

 

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞行codes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值