混淆矩阵与 TensorFlow
Confusion Matrix with Tensorflow
我在我自己的数据集上使用@kratzert 编写的微调 AlexNet 架构,它工作正常(我从这里得到代码:https://github.com/kratzert/finetune_alexnet_with_tensorflow)我想弄清楚如何从他的构建混淆矩阵代码。我曾尝试使用 tf.confusion_matrix(labels, predictions, num_classes)
来构建混淆矩阵,但我做不到。我很困惑标签和预测的值应该是什么,我的意思是,我知道应该是什么,但每次我输入这些值时都会出错。任何人都可以帮助我解决这个问题或查看代码(link 上方)并指导我吗?
我在 finetune.py 中恰好在计算准确度后添加了这两行,以使标签和预测成为 class 的数量。
with tf.name_scope("accuracy"):
correct_pred = tf.equal(tf.argmax(score, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
**true_class = tf.argmax(y, 1)
predicted_class = tf.argmax(score, 1)**
并且在保存模型的检查点之前,我在会话的最底部添加了 tf.confusion_matrix()
for _ in range(val_batches_per_epoch):
img_batch, label_batch = sess.run(next_batch)
acc, cost = sess.run([accuracy, loss], feed_dict={x: img_batch,
y: label_batch,
keep_prob: 1.})
test_acc += acc
test_count += 1
test_acc /= test_count
print("{} Validation Accuracy = {:.4f} -- Validation Loss = {:.4f}".format(datetime.now(),test_acc, cost))
print("{} Saving checkpoint of model...".format(datetime.now()))
**print(sess.run(tf.confusion_matrix(true_class, predicted_class, num_classes)))**
# save checkpoint of the model
checkpoint_name = os.path.join(checkpoint_path,
'model_epoch'+str(epoch+1)+'.ckpt')
save_path = saver.save(sess, checkpoint_name)
print("{} Model checkpoint saved at {}".format(datetime.now(),
checkpoint_name))
我也试过其他地方,但每次都会报错:
Caused by op 'Placeholder_1', defined at:
File "/home/armin/Desktop/Alexnet_DataPipeline/finetune.py", line 85, in <module>
y = tf.placeholder(tf.float32, [batch_size, num_classes])
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 1777, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 4521, in placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3290, in create_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1654, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [128,3]
任何帮助将不胜感激,谢谢。
您指的是一段相当长的代码,您没有指定放置混淆矩阵行的位置。
根据经验,混淆矩阵最常见的问题是 tf.confusion_matrix()
需要标签和预测作为 class 的数量,而不是单热向量。换句话说,标签和预测应该是数字 5
的形式,而不是 [0, 0, 0, 0, 0, 1, 0, 0, 0, 0 ].
在您引用的代码中,y
是单热格式。网络的输出 score
是一个向量,给出每个 class 的概率。这也不是必需的格式。你需要做类似
的事情
true_class = tf.argmax( y, 1 )
predicted_class = tf.argmax( score, 1 )
并使用像
这样的混淆矩阵
tf.confusion_matrix( true_class, predicted_class, num_classes )
(基本上,如果您查看 finetune.py 的第 123 行,其中包含用于确定准确性的两个元素,但它们未保存在单独的张量中。)
如果您想保留所有批次的混淆矩阵总数 运行,您只需将它们相加 - 因为矩阵的每个单元格都会计算属于该类别的示例数量,一个元素-wise 加法为整个集合创建混淆矩阵:
cm_running_total = None
cm_nupmy_array = sess.run(tf.confusion_matrix(true_class, predicted_class, num_classes), feed_dict={x: img_batch, y: label_batch, keep_prob: 1.} )
if cm_running_total is None:
cm_running_total = cm_numpy_array
else:
cm_running_total += cm_numpy_array
我在我自己的数据集上使用@kratzert 编写的微调 AlexNet 架构,它工作正常(我从这里得到代码:https://github.com/kratzert/finetune_alexnet_with_tensorflow)我想弄清楚如何从他的构建混淆矩阵代码。我曾尝试使用 tf.confusion_matrix(labels, predictions, num_classes)
来构建混淆矩阵,但我做不到。我很困惑标签和预测的值应该是什么,我的意思是,我知道应该是什么,但每次我输入这些值时都会出错。任何人都可以帮助我解决这个问题或查看代码(link 上方)并指导我吗?
我在 finetune.py 中恰好在计算准确度后添加了这两行,以使标签和预测成为 class 的数量。
with tf.name_scope("accuracy"):
correct_pred = tf.equal(tf.argmax(score, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
**true_class = tf.argmax(y, 1)
predicted_class = tf.argmax(score, 1)**
并且在保存模型的检查点之前,我在会话的最底部添加了 tf.confusion_matrix()
for _ in range(val_batches_per_epoch):
img_batch, label_batch = sess.run(next_batch)
acc, cost = sess.run([accuracy, loss], feed_dict={x: img_batch,
y: label_batch,
keep_prob: 1.})
test_acc += acc
test_count += 1
test_acc /= test_count
print("{} Validation Accuracy = {:.4f} -- Validation Loss = {:.4f}".format(datetime.now(),test_acc, cost))
print("{} Saving checkpoint of model...".format(datetime.now()))
**print(sess.run(tf.confusion_matrix(true_class, predicted_class, num_classes)))**
# save checkpoint of the model
checkpoint_name = os.path.join(checkpoint_path,
'model_epoch'+str(epoch+1)+'.ckpt')
save_path = saver.save(sess, checkpoint_name)
print("{} Model checkpoint saved at {}".format(datetime.now(),
checkpoint_name))
我也试过其他地方,但每次都会报错:
Caused by op 'Placeholder_1', defined at:
File "/home/armin/Desktop/Alexnet_DataPipeline/finetune.py", line 85, in <module>
y = tf.placeholder(tf.float32, [batch_size, num_classes])
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 1777, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 4521, in placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3290, in create_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1654, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [128,3]
任何帮助将不胜感激,谢谢。
您指的是一段相当长的代码,您没有指定放置混淆矩阵行的位置。
根据经验,混淆矩阵最常见的问题是 tf.confusion_matrix()
需要标签和预测作为 class 的数量,而不是单热向量。换句话说,标签和预测应该是数字 5
的形式,而不是 [0, 0, 0, 0, 0, 1, 0, 0, 0, 0 ].
在您引用的代码中,y
是单热格式。网络的输出 score
是一个向量,给出每个 class 的概率。这也不是必需的格式。你需要做类似
true_class = tf.argmax( y, 1 )
predicted_class = tf.argmax( score, 1 )
并使用像
这样的混淆矩阵tf.confusion_matrix( true_class, predicted_class, num_classes )
(基本上,如果您查看 finetune.py 的第 123 行,其中包含用于确定准确性的两个元素,但它们未保存在单独的张量中。)
如果您想保留所有批次的混淆矩阵总数 运行,您只需将它们相加 - 因为矩阵的每个单元格都会计算属于该类别的示例数量,一个元素-wise 加法为整个集合创建混淆矩阵:
cm_running_total = None
cm_nupmy_array = sess.run(tf.confusion_matrix(true_class, predicted_class, num_classes), feed_dict={x: img_batch, y: label_batch, keep_prob: 1.} )
if cm_running_total is None:
cm_running_total = cm_numpy_array
else:
cm_running_total += cm_numpy_array