TensorFlow issue with feed_dict not being noticed when using batching
<p>I have been trying to do the mnist tutorial with png files, and have gotten most things to the point where they make sense.</p>

<p>The gist of the code is <a href="https://gist.github.com/ThaHypnotoad/4438a6658c89bb803bd74080e78cf170" rel="nofollow">here</a> however I'm going to walk through what it does and where the issue is happening.</p>

<p>I have a function that generates filenames that I can give to the slice_input_producer.</p>

<pre><code>def gen_file_names_and_labels(rootDir):

"""goes through the directory structure and extracts images and labels from each image."""
file_names = []
labels = []
for file_name in glob.glob(rootDir+'/*/*'):

    file_type_removed = file_name.split('.')[0]
    split_by_dir = file_type_removed.split('/')
    file_names.append(file_name)
    labels.append(int(split_by_dir[2])) #getting the folder it's in, turning into an int, and using as label
return file_names, labels
</code></pre>

<p>This behaves as expected.</p>

<p>In the body I run this function for training and testing, and turning them into tensors, passing those tensors into a slice_input_producer</p>

<pre><code>sess = tf.InteractiveSession()

#THERE A PIPELINE FOR BOTH TESTING AND TRAINING. THEY COME IN PAIRS    
image_list_train,   label_list_train    = gen_file_names_and_labels('mnist_png/training')
image_list_test,    label_list_test     = gen_file_names_and_labels('mnist_png/testing')

images_train    = tf.convert_to_tensor(image_list_train,dtype=tf.string)    
images_test     = tf.convert_to_tensor(image_list_test,dtype=tf.string)    

#remember that these aren't the actual images, just file_names
labels_train    = tf.convert_to_tensor(label_list_train,dtype=tf.int32)
labels_test     = tf.convert_to_tensor(label_list_test,dtype=tf.int32)

input_queue_train   = tf.train.slice_input_producer([images_train   ,labels_train]  , shuffle=True)
input_queue_test    = tf.train.slice_input_producer([images_train   ,labels_train]  , shuffle=True)
</code></pre>

<p>this part also works correctly.</p>

<p>This is where things get strange.</p>

<pre><code>asdf = tf.placeholder(tf.int32)
input_queue = tf.cond( asdf&gt;0, lambda: input_queue_train, lambda: input_queue_test)
# input_queue = input_queue_test
image, label = read_images_from_disk(input_queue)
image_reshaped = tf.reshape( image, [28,28,1])
image_batch, label_batch = tf.train.batch([image_reshaped,label],batch_size=50)
</code></pre>

<p>The variable asdf was renamed in anger as it was the bearer of bad news.
See the plan here was to use different queues for training and testing.
I planned to feed_dict a single int that would work as an ad-hoc boolean for switching between the two.</p>

<pre><code>coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
sess.run(tf.initialize_all_variables())
print(label_batch.eval(feed_dict={asdf:0,keep_prob:1.0}))
for i in range(500):
    # batch = mnist.train.next_batch(50)

    if i%20 ==0:
        train_accuracy = accuracy.eval(feed_dict={keep_prob:1.0,asdf:0})
        print("step %d, training accuracy %g"%(i, train_accuracy))

    train_step.run(feed_dict={keep_prob:0.9,asdf:0})
</code></pre>

<p>When running it though, I get the error:
 "You must feed a value for placeholder tensor 'Placeholder' with dtype int32"
which is strange because <em>I am feeding it.</em></p>

<p>using the "print(foo.eval(feed_dict={asdf:0,keep_prob:1.0)) I was able to notice some interesting phenomena. It seems that the switching works fine when I evaluate the individual variables declared "image, label" that come out of "read_images_from_disk(input_queue)"</p>

<p>However if I try to evaluate the batching that comes right after, I get the aforementioned error.</p>

<p>What am I doing wrong with batching to make this happen? Is there a better way to do this switching between testing and training sets? What is the meaning of life the universe and everything? I'm counting on you StackOverflow. You're my only hope.</p>

<p>In answer to your question, "Is there a better way to do this switching between testing and training sets?", yes there is. <code>tf.cond()</code> evaluates both functions at each step (see <a href="https://www.tensorflow.org/versions/r0.10/api_docs/python/control_flow_ops.html#cond" rel="nofollow">here</a>) and therefore unnecessarily accesses both queues. <a href="http://stackoverflow.com/questions/39617686/tensorflow-data-input-toggle-train-validation/39620485#39620485">This SO discussion</a> and the associated links provide a couple of better alternatives:</p>

<ul>
<li>use <code>tf.placeholder_with_default()</code> for your test data</li>
<li>use <code>make_template</code></li>
</ul>

python tensorflow batching