TensorBoard中的Tensorflow混淆矩阵 [英] Tensorflow Confusion Matrix in TensorBoard

查看:571
本文介绍了TensorBoard中的Tensorflow混淆矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在张量板上看到混乱矩阵的图像。为此,我正在修改Tensorflow Slim的评估示例:



这里是可以为您做所有事情的函数。

  from textwrap import wrap 
import re
import itertools
import tfplot
import matplotlib
numpy as np
from sklearn.metrics import confusion_matrix



def plot_confusion_matrix(correct_labels,predict_labels,labels,title ='Confusion matrix',tensor_name ='MyFigure / image',normalize = False):
'''
参数:
correct_labels:这是您真正的分类类别。
预测_标签:这些是您预测的分类类别
标签:这是一排标签,将用于显示axix标签
title ='Confusion matrix':矩阵$ b的标题$ b tensor_name ='MyFigure / image':输出和的名称可能张量

返回:
摘要:TensorFlow摘要

其他需要注意的项目:
-根据类别和数据的数量,您可能必须修改图形,字体大小等。
-当前,某些刻度由于旋转而无法对齐。
'''
cm = confusion_matrix(correct_labels,predict_labels,label = labels)如果标准化,则

cm = cm.astype('float')* 10 / cm.sum (axis = 1)[:, np.newaxis]
cm = np.nan_to_num(cm,copy = True)
cm = cm.astype('int')

np.set_printoptions(precision = 2)
### fig,ax = matplotlib.figure.Figure()

fig = matplotlib.figure.Figure(figsize =(7,7), dpi = 320,facecolor ='w',edgecolor ='k')
ax = fig.add_subplot(1,1,1)
im = ax.imshow(cm,cmap ='Oranges')

classes = [re.sub(r'([az](?= [AZ])| [AZ](?= [AZ] [az])))',r'\1 ',x)对于标签中的x]
classes = ['\n'.join(wrap(l,40))对于l中的类]

tick_marks = np.arange( len(classes))

ax.set_xlabel('Predicted',fontsize = 7)
ax.set_xticks(tick_marks)
c = ax.set_xticklabels(classes,fontsize = 4, rotation = -90,ha ='center')
ax.xaxis.set_label_position('bottom')
ax.xaxis.tick_bottom()

ax.set_ylabel('True标签',fontsize = 7)
ax.se t_yticks(tick_marks)
ax.set_yticklabels(class,fontsize = 4,va ='center')
ax.yaxis.set_label_position('left')
ax.yaxis.tick_left()$在itertools.product(range(cm.shape [0]),range(cm.shape [1]))中的i,j的b
$ b:
ax.text(j,i,format (cm [i,j],'d')if cm [i,j]!= 0 else'。',horizo​​ntalalignment = center,fontsize = 6,verticalalignment ='center',color = black)
fig.set_tight_layout(True)
summary = tfplot.figure.to_summary(fig,tag = tensor_name)
返回摘要





这是调用此函数所需的其余代码。

 '''混淆矩阵摘要'''
img_d_summary_dir = os.path.join(checkpoint_dir,摘要 , img)
img_d_summary_writer = tf.summary.FileWriter(img_d_summary_dir,sess.graph)
img_d_summary = plot_confusion_matrix(correct_labels,predict_labels,labels,tensor_name ='dev / cm_d)$ b $ mary img .add_summary(img_d_summary,current_step)

混淆了!!!


I want to have a visual of confusion matrix in tensorboard. To do this, I am modifying Evaluation example of Tensorflow Slim: https://github.com/tensorflow/models/blob/master/slim/eval_image_classifier.py

In this example code, Accuracy already provided but it is not possible to add "confusion matrix" metric directly because it is not streaming.

What is difference between streaming metrics and non-streaming ones?

Therefore, I tried to add it like this:

c_matrix = slim.metrics.confusion_matrix(predictions, labels)

#These operations needed for image summary
c_matrix = tf.cast(c_matrix, uint8)
c_matrix = tf.expand_dims(c_matrix, 2)
c_matrix = tf.expand_dims(c_matrix, 0)

op = tf.image_summary("confusion matrix", c_matrix, collections=[])
tf.add_to_collection(tf.GraphKeys.SUMMARIES, op)

This creates an image in tensorboard but probably there is a formatting problem. Matrix should be normalized between 0-1 so that It produces meaningful image.

How can I produce a meaningful confusion matrix? How can I deal with multi batch evaluation process?

解决方案

Here is something I have put together That works reasonably well. Still need to adjust a few things like the tick placements etc.

Here is the function that will pretty much do everything for you.

from textwrap import wrap
import re
import itertools
import tfplot
import matplotlib
import numpy as np
from sklearn.metrics import confusion_matrix



def plot_confusion_matrix(correct_labels, predict_labels, labels, title='Confusion matrix', tensor_name = 'MyFigure/image', normalize=False):
''' 
Parameters:
    correct_labels                  : These are your true classification categories.
    predict_labels                  : These are you predicted classification categories
    labels                          : This is a lit of labels which will be used to display the axix labels
    title='Confusion matrix'        : Title for your matrix
    tensor_name = 'MyFigure/image'  : Name for the output summay tensor

Returns:
    summary: TensorFlow summary 

Other itema to note:
    - Depending on the number of category and the data , you may have to modify the figzie, font sizes etc. 
    - Currently, some of the ticks dont line up due to rotations.
'''
cm = confusion_matrix(correct_labels, predict_labels, labels=labels)
if normalize:
    cm = cm.astype('float')*10 / cm.sum(axis=1)[:, np.newaxis]
    cm = np.nan_to_num(cm, copy=True)
    cm = cm.astype('int')

np.set_printoptions(precision=2)
###fig, ax = matplotlib.figure.Figure()

fig = matplotlib.figure.Figure(figsize=(7, 7), dpi=320, facecolor='w', edgecolor='k')
ax = fig.add_subplot(1, 1, 1)
im = ax.imshow(cm, cmap='Oranges')

classes = [re.sub(r'([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))', r'\1 ', x) for x in labels]
classes = ['\n'.join(wrap(l, 40)) for l in classes]

tick_marks = np.arange(len(classes))

ax.set_xlabel('Predicted', fontsize=7)
ax.set_xticks(tick_marks)
c = ax.set_xticklabels(classes, fontsize=4, rotation=-90,  ha='center')
ax.xaxis.set_label_position('bottom')
ax.xaxis.tick_bottom()

ax.set_ylabel('True Label', fontsize=7)
ax.set_yticks(tick_marks)
ax.set_yticklabels(classes, fontsize=4, va ='center')
ax.yaxis.set_label_position('left')
ax.yaxis.tick_left()

for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
    ax.text(j, i, format(cm[i, j], 'd') if cm[i,j]!=0 else '.', horizontalalignment="center", fontsize=6, verticalalignment='center', color= "black")
fig.set_tight_layout(True)
summary = tfplot.figure.to_summary(fig, tag=tensor_name)
return summary

#

And here is the rest of the code that you will need to call this functions.

''' confusion matrix summaries '''
img_d_summary_dir = os.path.join(checkpoint_dir, "summaries", "img")
img_d_summary_writer = tf.summary.FileWriter(img_d_summary_dir, sess.graph)
img_d_summary = plot_confusion_matrix(correct_labels, predict_labels, labels, tensor_name='dev/cm')
img_d_summary_writer.add_summary(img_d_summary, current_step)

Confuse away!!!

这篇关于TensorBoard中的Tensorflow混淆矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆