def evaluate(): """Eval KANJI for a single example""" with tf.Graph().as_default() as g: # Get images and labels for KANJI. eval_data = FLAGS.eval_data == 'test' images, labels = kanji.single_input(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = kanji.inference(images) # Calculate predictions. top_k_op = tf.nn.top_k(logits, k=5) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( kanji.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) while True: eval_once(saver, top_k_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def tower_loss(scope): """Calculate the total loss on a single tower running the CIFAR model. Args: scope: unique prefix string identifying the CIFAR tower, e.g. 'tower_0' Returns: Tensor of shape [] containing the total loss for a batch of data """ # Get images and labels for KANJI. images, labels = kanji.distorted_inputs() # Build inference Graph. logits = kanji.inference(images) # Build the portion of the Graph calculating the losses. Note that we will # assemble the total_loss using a custom function below. _ = kanji.loss(logits, labels) # Assemble all of the losses for the current tower only. losses = tf.get_collection('losses', scope) # Calculate the total loss for the current tower. total_loss = tf.add_n(losses, name='total_loss') # Attach a scalar summary to all individual losses and the total loss; do the # same for the averaged version of the losses. for l in losses + [total_loss]: # Remove 'tower_[0-9]/' from the name in case this is a multi-GPU training # session. This helps the clarity of presentation on tensorboard. loss_name = re.sub('%s_[0-9]*/' % kanji.TOWER_NAME, '', l.op.name) tf.summary.scalar(loss_name, l) return total_loss
def evaluate(): """Eval KANJI for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for KANJI. eval_data = FLAGS.eval_data == 'test' images, labels = kanji.inputs(eval_data=eval_data) # Build a Graph that computes the logits predictions from the # inference model. logits = kanji.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( kanji.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 generateGraph(): """Generate the graph and its graphDef, freeze it, optimize it and then save it""" with tf.Graph().as_default() as g: # Get images and labels for KANJI. eval_data = FLAGS.eval_data == 'test' images, labels = kanji.single_input(eval_data=eval_data) images2 = tf.reshape(images, [64, 64, 1]) images2 = tf.identity(images2, name='InputI') images2 = tf.image.per_image_standardization(images2) images2 = tf.reshape(images2, [1, 64, 64, 1]) # Build a Graph that computes the logits predictions from the # inference model. logits = kanji.inference(images2) final_tensor = tf.add(logits, 0, name="finalresult") generateFile()
def train(): """Train KANJI for a number of steps.""" with tf.Graph().as_default(): global_step = tf.Variable(0, trainable=False) # Get images and labels for kanjis. images, labels = kanji.distorted_inputs() # Build a Graph that computes the logits predictions from the # inference model. logits = kanji.inference(images) # Calculate loss. loss = kanji.loss(logits, labels) # Build a Graph that trains the model with one batch of examples and # updates the model parameters. train_op = kanji.train(loss, global_step) # Create a saver. saver = tf.train.Saver(tf.global_variables()) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() # Build an initialization operation to run below. init = tf.global_variables_initializer() # 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.summary.FileWriter(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 assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step % 10 == 0: num_examples_per_step = FLAGS.batch_size examples_per_sec = num_examples_per_step / duration sec_per_batch = float(duration) format_str = ( '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') print(format_str % (datetime.now(), step, loss_value, examples_per_sec, sec_per_batch)) if step % 100 == 0: summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step) # Save the model checkpoint periodically. if step % 1000 == 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)