【发布时间】:2021-06-12 14:40:09
【问题描述】:
我已经建立了自己的神经模型并对其进行了训练,并获得了 99.58% 的准确率。但是我在绘制混淆矩阵时遇到了问题。 flow_from_directory 有一些示例,但 image_dataset_from_directory 没有示例。谁能帮帮我?
【问题讨论】:
标签: image-processing conv-neural-network confusion-matrix
我已经建立了自己的神经模型并对其进行了训练,并获得了 99.58% 的准确率。但是我在绘制混淆矩阵时遇到了问题。 flow_from_directory 有一些示例,但 image_dataset_from_directory 没有示例。谁能帮帮我?
【问题讨论】:
标签: image-processing conv-neural-network confusion-matrix
使用How to plot confusion matrix for prefetched dataset in Tensorflow查看帖子
true_categories = tf.concat([y for x, y in val_ds], axis=0)
获取验证集的真实标签。然后你可以用这样的东西绘制混淆矩阵
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import confusion_matrix
cm = confusion_matrix(true_categories, predicted_id)
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(1,1,1)
sns.set(font_scale=1.4) #for label size
sns.heatmap(cm, annot=True, annot_kws={"size": 12},
cbar = False, cmap='Purples');
ax1.set_ylabel('True Values',fontsize=14)
ax1.set_xlabel('Predicted Values',fontsize=14)
plt.show()
【讨论】:
这是我创建的能够组装混淆矩阵的代码
注意: test_dataset 是一个 tf.data.Dataset 变量。 我使用了validation_dataset = tf.keras.preprocessing.image_dataset_from_directory()
import tensorflow as tf
y_true = []
y_pred = []
for x,y in validation_dataset:
y= tf.argmax(y,axis=1)
y_true.append(y)
y_pred.append(tf.argmax(model.predict(x),axis = 1))
y_pred = tf.concat(y_pred, axis=0)
y_true = tf.concat(y_true, axis=0)
【讨论】: