def evaluateWithMcnemar(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = tfFLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. #logits = cifar10.inference(images) images, labels = cifar10.inputs(tfFLAGS.eval_data) logits1, w1, b1, w2, b2 = MyModel.inference(images) logits2, w1, b1, w2, b2 = MyModel2.inference(images) # Calculate predictions. top_k_op1 = tf.nn.in_top_k(logits1, labels, 1) top_k_op2 = tf.nn.in_top_k(logits2, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage(tfFLAGS.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(tfFLAGS.eval_dir, g) while True: eval_once2(saver, summary_writer, top_k_op1, summary_op, top_k_op2, labels) if tfFLAGS.run_once: break time.sleep(tfFLAGS.eval_interval_secs)
def evaluate_all_threshold(epsilon): thresh = 0.5 with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. validation_data = FLAGS.eval_data == "not test" valid_images, labels = cifar10.inputs(eval_data=validation_data) valid_scores = cifar10.inference(valid_images) variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) thresh = find_all_threshold(saver, summary_writer, summary_op, valid_scores, labels, epsilon) #thresh = 0.5 with tf.Graph().as_default() as g: eval_data = FLAGS.eval_data == "test" images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images=images) variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) # Calculate predictions. #top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) #while : #eval_once(saver, summary_writer, top_k_op, summary_op) recall, precision = conformal_direct_eval(saver, summary_writer, summary_op, logits, labels, thresh) return recall, precision
def main(_): with tf.Graph().as_default() as g: with tf.device("/cpu:0"): images_eval_train, _ = inputs(batch_size=FLAGS.finetune_batch_size, validation=FLAGS.validation, shuffle=True) images_eval_test, labels_eval_test = inputs( batch_size=FLAGS.eval_batch_size, train=False, validation=FLAGS.validation, shuffle=False, num_epochs=1) with tf.device(FLAGS.device): with tf.variable_scope("CNN") as scope: # Build graph of finetuning BN stats finetune_op = build_finetune_graph(images_eval_train) scope.reuse_variables() # Build eval graph n_correct, m = build_eval_graph(images_eval_test, labels_eval_test) init_op = tf.global_variables_initializer() saver = tf.train.Saver(tf.global_variables()) sess = tf.Session() sess.run(init_op) ckpt = tf.train.get_checkpoint_state(FLAGS.log_dir) print("Checkpoints:", ckpt) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) sess.run(tf.local_variables_initializer()) coord = tf.train.Coordinator() tf.train.start_queue_runners(sess=sess, coord=coord) print("Finetuning...") for _ in range(FLAGS.finetune_iter): sess.run(finetune_op) sum_correct_examples = 0 sum_m = 0 try: while not coord.should_stop(): _n_correct, _m = sess.run([n_correct, m]) sum_correct_examples += _n_correct sum_m += _m except tf.errors.OutOfRangeError: print('Done evaluation -- epoch limit reached') finally: # When done, ask the threads to stop. coord.request_stop() print( "Test: num_test_examples:{}, num_correct_examples:{}, accuracy:{}". format(sum_m, sum_correct_examples, sum_correct_examples / float(sum_m)))
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images, eval=True) #Pass eval=True so we can use tf.nn.softmax to get normalized logits # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """ Eval CIFAR-10 for a number of steps :return: """ with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10 eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # computes the logits predictions logits = cifar10.inference(images) # Calculate predictions top_k_op = tf.nn.in_top_k(logits, labels, 1) # variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) # Build the summary operation based on the TF collection of Summaries summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) # 每隔指定时间评估下精度 while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, _ = cifar10.inputs(eval_data=eval_data) logits = cifar10.inference(images) prediction = tf.nn.softmax(logits) variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: # Restores from checkpoint saver.restore(sess, ckpt.model_checkpoint_path) else: print('No checkpoint file found') return # Build a Graph that computes the logits predictions from the # inference model. result = tf.argmax(prediction, 1) tf.print(result) print('DONE')
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default(): # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.contrib.deprecated.merge_all_summaries() graph_def = tf.get_default_graph().as_graph_def() #summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir, # graph_def=graph_def) eval_once(saver, top_k_op, summary_op, labels)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) if os.path.isfile('log_cifar_eval.csv'): os.remove('log_cifar_eval.csv') df = pandas.DataFrame([], columns=['time', 'precision']) df.to_csv('log_cifar_eval.csv', index=False) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. with tf.variable_scope('partitioned_space'): logits = cifarnet.cifarnet(images, num_classes=10, is_training=False) logits = tf.nn.softmax(logits) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. saver = tf.train.Saver() # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) tf.logging.info('continue') if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def train(): """Train CIFAR-10 for a number of steps.""" with tf.Graph().as_default(): global_step = tf.Variable(0, trainable=False) # Get images and labels for CIFAR-10. images, labels = cifar10.distorted_inputs() testImg, testlabels = cifar10.inputs(eval_data=True) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) test_pre = cifar10.inference(testImg,test=True) # Calculate loss. loss = cifar10.loss(logits, labels) # Build a Graph that trains the model with one batch of examples and # updates the model parameters. train_op = cifar10.train(loss, global_step) # Create a saver. saver = tf.train.Saver(tf.all_variables()) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() # Build an initialization operation to run below. init = tf.initialize_all_variables() # Start running operations on the Graph. sess = tf.Session(config=tf.ConfigProto( log_device_placement=FLAGS.log_device_placement)) sess.run(init) # Start the queue runners. tf.train.start_queue_runners(sess=sess) summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph) for step in xrange(FLAGS.max_steps): start_time = time.time() _, loss_value = sess.run([train_op, loss]) duration = time.time() - start_time if step % 10 == 0: print ('loss '+str(loss_value)) if step % 100 == 0: summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step) # Save the model checkpoint periodically. if step % 10 == 0 or (step + 1) == FLAGS.max_steps: checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) #eval if step%10==0: cifar10.accuracy(test_pre,testlabels)
def train(): """Train chess-cnn for a number of steps.""" with tf.Graph().as_default(): global_step = tf.train.get_or_create_global_step() # Get images and labels for CIFAR-10. # Force input pipeline to CPU:0 to avoid operations sometimes ending up on # GPU and resulting in a slow down. with tf.device('/cpu:0'): train_data = FLAGS.which_data == 'train' images, labels = cifar10.inputs(which_data=train_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) # Calculate loss. loss = cifar10.loss(logits, labels) # Build a Graph that trains the model with one batch of examples and # updates the model parameters. train_op = cifar10.train(loss, global_step) class _LoggerHook(tf.train.SessionRunHook): """Logs loss and runtime.""" def begin(self): self._step = -1 self._start_time = time.time() def before_run(self, run_context): self._step += 1 return tf.train.SessionRunArgs(loss) # Asks for loss value. def after_run(self, run_context, run_values): if self._step % FLAGS.log_frequency == 0: current_time = time.time() duration = current_time - self._start_time self._start_time = current_time loss_value = run_values.results examples_per_sec = FLAGS.log_frequency * FLAGS.batch_size / duration sec_per_batch = float(duration / FLAGS.log_frequency) format_str = ( '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') print(format_str % (datetime.now(), self._step, loss_value, examples_per_sec, sec_per_batch)) with tf.train.MonitoredTrainingSession( checkpoint_dir=FLAGS.train_dir, hooks=[ tf.train.StopAtStepHook(last_step=FLAGS.max_steps), tf.train.NanTensorHook(loss), _LoggerHook() ], config=tf.ConfigProto(log_device_placement=FLAGS. log_device_placement)) as mon_sess: while not mon_sess.should_stop(): mon_sess.run(train_op)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default(): # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() graph_def = tf.get_default_graph().as_graph_def() summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir, graph_def=graph_def) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Evaluate CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build graph that computes the logits from the inference model logits = cifar10.inference(images) # Calculate predictions top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation summary_op = tf.merge_all_summaries() summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # 获取 CIFAR-10图片和标签 eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # 计算logits 预测,从 inference模型. logits = cifar10.inference(images) top_k_op = tf.nn.in_top_k(logits, labels, 1) # 存储在验证集上被学习的variables的滑动均值. variable_averages = tf.train.ExponentialMovingAverage(cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # 汇总所有的summary,并写到相应的文件 summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(args): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = args.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data, args=args) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images, args) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( args.moving_average_decay) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(args.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op, args) if args.run_once: break time.sleep(args.eval_interval_secs)
def evaluate(): with tf.Graph().as_default() as g: eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data) logits = cifar10.inference(images, r=low_ranks) # top_k_op = tf.nn.in_top_k(logits, labels, 1) predict = tf.equal(tf.argmax(logits, axis=1, output_type=tf.int32), labels) truth_num = tf.reduce_sum(tf.cast(predict, tf.int32)) # variable_averages = tf.train.ExponentialMovingAverage( # cifar10.MOVING_AVERAGE_DECAY) # variables_to_restore = variable_averages.variables_to_restore() # print(variables_to_restore) # saver = tf.train.Saver(variables_to_restore) saver = tf.train.Saver() summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) # while True: # eval_once(saver, summary_writer, truth_num, summary_op, logits) # if FLAGS.run_once: # break # time.sleep(FLAGS.eval_interval_secs) for i in range(eval_time): eval_once(saver, summary_writer, truth_num, summary_op, logits)
def evaluate(): """ Evaluate cifar10 for a number of steps """ with tf.Graph().as_default() as g: # get images and labels for cifar-10 eval_data = FLAGS.eval_data == "test" images, labels = cifar10.inputs(eval_data=eval_data) # build a tf.Graph that computes logits from inference model logits = cifar10.inference(images) # calculate prediction top_k_op = tf.nn.in_top_k(logits, labels, 1) # restore moving average version of the learned variables for eval variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # build the summary operation based on the tf collection of summaries summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # ccen: eval_data should be true to use train_data/test_batch.bin to evaluate eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data, custom_data_dir="./test_data") # Build a Graph that computes the logits predictions from the # inference model. # ccen: there're 128 classes instead of 10 logits = cifar10.inference(images, 128) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(eval_dir): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) phase = tf.Variable(False, name='is_train', dtype=bool, trainable=False) # Build a Graph that computes the logits predictions from the # inference model. if not FLAGS.vanilla: logits = cifar10.inference(images, phase, vd.conv2d) else: logits = cifar10.inference(images, phase, None) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. images, labels = cifar10.inputs(eval_data=True) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. saver = tf.train.Saver() # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. images, labels = cifar10.inputs(eval_data='test') # need modify for test logits, _ = cifar10.inference(images) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() del variables_to_restore['input_producer/limit_epochs/epochs'] saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() summary_writer = tf.train.SummaryWriter(FLAGS.test_dir, g) while True: eval_once_given_model(saver, summary_writer, logits, labels, summary_op, images) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def train(): with tf.Graph().as_default(): global_step = tf.Variable(0, trainable=False) #get images and labels for cifar-10 images, labels = cifar10.inputs() logits = cifar10.inference(images) loss = cifar10.loss(logits, labels) train_op = cifar10.train(loss, global_step) with tf.Session() as sess: init = tf.initialize_all_variables() sess.run(init) tf.train.start_queue_runners() for step in xrange(FLAGS.max_steps): _, loss_value = sess.run([train_op, loss]) if step % 10 == 0: print("Step %s: loss = %s" % (step, loss_value))
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Generate a batch of images and labels # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # k = 1, this outputs a batch_size bool array, an entry out[i] is true if the prediction for the target class is among the top k predictions among all predictions for example i # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() # Returns a map of names to Variables to restore. saver = tf.train.Saver(variables_to_restore) # The Saver class adds ops to save and restore variables to and from checkpoints # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() # Merges all summaries collected in the default graph. summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) # create an event file in a given directory and add summaries and events to it while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs) # Suspend execution of the calling thread for the given number of seconds
def train(): """Train CIFAR-10 for a number of steps.""" with tf.Graph().as_default(): # with tf.variable_scope("cifar10", reuse=tf.AUTO_REUSE) as scope: global_step = tf.train.get_or_create_global_step() # Get images and labels for CIFAR-10. train_flag = tf.placeholder(tf.bool, shape = ()) trX, trY = cifar10.distorted_inputs() teX, teY = cifar10.inputs(eval_data = True) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(trX) # Calculate accuracy tr_acc = cifar10.accuracy(logits, trY)[1] print(tr_acc, "tr_acc\n") # tr_acc_sum = tf.summary.scalar('train/accuracy', tr_acc) # Calculate loss. loss = cifar10.loss(logits, trY) # Build a Graph that trains the model with one batch of examples and # updates the model parameters. train_op = cifar10.train(loss, global_step) tf.get_variable_scope().reuse_variables() eval_logits = cifar10.inference(teX) te_acc = cifar10.accuracy(eval_logits, teY)[1] print(te_acc, "te_acc\n") # te_acc_sum = tf.summary.scalar('test/accuracy', te_acc) accuracy = tf.cond(train_flag, lambda: tr_acc, lambda: te_acc) tf.summary.scalar("accuracy", accuracy) merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter('tmp/cifar10/train') test_writer = tf.summary.FileWriter('tmp/cifar10/test') print("Training Starts") # Configs config = tf.ConfigProto(log_device_placement=FLAGS.log_device_placement) config.gpu_options.allow_growth=True mon_sess = tf.train.MonitoredTrainingSession( hooks=[tf.train.StopAtStepHook(last_step=FLAGS.max_steps), tf.train.NanTensorHook(loss)],config=config) step = -1 while not mon_sess.should_stop(): step += 1 _,loss_value = mon_sess.run([train_op,loss]) if step % FLAGS.log_frequency == 0: tr_acc,summary = mon_sess.run([accuracy,merged], feed_dict = {train_flag : True}) train_writer.add_summary(summary, step) te_acc, summary = mon_sess.run([accuracy, merged], feed_dict = {train_flag : False}) test_writer.add_summary(summary, step) format_str = ('%s: step %d, loss = %.2f, test accuracy = %.2f, train accuracy = %.2f') print (format_str % (datetime.now(), step, loss_value, te_acc, tr_acc))
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' cifar_images, cifar_labels = cifar10.inputs(eval_data=eval_data) mnist_images, mnist_labels = cifar10.mnist_inputs("test") # Build a Graph that computes the logits predictions from the # inference model. # logits = cifar10.inference(images) #logits, mnist_logits = cifar10.inference(mnist_images) with tf.variable_scope('shared_net') as scope: cifar_local4 = cifar10.inference_shared(cifar_images) scope.reuse_variables() mnist_local4 = cifar10.inference_shared(mnist_images) mnist_logits = cifar10.inference_mnist(mnist_local4) cifar_logits = cifar10.inference_cifar(cifar_local4) #mnist_labels = tf.Print(mnist_labels, [mnist_labels],'*.*.*.* MNIST labels:') # cifar_labels = tf.Print(cifar_labels, [cifar_labels],'*.*.*.* CIFAR labels:') # Calculate predictions. cifar_top_k_op = tf.nn.in_top_k(cifar_logits, cifar_labels, 1) mnist_top_k_op = tf.nn.in_top_k(mnist_logits, tf.cast(mnist_labels,dtype=tf.int32), 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) itercount=0 max_eval_iters = cifar10_train.FLAGS.max_steps while True: gs,nt = eval_once(saver, summary_writer, cifar_top_k_op, mnist_top_k_op, summary_op,itercount) itercount+=1 if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs) # must be set according to training if nt: max_eval_iters = 101 # 101 % of noise is added if noise test is applied print('MAX_EVAL_ITERS :',max_eval_iters) if int(gs)>=max_eval_iters: print('F I N I S H E D ') break
def evaluate(): """Eval CIFAR-10 for a number of steps.""" dt = Transformer.Transformer() dt.checkpoint_version = FLAGS.checkpoint_version dt.trainable = False #with dt.graph.as_default() as g: with tf.Graph().as_default() as g: if FLAGS.is_compressed: dt.load_flow('/tmp/cifar10_train_' + str(dt.checkpoint_version - 1) + '/pruned_flow_graph.pkl') # prefetch flow graph dt.flow = dt.cached_flow else: dt.flow = NeuralFlow() with g.device('/cpu:0'): # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(dt, images, is_training=False, is_compressed=FLAGS.is_compressed) total_params, _ = slim.model_analyzer.analyze_vars( slim.get_model_variables()) print('total params: %d' % total_params) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Calculate loss. loss_op = cifar10.loss(dt, logits, labels) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() variables_to_restore_simple = tf.global_variables() if FLAGS.is_compressed: saver = tf.train.Saver(variables_to_restore_simple) else: saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) while True: eval_once(dt, saver, summary_writer, top_k_op, loss_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # # inference model. 推理模型 logits = cifar10.inference(images) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) ### # tf.nn.in_top_k组要是用于计算预测的结果和实际结果的是否相等,返回一个bool类型的张量, # tf.nn.in_top_k(prediction, target, K): # prediction就是表示你预测的结果,大小就是预测样本的数量乘以输出的维度,类型是tf.float32 # 等。target就是实际样本类别的标签,大小就是样本数量的个数。K表示每个样本的预测结果的前K # 个最大的数里面是否含有target中的值。一般都是取1。 # # import tensorflow as tf; # A = [[0.8,0.6,0.3], [0.1,0.6,0.4]] # B = [1, 1] # out = tf.nn.in_top_k(A, B, 1) # with tf.Session() as sess: # sess.run(tf.initialize_all_variables()) # print sess.run(out) # # Running Result:[False, True] ! 注意0起址index ### # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) ### # tf.train.ExponentialMovingAverage这个函数用于更新参数,就是采用滑动平均的方法更新参数。 # 这个函数初始化需要提供一个衰减速率(decay),用于控制模型的更新速度。这个函数还会维护一个 # 影子变量(也就是更新参数后的参数值),这个影子变量的初始值就是这个变量的初始值,影子变量 # 值的更新方式如下: # shadow_variable = decay * shadow_variable + (1-decay) * variable # shadow_variable是影子变量,variable表示待更新的变量,也就是变量被赋予的值,decay为衰减速率。 # decay一般设为接近于1的数(0.99,0.999)。decay越大模型越稳定,因为decay越大,参数更新的速度 # 就越慢,趋于稳定。 ### variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break # # 休眠一顿时间后再评估 time.sleep(FLAGS.eval_interval_secs)
def main(): # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) with tf.Session() as sess: # Build a Graph that computes the logits predictions from the # inference model. probabilities = tf.nn.softmax(cifar10.inference(images)) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: # Restores from checkpoint saver.restore(sess, ckpt.model_checkpoint_path) else: print('No checkpoint file found') return # Start the queue runners. coord = tf.train.Coordinator() try: threads = [] for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS): threads.extend( qr.create_threads(sess, coord=coord, daemon=True, start=True)) num_iter = int(math.ceil(FLAGS.num_examples / FLAGS.batch_size)) submission = [] true_labels = [] step = 0 while step < num_iter and not coord.should_stop(): submission_batch, true_labels_batch = sess.run( [probabilities, labels]) submission.append(submission_batch) true_labels.append(true_labels_batch) step += 1 submission = np.vstack(submission) true_labels = np.concatenate(true_labels) except Exception as e: # pylint: disable=broad-except coord.request_stop(e) coord.request_stop() coord.join(threads, stop_grace_period_secs=10) return submission, true_labels
def estimation_T(): """Estimation T based on a pretrained model""" with tf.Graph().as_default(): images, labels = cifar10.inputs( eval_data=False) # on the training data logits = cifar10.inference(images) pred = tf.nn.softmax(logits) variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(FLAGS.init_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: print('No checkpoint file found') return # start the queue runner coord = tf.train.Coordinator() try: threads = [] for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS): threads.extend( qr.create_threads(sess, coord=coord, daemon=True, start=True)) num_iter = int( math.ceil(cifar10.NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN / FLAGS.batch_size)) step = 0 preds = [] while step < num_iter: #print('step: ', step) res = sess.run(pred) preds.append(res) step += 1 except Exception as e: coord.request_stop(e) coord.request_stop() coord.join(threads, stop_grace_period_secs=10) preds = np.concatenate(preds, axis=0) #print(preds.shape) indices = np.argmax(preds, axis=0) #print(indices) est_T = np.array(np.take(preds, indices, axis=0)) return est_T
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default(): # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. if FLAGS.teacher: with tf.variable_scope('model') as m_scope: logits = cifar10.inference(images) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) if FLAGS.student: with tf.variable_scope('student') as s_scope: st_logits = cifar10.inference(images) st_top_k_op = tf.nn.in_top_k(st_logits, labels, 1) with tf.variable_scope('med') as m_scope: md_logits = cifar10.inference_vars(images, 48, 48, 192, 96) md_top_k_op = tf.nn.in_top_k(md_logits, labels, 1) with tf.variable_scope('small') as small: sm_logits = cifar10.inference_vars(images, 32, 32, 96, 48) sm_top_k_op = tf.nn.in_top_k(sm_logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() variables_to_restore.pop("input_producer/limit_epochs/epochs", None) saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() graph_def = tf.get_default_graph().as_graph_def() summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir, graph_def=graph_def) while True: if FLAGS.teacher: print('MODEL:') eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.student: print('STUDENT:') eval_once(saver, summary_writer, st_top_k_op, summary_op) print('MEDIUM:') eval_once(saver, summary_writer, md_top_k_op, summary_op) print('SMALL:') eval_once(saver, summary_writer, sm_top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) logits = cifar10.inference(images) top_k_op = tf.nn.in_top_k(logits, labels, 1) loss = cifar10.loss(logits, labels) saver = tf.train.Saver(tf.all_variables()) while True: eval_once(saver, top_k_op, loss) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def read_images(): D = inputs(batch_size=FLAGS.ul_batch_size) inp_D = dict() inp_init = dict() inp_next = dict() inp_D["AE_train"] = D["train"].shuffle(buffer_size=1000).repeat().batch(FLAGS.ul_batch_size) inp_D["AE_eval_train"] = D["train"].shuffle(buffer_size=1000).take(NUM_EVAL_EXAMPLES).batch(FLAGS.eval_batch_size) inp_D["AE_eval_test"] = D["test"].shuffle(buffer_size=1000).take(NUM_EVAL_EXAMPLES).batch(FLAGS.eval_batch_size) inp_D["AE_emb"] = D["train"].batch(FLAGS.eval_batch_size) for k in inp_D.keys(): inp_init[k] = inp_D[k].make_initializable_iterator() inp_next[k] = inp_init[k].get_next() return inp_D, inp_init, inp_next
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs( eval_data=eval_data) # input 받아오기 -> batch 크기로 #tf.summary.image('images', images, max_outputs=30) # Build a Graph that computes the logits predictions from the # inference model. # logtis, top_k_op 등은 아직 세션이 시작되지 않았기 때문에 연산이 되기 전이다. logits = cifar10.inference(images) # 평가 받을 데이터로 만든것. batch 크기 기준, # Calculate predictions. # 우리는 예측이 실제 라벨과 정확히 일치할 경우만 올바르다고 기록하기 위해 K의 값(3번째 인자)을 1로 두었습니다. top_k_op = tf.nn.in_top_k(logits, labels, 1) # 평가 받을 데이터로 만든 것. batch크기 기준 # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. #summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) eval_count = 0 while True: eval_once( saver, summary_writer, logits, labels, top_k_op, #summary_op, images, eval_count) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs) if eval_count == 0: break eval_count += 1
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) # predictions = { # # Generate predictions (for PREDICT and EVAL mode) # "classes": tf.argmax(input=logits, axis=1), # # Add `softmax_tensor` to the graph. It is used for PREDICT and by the # # `logging_hook`. # "probabilities": tf.nn.softmax(logits, name="softmax_tensor") # } # # accuracy = tf.metrics.accuracy(labels=labels, # predictions=predictions["classes"], # name='acc_op') # metrics = {'accuracy': accuracy} # tf.summary.scalar('accuracy_my', accuracy[1]) #Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) while True: c = open('check').read() if c == '1': eval_once(saver, summary_writer, top_k_op, summary_op) checkflag = open('check', 'w+') checkflag.write('0') checkflag.close() if c == '2': eval_once(saver, summary_writer, top_k_op, summary_op) f = open('eval.txt', 'a') f.write('\n Average test accuracy: %s' % (np.mean(test_acc_list))) f.write('\n Max test accuracy: %s\n' % (np.amax(test_acc_list))) f.close() break if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == "test" print(eval_data) images, labels, ground_truth = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits, _ = cifar10.inference(images) print(logits) print(logits.get_shape()) print("after inference node creation") loss = cifar10.loss(logits, labels) accuracy, precision, accuracies = cifar10.accuracy(logits, ground_truth) labels = tf.cast(labels, tf.int64) label_shape = labels.get_shape().as_list() reshaped_labels = tf.reshape(labels, [label_shape[0] * label_shape[1] * label_shape[2]]) logits_shape = logits.get_shape().as_list() reshaped_logits = tf.reshape(logits, [logits_shape[0] * logits_shape[1] * logits_shape[2], logits_shape[3]]) # Calculate predictions. # top_k_op = tf.nn.in_top_k(logits, labels, 1) # top_k_op = tf.nn.in_top_k(reshaped_logits, reshaped_labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage(cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir, g) while True: print("evaluate:") eval_once(saver, summary_writer, summary_op, accuracy, precision, accuracies) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): """Eval CIFAR-10 for a number of steps.""" img_flag = True global MOVING_AVGS with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. eval_data = FLAGS.eval_data == 'test' if img_flag: images, labels = mod_inputs(eval_data=eval_data, img_flag=img_flag) else: images, labels = cifar10.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = cifar10.inference(images) # Calculate predictions. # Restore the moving average version of the learned variables for evaluation if MOVING_AVGS: variable_averages = tf.train.ExponentialMovingAverage( cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) else: saver = tf.train.Saver() # Build the summary operation based on the TF collection of Summaries. summary_op = tf.merge_all_summaries() summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, summary_op, img_flag, logits, labels, labels) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def evaluate(): with tf.Graph().as_default(): eval_data = FLAGS.eval_data == 'test' images, labels = cifar10.inputs(eval_data=eval_data) logits = cifar10.inference(images) top_k_op = tf.nn.in_top_k(logits, labels, 1) variable_averages = tf.train.ExponentialMovingAverage(cifar10.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) summary_op = tf.merge_all_summaries() graph_def = tf.get_default_graph().as_graph_def() summary_writer = tf.train.SummaryWriter(FLAGS.eval_dir, graph_def=graph_def) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def main(_): model_dir = get_model_dir(conf, ['data_dir', 'sample_dir', 'max_epoch', 'test_step', 'save_step', 'is_train', 'random_seed', 'log_level', 'display']) preprocess_conf(conf) DATA_DIR = os.path.join(conf.data_dir, conf.data) SAMPLE_DIR = os.path.join(conf.sample_dir, conf.data, model_dir) check_and_create_dir(DATA_DIR) check_and_create_dir(SAMPLE_DIR) # 0. prepare datasets if conf.data == "mnist": from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets(DATA_DIR, one_hot=True) next_train_batch = lambda x: mnist.train.next_batch(x)[0] next_test_batch = lambda x: mnist.test.next_batch(x)[0] height, width, channel = 28, 28, 1 train_step_per_epoch = mnist.train.num_examples / conf.batch_size test_step_per_epoch = mnist.test.num_examples / conf.batch_size elif conf.data == "cifar": from cifar10 import IMAGE_SIZE, inputs maybe_download_and_extract(DATA_DIR) images, labels = inputs(eval_data=False, data_dir=os.path.join(DATA_DIR, 'cifar-10-batches-bin'), batch_size=conf.batch_size) height, width, channel = IMAGE_SIZE, IMAGE_SIZE, 3 with tf.Session() as sess: network = Network(sess, conf, height, width, channel) stat = Statistic(sess, conf.data, model_dir, tf.trainable_variables(), conf.test_step) stat.load_model() if conf.is_train: logger.info("Training starts!") initial_step = stat.get_t() if stat else 0 iterator = trange(conf.max_epoch, ncols=70, initial=initial_step) for epoch in iterator: # 1. train total_train_costs = [] for idx in xrange(train_step_per_epoch): images = binarize(next_train_batch(conf.batch_size)) \ .reshape([conf.batch_size, height, width, channel]) cost = network.test(images, with_update=True) total_train_costs.append(cost) # 2. test total_test_costs = [] for idx in xrange(test_step_per_epoch): images = binarize(next_test_batch(conf.batch_size)) \ .reshape([conf.batch_size, height, width, channel]) cost = network.test(images, with_update=False) total_test_costs.append(cost) avg_train_cost, avg_test_cost = np.mean(total_train_costs), np.mean(total_test_costs) stat.on_step(avg_train_cost, avg_test_cost) # 3. generate samples samples = network.generate() save_images(samples, height, width, 10, 10, directory=SAMPLE_DIR, prefix="epoch_%s" % epoch) iterator.set_description("train l: %.3f, test l: %.3f" % (avg_train_cost, avg_test_cost)) print else: logger.info("Image generation starts!") samples = network.generate() save_images(samples, height, width, 10, 10, directory=SAMPLE_DIR)