title: TensorFlow TFRecords load images sequentially
body: <p>I'm having problems loading in images one by one after converting my own sets of images to a TFRecords file. At both train and test time they are loaded, in a random order, which for training is great, but for testing I require that the images come one by one.</p>
  
  <p><strong>Conversion of own 2D gray scale *.png images to TFRecords</strong></p>
  
  <p>I've converted my images to TFRecords by using <a href="https://github.com/tensorflow/models/blob/master/inception/inception/data/build_image_data.py" rel="nofollow noreferrer">build_image_data.py</a>, and it seems to work just fine :)</p>
  
  <p><strong>Reading in the TFRecords file</strong></p>
  
  <p>To read in the images from the TFRecords file I use the following piece of code</p>
  
  <pre><code>def getImage(filename):
      # convert filenames to a queue for an input pipeline.
      filenameQ = tf.train.string_input_producer([filename], num_epochs=None)
  
      # object to read records
      recordReader = tf.TFRecordReader()
  
      # read the full set of features for a single example
      key, fullExample = recordReader.read(filenameQ)
  
      # parse the full example into its' component features.
      features = tf.parse_single_example(
          fullExample,
          features={
              'image/height': tf.FixedLenFeature([], tf.int64),
              'image/width': tf.FixedLenFeature([], tf.int64),
              'image/colorspace': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
              'image/channels':  tf.FixedLenFeature([], tf.int64),
              'image/class/label': tf.FixedLenFeature([], tf.int64),
              'image/class/text': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
              'image/format': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
              'image/filename': tf.FixedLenFeature([], dtype=tf.string, default_value=''),
              'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value='')
          })
  
  # now we are going to manipulate the label and image features
  label = features['image/class/label']
  image_buffer = features['image/encoded']
  
  # Decode the jpeg
  with tf.name_scope('decode_jpeg', [image_buffer], None):
      # decode
      image = tf.image.decode_jpeg(image_buffer, channels=3)
  
      # and convert to single precision data type
      image = tf.image.convert_image_dtype(image, dtype=tf.float32)
  
  # cast image into a single array, where each element corresponds to the greyscale
  # value of a single pixel.
  image = tf.reshape(tf.image.rgb_to_grayscale(image), [101 * 201])
  
  # re-define label as a "one-hot" vector
  label = tf.pack(tf.one_hot(label - 1, 4))
  
  return label, image
  
  
  # associate the "label" and "image" objects with the corresponding features read from
  # a single example in the training data file
  label, image = getImage("../image-to-tfrecords/train-00000-of-00001")
  
  # associate the "label_batch" and "image_batch" objects with a randomly selected batch---
  # of labels and images respectively
  imageBatch, labelBatch = tf.train.shuffle_batch(
      [image, label], batch_size=100,
      capacity=2000,
      min_after_dequeue=1000)
  </code></pre>
  
  <p><strong>Using the image to predict</strong></p>
  
  <p>Then I load my saved model and its weights, predict_op, etc. and evaluate the prediction operation by:</p>
  
  <pre><code>with tf.Session() as sess:
      for i in range(1):
          batch_xs = sess.run(image)
          batch_xs = np.reshape(batch_xs, (-1, self.img_w * self.img_h))
          prediction = sess.run([predict_op], feed_dict={x: batch_xs})
  </code></pre>
  
  <p>Where <code>image</code> is the return from the <code>getImage</code> function.</p>
  
  <p><strong>Directory Structure of Images To TFRecords (just for ref)</strong></p>
  
  <pre><code>├── train
  |   ├── chef
  |       ├── data0.png
  |       ├── ...
  |       └── data5467.png
  |   ├── chicken
  |       ├── data0.png
  |       ├── ...
  |       └── data2098.png
  |   ├── parasaurolophus
  |       ├── data0.png
  |       ├── ...
  |       └── data2977.png
  |   └── Trex
  |       ├── data0.png
  |       ├── ...
  |       └── data2841.png
  ├── validation
  |   ├── ...
  ├── build_image_data.py
  └── labels.txt
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>I've tried to print the filenames of the images received by the getImage function, and it is getting the images in random order from the TFRecords file.</p>
  
  <p>1st run:</p>
  
  <pre><code>[output]:
  data4918.png
  data4984.png
  data1144.png
  data2186.png
  data138.png
  data573.png
  data2590.png
  data392.png
  data846.png
  data3222.png
  </code></pre>
  
  <p>2nd run:</p>
  
  <pre><code>[output]:
  data5212.png
  data1144.png
  data3588.png
  data4054.png
  data2938.png
  data3396.png
  data4711.png
  data3222.png
  data5003.png
  data1298.png
  </code></pre>
  
  <p>Shouldn't it read the images in the same order every time??</p>