簡體   English   中英

Tensorflow混淆矩陣用於再訓練示例中的驗證

[英]Tensorflow confusion matrix for validation in retrain example

我一直在使用github tensorflow hub上的keep示例,在嘗試添加以下兩件事時遇到了兩個問題:

  1. 基於最終測試結果的混淆矩陣
  2. 一種記錄測試集中每個評估時間的方法,將其添加到數組中

這是再培訓示例的鏈接

混淆矩陣

對於混淆矩陣,我將運行評估功能更改為以下內容

def run_final_eval(train_session, module_spec, class_count, image_lists,
               jpeg_data_tensor, decoded_image_tensor,
               resized_image_tensor, bottleneck_tensor):
#Runs a final evaluation on an eval graph using the test data set.

Args:


   train_session: Session for the train graph with the tensors below.
    module_spec: The hub.ModuleSpec for the image module being used.
    class_count: Number of classes
    image_lists: OrderedDict of training images for each label.
    jpeg_data_tensor: The layer to feed jpeg image data into.
    decoded_image_tensor: The output of decoding and resizing the image.
    resized_image_tensor: The input node of the recognition graph.
    bottleneck_tensor: The bottleneck output layer of the CNN graph.

  test_bottlenecks, test_ground_truth, test_filenames = (
      get_random_cached_bottlenecks(train_session, image_lists,
                                    FLAGS.test_batch_size,
                                    'testing', FLAGS.bottleneck_dir,
                                    FLAGS.image_dir, jpeg_data_tensor,
                                    decoded_image_tensor, resized_image_tensor,
                                    bottleneck_tensor, FLAGS.tfhub_module))

  (eval_session, _, bottleneck_input, ground_truth_input, evaluation_step,
   prediction) = build_eval_session(module_spec, class_count)
  test_accuracy, predictions = eval_session.run(
      [evaluation_step, prediction],
      feed_dict={
          bottleneck_input: test_bottlenecks,
          ground_truth_input: test_ground_truth
      })
  tf.logging.info('Final test accuracy = %.1f%% (N=%d)' %
                  (test_accuracy * 100, len(test_bottlenecks)))

  confusion = tf.confusion_matrix(labels=test_ground_truth, predictions=predictions,num_classes=class_count)
  print(confusion)

  if FLAGS.print_misclassified_test_images:
    tf.logging.info('=== MISCLASSIFIED TEST IMAGES ===')
    for i, test_filename in enumerate(test_filenames):
      if predictions[i] != test_ground_truth[i]:
        tf.logging.info('%70s  %s' % (test_filename,
                                      list(image_lists.keys())[predictions[i]]))

輸出為:

INFO:tensorflow:Final test accuracy = 88.5% (N=710)
INFO:tensorflow:=== CONwaka ===
Tensor("confusion_matrix/SparseTensorDenseAdd:0", shape=(5, 5), dtype=int32)

我也嘗試使用tf.logging.info獲得相同的結果。 我想以數組形式打印出來。 我找到了MLninja的《答案》,這似乎也是一個更好的解決方案,但我不知道如何在重新訓練文件中實現它。

任何幫助都非常感謝!

您需要評估混淆矩陣張量。 現在,您將混淆矩陣運算添加到圖形並打印該運算,但是您要打印的是矩陣運算結果。 在代碼中,它看起來像這樣:

confusion_matrix_np = eval_session.run(
  confusion,
  feed_dict={
      bottleneck_input: test_bottlenecks,
      ground_truth_input: test_ground_truth
  })

print(confusion_matrix_np)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM