1# Copyright 2017 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 17Accuracy: 18cifar10_train.py achieves 83.0% accuracy after 100K steps (256 epochs 19of data) as judged by cifar10_eval.py. 20 21Speed: 22On a single Tesla K40, cifar10_train.py processes a single batch of 128 images 23in 0.25-0.35 sec (i.e. 350 - 600 images /sec). The model reaches ~86% 24accuracy after 100K steps in 8 hours of training time. 25 26Usage: 27Please see the tutorial and website for how to download the CIFAR-10 28data set, compile the program and train the model. 29 30http://tensorflow.org/tutorials/deep_cnn/ 31""" 32from __future__ import absolute_import 33from __future__ import division 34from __future__ import print_function 35 36import argparse 37import datetime 38import math 39import sys 40import time 41 42import numpy as np 43import tensorflow as tf 44 45from tensorflow.contrib.model_pruning.examples.cifar10 import cifar10_pruning as cifar10 46 47FLAGS = None 48 49 50def eval_once(saver, summary_writer, top_k_op, summary_op): 51 """Run Eval once. 52 53 Args: 54 saver: Saver. 55 summary_writer: Summary writer. 56 top_k_op: Top K op. 57 summary_op: Summary op. 58 """ 59 with tf.Session() as sess: 60 ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) 61 if ckpt and ckpt.model_checkpoint_path: 62 # Restores from checkpoint 63 saver.restore(sess, ckpt.model_checkpoint_path) 64 # Assuming model_checkpoint_path looks something like: 65 # /my-favorite-path/cifar10_train/model.ckpt-0, 66 # extract global_step from it. 67 global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 68 else: 69 print('No checkpoint file found') 70 return 71 72 # Start the queue runners. 73 coord = tf.train.Coordinator() 74 try: 75 threads = [] 76 for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS): 77 threads.extend(qr.create_threads(sess, coord=coord, daemon=True, 78 start=True)) 79 80 num_iter = int(math.ceil(FLAGS.num_examples / 128)) 81 true_count = 0 # Counts the number of correct predictions. 82 total_sample_count = num_iter * 128 83 step = 0 84 while step < num_iter and not coord.should_stop(): 85 predictions = sess.run([top_k_op]) 86 true_count += np.sum(predictions) 87 step += 1 88 89 # Compute precision @ 1. 90 precision = true_count / total_sample_count 91 print('%s: precision @ 1 = %.3f' % (datetime.datetime.now(), precision)) 92 93 summary = tf.Summary() 94 summary.ParseFromString(sess.run(summary_op)) 95 summary.value.add(tag='Precision @ 1', simple_value=precision) 96 summary_writer.add_summary(summary, global_step) 97 except Exception as e: # pylint: disable=broad-except 98 coord.request_stop(e) 99 100 coord.request_stop() 101 coord.join(threads, stop_grace_period_secs=10) 102 103 104def evaluate(): 105 """Eval CIFAR-10 for a number of steps.""" 106 with tf.Graph().as_default() as g: 107 # Get images and labels for CIFAR-10. 108 eval_data = FLAGS.eval_data == 'test' 109 images, labels = cifar10.inputs(eval_data=eval_data) 110 111 # Build a Graph that computes the logits predictions from the 112 # inference model. 113 logits = cifar10.inference(images) 114 115 # Calculate predictions. 116 top_k_op = tf.nn.in_top_k(logits, labels, 1) 117 118 # Restore the moving average version of the learned variables for eval. 119 variable_averages = tf.train.ExponentialMovingAverage( 120 cifar10.MOVING_AVERAGE_DECAY) 121 variables_to_restore = variable_averages.variables_to_restore() 122 saver = tf.train.Saver(variables_to_restore) 123 124 # Build the summary operation based on the TF collection of Summaries. 125 summary_op = tf.summary.merge_all() 126 127 summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) 128 129 while True: 130 eval_once(saver, summary_writer, top_k_op, summary_op) 131 if FLAGS.run_once: 132 break 133 time.sleep(FLAGS.eval_interval_secs) 134 135 136def main(argv=None): # pylint: disable=unused-argument 137 cifar10.maybe_download_and_extract() 138 if tf.gfile.Exists(FLAGS.eval_dir): 139 tf.gfile.DeleteRecursively(FLAGS.eval_dir) 140 tf.gfile.MakeDirs(FLAGS.eval_dir) 141 evaluate() 142 143 144if __name__ == '__main__': 145 parser = argparse.ArgumentParser() 146 parser.add_argument( 147 '--eval_dir', 148 type=str, 149 default='/tmp/cifar10_eval', 150 help='Directory where to write event logs.') 151 parser.add_argument( 152 '--eval_data', 153 type=str, 154 default='test', 155 help="""Either 'test' or 'train_eval'.""") 156 parser.add_argument( 157 '--checkpoint_dir', 158 type=str, 159 default='/tmp/cifar10_train', 160 help="""Directory where to read model checkpoints.""") 161 parser.add_argument( 162 '--eval_interval_secs', 163 type=int, 164 default=60 * 5, 165 help='How often to run the eval.') 166 parser.add_argument( 167 '--num_examples', 168 type=int, 169 default=10000, 170 help='Number of examples to run.') 171 parser.add_argument( 172 '--run_once', 173 type=bool, 174 default=False, 175 help='Whether to run eval only once.') 176 177 FLAGS, unparsed = parser.parse_known_args() 178 tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) 179