这是与tf.contrib.metrics.MetricSpec一起使用的功能(使用Estimator时)。它的灵感来自Jerod的答案和metric_op.py源文件。您将获得一个包含百分比的流式混淆矩阵:
from tensorflow.python.framework import ops,dtypes
from tensorflow.python.ops import array_ops,variables
def _createLocalVariable(name, shape, collections=None,
validate_shape=True,
dtype=dtypes.float32):
"""Creates a new local variable.
"""
# Make sure local variables are added to
# tf.GraphKeys.LOCAL_VARIABLES
collections = list(collections or [])
collections += [ops.GraphKeys.LOCAL_VARIABLES]
return variables.Variable(
initial_value=array_ops.zeros(shape, dtype=dtype),
name=name,
trainable=False,
collections=collections,
validate_shape=validate_shape)
def streamingConfusionMatrix(label, prediction,
weights=None,num_classes=None):
"""
Compute a streaming confusion matrix
:param label: True labels
:param prediction: Predicted labels
:param weights: (Optional) weights (unused)
:param num_classes: Number of labels for the confusion matrix
:return: (percentConfusionMatrix,updateOp)
"""
# Compute a per-batch confusion
batch_confusion = tf.confusion_matrix(label, prediction,
num_classes=num_classes,
name='batch_confusion')
count = _createLocalVariable(None,(),dtype=tf.int32)
confusion = _createLocalVariable('streamConfusion',[num_classes,
num_classes],dtype=tf.int32)
# Create the update op for doing a "+=" accumulation on the batch
countUpdate = count.assign(count + tf.reduce_sum(batch_confusion))
confusionUpdate = confusion.assign(confusion + batch_confusion)
updateOp = tf.group(confusionUpdate,countUpdate)
percentConfusion = 100 * tf.truediv(confusion,count)
return percentConfusion,updateOp
然后可以通过以下方式将其用作评估指标:
from tensorflow.contrib import learn,metrics
#[...]
evalMetrics = {'accuracy':
learn.MetricSpec(metric_fn=metrics.streaming_accuracy),
'confusionMatrix':learn.MetricSpec(metric_fn=
lambda
label,prediction,weights=None:
streamingConfusionMatrix(
label,prediction,weights,num_classes=nLabels))}
我建议您使用numpy.set_printoptions(precision = 2,suppress = True)进行打印。
0
我想在张量板上看到混乱矩阵的图像。为此,我正在修改Tensorflow Slim的评估示例: https : //github.com/tensorflow/models/blob/master/slim/eval_image_classifier.py
在此示例代码中,已经提供了“准确性”,但是无法直接添加“混淆矩阵”指标,因为它没有流式传输。
流指标和非流指标有什么区别?
因此,我尝试像这样添加它:
这会在张量板上创建图像,但可能存在格式问题。矩阵应在0-1之间归一化,以便产生有意义的图像。
如何产生有意义的混淆矩阵?如何处理多批次评估流程?