cifar10_eval.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Evaluation for CIFAR-10.
  16. Accuracy:
  17. cifar10_train.py achieves 83.0% accuracy after 100K steps (256 epochs
  18. of data) as judged by cifar10_eval.py.
  19. Speed:
  20. On a single Tesla K40, cifar10_train.py processes a single batch of 128 images
  21. in 0.25-0.35 sec (i.e. 350 - 600 images /sec). The model reaches ~86%
  22. accuracy after 100K steps in 8 hours of training time.
  23. Usage:
  24. Please see the tutorial and website for how to download the CIFAR-10
  25. data set, compile the program and train the model.
  26. http://tensorflow.org/tutorials/deep_cnn/
  27. """
  28. from __future__ import absolute_import
  29. from __future__ import division
  30. from __future__ import print_function
  31. from datetime import datetime
  32. import math
  33. import time
  34. import numpy as np
  35. import tensorflow as tf
  36. import cifar10
  37. FLAGS = tf.app.flags.FLAGS
  38. tf.app.flags.DEFINE_string('eval_dir', '/tmp/cifar10_eval',
  39. """Directory where to write event logs.""")
  40. tf.app.flags.DEFINE_string('eval_data', 'test',
  41. """Either 'test' or 'train_eval'.""")
  42. tf.app.flags.DEFINE_string('checkpoint_dir', '/tmp/cifar10_train',
  43. """Directory where to read model checkpoints.""")
  44. tf.app.flags.DEFINE_integer('eval_interval_secs', 60 * 5,
  45. """How often to run the eval.""")
  46. tf.app.flags.DEFINE_integer('num_examples', 10000,
  47. """Number of examples to run.""")
  48. tf.app.flags.DEFINE_boolean('run_once', False,
  49. """Whether to run eval only once.""")
  50. def eval_once(saver, summary_writer, top_k_op, summary_op):
  51. """Run Eval once.
  52. Args:
  53. saver: Saver.
  54. summary_writer: Summary writer.
  55. top_k_op: Top K op.
  56. summary_op: Summary op.
  57. """
  58. with tf.Session() as sess:
  59. ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
  60. if ckpt and ckpt.model_checkpoint_path:
  61. # Restores from checkpoint
  62. saver.restore(sess, ckpt.model_checkpoint_path)
  63. # Assuming model_checkpoint_path looks something like:
  64. # /my-favorite-path/cifar10_train/model.ckpt-0,
  65. # extract global_step from it.
  66. global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
  67. else:
  68. print('No checkpoint file found')
  69. return
  70. # Start the queue runners.
  71. coord = tf.train.Coordinator()
  72. try:
  73. threads = []
  74. for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
  75. threads.extend(qr.create_threads(sess, coord=coord, daemon=True,
  76. start=True))
  77. num_iter = int(math.ceil(FLAGS.num_examples / FLAGS.batch_size))
  78. true_count = 0 # Counts the number of correct predictions.
  79. total_sample_count = num_iter * FLAGS.batch_size
  80. step = 0
  81. while step < num_iter and not coord.should_stop():
  82. predictions = sess.run([top_k_op])
  83. true_count += np.sum(predictions)
  84. step += 1
  85. # Compute precision @ 1.
  86. precision = true_count / total_sample_count
  87. print('%s: precision @ 1 = %.3f' % (datetime.now(), precision))
  88. summary = tf.Summary()
  89. summary.ParseFromString(sess.run(summary_op))
  90. summary.value.add(tag='Precision @ 1', simple_value=precision)
  91. summary_writer.add_summary(summary, global_step)
  92. except Exception as e: # pylint: disable=broad-except
  93. coord.request_stop(e)
  94. coord.request_stop()
  95. coord.join(threads, stop_grace_period_secs=10)
  96. def evaluate():
  97. """Eval CIFAR-10 for a number of steps."""
  98. with tf.Graph().as_default() as g:
  99. # Get images and labels for CIFAR-10.
  100. eval_data = FLAGS.eval_data == 'test'
  101. images, labels = cifar10.inputs(eval_data=eval_data)
  102. # Build a Graph that computes the logits predictions from the
  103. # inference model.
  104. logits = cifar10.inference(images)
  105. # Calculate predictions.
  106. top_k_op = tf.nn.in_top_k(logits, labels, 1)
  107. # Restore the moving average version of the learned variables for eval.
  108. variable_averages = tf.train.ExponentialMovingAverage(
  109. cifar10.MOVING_AVERAGE_DECAY)
  110. variables_to_restore = variable_averages.variables_to_restore()
  111. saver = tf.train.Saver(variables_to_restore)
  112. # Build the summary operation based on the TF collection of Summaries.
  113. summary_op = tf.summary.merge_all()
  114. summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g)
  115. while True:
  116. eval_once(saver, summary_writer, top_k_op, summary_op)
  117. if FLAGS.run_once:
  118. break
  119. time.sleep(FLAGS.eval_interval_secs)
  120. def main(argv=None): # pylint: disable=unused-argument
  121. cifar10.maybe_download_and_extract()
  122. if tf.gfile.Exists(FLAGS.eval_dir):
  123. tf.gfile.DeleteRecursively(FLAGS.eval_dir)
  124. tf.gfile.MakeDirs(FLAGS.eval_dir)
  125. evaluate()
  126. if __name__ == '__main__':
  127. tf.app.run()