Skip to content

Instantly share code, notes, and snippets.

@AtiqueUrRehman
Created February 11, 2017 18:11
Show Gist options
  • Save AtiqueUrRehman/78c3584d105fd282beb962d24cc47927 to your computer and use it in GitHub Desktop.
Save AtiqueUrRehman/78c3584d105fd282beb962d24cc47927 to your computer and use it in GitHub Desktop.
Read the examples in tfrecords files in batches using Tensorflow queue runners
import tensorflow as tf
def read_and_decode_example(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
# Defaults are not specified since both keys are required.
features={
'label/image/encoded': tf.FixedLenFeature([], tf.string),
'orig/image/encoded': tf.FixedLenFeature([], tf.string),
})
image = tf.reshape(tf.image.decode_jpeg(features['orig/image/encoded'], channels=3),[100,100,3])
label = tf.reshape(tf.image.decode_jpeg(features['label/image/encoded'], channels=3),[100,100,3])
return image, label
def read_in_batches(files):
sess = tf.Session()
filename_queue = tf.train.string_input_producer(files)
image, label = read_and_decode_example(filename_queue)
images_batch, labels_batch = tf.train.shuffle_batch(
[image, label], batch_size=128,
capacity=2000,
min_after_dequeue=1000)
init_op = tf.global_variables_initializer()
sess.run(init_op)
tf.train.start_queue_runners(sess=sess)
labels, images= sess.run([labels_batch, images_batch])
read_in_batches(['../Data/tfrecords/00000-of-00004','../Data/tfrecords/00001-of-00004'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment