I'm trying to run a tensorflow graph to train a model and periodically evaluate using a separate evaluation dataset. Both training and evaluation data is implemented using queue runners.
My current solution is to create both inputs in the same graph and use a tf.cond dependent on an is_training placeholder. My issue is highlighted by the following code:
import tensorflow as tf
from tensorflow.models.image.cifar10 import cifar10
from time import time
def get_train_inputs(is_training):
return cifar10.inputs(False)
def get_eval_inputs(is_training):
return cifar10.inputs(True)
def get_mixed_inputs(is_training):
train_inputs = get_train_inputs(None)
eval_inputs = get_eval_inputs(None)
return tf.cond(is_training, lambda: train_inputs, lambda: eval_inputs)
def time_inputs(inputs_fn, n_runs=10):
graph = tf.Graph()
with graph.as_default():
is_training = tf.placeholder(dtype=tf.bool, shape=(),
name='is_training')
images, labels = inputs_fn(is_training)
with tf.Session(graph=graph) as sess:
coordinator = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coordinator)
t = time()
for i in range(n_runs):
im, l = sess.run([images, labels], feed_dict={is_training: True})
dt = time() - t
coordinator.request_stop()
coordinator.join(threads)
return dt / n_runs
print('Train inputs: %.3f' % time_inputs(get_train_inputs))
print('Eval inputs: %.3f' % time_inputs(get_eval_inputs))
print('Mixed inputs: %.3f' % time_inputs(get_mixed_inputs))
I also had to comment out the image_summary line 133 of tensorflow/models/image/cifar10/cifar10_inputs.py.
This yielded the following results:
Train inputs: 0.055
Eval inputs: 0.050
Mixed inputs: 0.105
It would seem in the mixed case both inputs are being read/parsed, even though only 1 is used. Is there a way of avoiding this redundant computation? Or is there a nicer way of switching between training/evaluation data that still leverages the queue-runner setup?