|
| 1 | +"""Evaluation of the Model. |
| 2 | +
|
| 3 | +
|
| 4 | +""" |
| 5 | +from __future__ import absolute_import |
| 6 | +from __future__ import division |
| 7 | +from __future__ import print_function |
| 8 | + |
| 9 | +from datetime import datetime |
| 10 | +import math |
| 11 | +import time |
| 12 | + |
| 13 | +import tensorflow.python.platform |
| 14 | +from tensorflow.python.platform import gfile |
| 15 | +import numpy as np |
| 16 | +import tensorflow as tf |
| 17 | +import os |
| 18 | + |
| 19 | +import utils |
| 20 | +import logging |
| 21 | +import sys |
| 22 | +import imp |
| 23 | + |
| 24 | + |
| 25 | +logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', |
| 26 | + level=logging.DEBUG, |
| 27 | + stream=sys.stdout) |
| 28 | + |
| 29 | + |
| 30 | +FLAGS = tf.app.flags.FLAGS |
| 31 | +tf.app.flags.DEFINE_string('eval_data', 'test', |
| 32 | + """Either 'test' or 'train_eval'.""") |
| 33 | + |
| 34 | + |
| 35 | +# TODO: Iterate over all possible Values |
| 36 | +# Write Values to Tensorboard |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +def evaluate(train_dir): |
| 41 | + """Loads the model and runs evaluation |
| 42 | + """ |
| 43 | + |
| 44 | + target_dir = os.path.join(train_dir, "model_files") |
| 45 | + params = imp.load_source("params", os.path.join(target_dir, "params.py")) |
| 46 | + data_input = imp.load_source("input", os.path.join(target_dir, "input.py")) |
| 47 | + network = imp.load_source("network", os.path.join(target_dir, "network.py")) |
| 48 | + |
| 49 | + with tf.Graph().as_default(): |
| 50 | + |
| 51 | + # Retrieve images and labels |
| 52 | + eval_data = FLAGS.eval_data == 'test' |
| 53 | + images, labels = data_input.inputs(eval_data=eval_data, data_dir=utils.cfg.data_dir, |
| 54 | + batch_size=params.batch_size) |
| 55 | + |
| 56 | + # Generate placeholders for the images and labels. |
| 57 | + keep_prob = utils.placeholder_inputs(params.batch_size) |
| 58 | + |
| 59 | + # Build a Graph that computes predictions from the inference model. |
| 60 | + logits = network.inference(images, keep_prob) |
| 61 | + |
| 62 | + # Add to the Graph the Ops for loss calculation. |
| 63 | + loss = network.loss(logits, labels) |
| 64 | + |
| 65 | + # Calculate predictions. |
| 66 | + top_k_op = tf.nn.in_top_k(logits, labels, 1) |
| 67 | + |
| 68 | + # Add the Op to compare the logits to the labels during evaluation. |
| 69 | + eval_correct = network.evaluation(logits, labels) |
| 70 | + |
| 71 | + # Build the summary operation based on the TF collection of Summaries. |
| 72 | + summary_op = tf.merge_all_summaries() |
| 73 | + |
| 74 | + # Create a saver for writing training checkpoints. |
| 75 | + saver = tf.train.Saver() |
| 76 | + |
| 77 | + # Create a session for running Ops on the Graph. |
| 78 | + sess = tf.Session() |
| 79 | + |
| 80 | + # Run the Op to initialize the variables. |
| 81 | + init = tf.initialize_all_variables() |
| 82 | + sess.run(init) |
| 83 | + |
| 84 | + # Start the queue runners. |
| 85 | + tf.train.start_queue_runners(sess=sess) |
| 86 | + |
| 87 | + ckpt = tf.train.get_checkpoint_state(train_dir) |
| 88 | + if ckpt and ckpt.model_checkpoint_path: |
| 89 | + saver.restore(sess, ckpt.model_checkpoint_path) |
| 90 | + else: |
| 91 | + print("No checkpoints found! ") |
| 92 | + exit(1) |
| 93 | + |
| 94 | + print("Doing Evaluation with lots of data") |
| 95 | + utils.do_eval(sess=sess, |
| 96 | + eval_correct=eval_correct, |
| 97 | + keep_prob=keep_prob, |
| 98 | + num_examples=params.num_examples_per_epoch_for_eval, |
| 99 | + params=params, |
| 100 | + name="eval") |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +def main(_): |
| 106 | + |
| 107 | + train_dir = utils.get_train_dir() |
| 108 | + evaluate(train_dir) |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + tf.app.run() |
0 commit comments