Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hdh7485/18feca2902f262c587ca914db5dacaa7 to your computer and use it in GitHub Desktop.
Save hdh7485/18feca2902f262c587ca914db5dacaa7 to your computer and use it in GitHub Desktop.
Example loading multiple JPEG files with TensorFlow and make them available as Tensors with the shape [[R, G, B], ... ].
# Typical setup to include TensorFlow.
import tensorflow as tf
# Make a queue of file names including all the JPEG images files in the relative
# image directory.
filename_queue = tf.train.string_input_producer(
tf.train.match_filenames_once("./images/*.jpg"))
# Read an entire image file which is required since they're JPEGs, if the images
# are too large they could be split in advance to smaller files or use the Fixed
# reader to split up the file.
image_reader = tf.WholeFileReader()
# Read a whole file from the queue, the first returned value in the tuple is the
# filename which we are ignoring.
_, image_file = image_reader.read(filename_queue)
# Decode the image as a JPEG file, this will turn it into a Tensor which we can
# then use in training.
image = tf.image.decode_jpeg(image_file)
# Start a new session to show example output.
with tf.Session() as sess:
# Required to get the filename matching to run.
tf.initialize_all_variables().run()
# Coordinate the loading of image files.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
# Get an image tensor and print its value.
image_tensor = sess.run([image])
print(image_tensor)
# Finish off the filename queue coordinator.
coord.request_stop()
coord.join(threads)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment