def print_test_accuracy(show_example_errors=False, show_confusion_matrix=False): # For all the images in the test-set, # calculate the predicted classes and whether they are correct. correct, cls_pred = predict_cls_test() #---------------------------------------- # save cls_pred and cls_true save_cls_pred(images_name[:-4], time_stamp, cls_pred) save_cls_true(images_name[:-4], time_stamp, data.test.cls) #---------------------------------------- # Classification accuracy and the number of correct classifications. acc, num_correct = cls_accuracy(correct) # Number of images being classified. num_images = len(correct) # Print the accuracy. msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})" print(msg.format(acc, num_correct, num_images)) # Plot the confusion matrix, if desired. if show_confusion_matrix: print("Confusion Matrix:") plot_confusion_matrix(cls_pred=cls_pred) # Plot some examples of mis-classifications, if desired. if show_example_errors: print("Example errors:") plot_example_errors(cls_pred=cls_pred, correct=correct)
correct_prediction = tf.equal(y_pred_cls, y_true_cls) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #----------------------------------- # Saver saver = tf.train.Saver() print ("AI:{0}".format(AI_saved_dir)) if not os.path.exists(AI_saved_dir): print ("No AI can be restore, please check folder ./checkpoints") save_path = os.path.join(AI_saved_dir, 'best_validation') #----------------------------------- # Tensorflow run session = tf.Session() # restore previous weight saver.restore(sess=session, save_path=save_path) batch_size = 512 print ("batch_size = {0}".format(batch_size)) # test the restored AI, show confusion matrix and example_errors # and save the cls of prediction print_test_accuracy(show_confusion_matrix=True) # save labels of prediction label_pred = predict_label(data.test.images, data.test.labels) save_label_pred(images_name[:-4], directory, label_pred) # save cls_pred and cls_true save_cls_pred(images_name[:-4], directory, cls_pred) save_cls_true(images_name[:-4], directory, data.test.cls) session.close() #----------------------------------- # measuring time elapsed_time = time.time() - start_time print ("Exiting Main Program, spending ", elapsed_time, "seconds.")