Skip to content

Instantly share code, notes, and snippets.

@flyher
Forked from eerwitt/load_jpeg_with_tensorflow.py
Last active May 18, 2018 05:46
Show Gist options
  • Save flyher/3ce88c9e77af2c6cd423380d10390927 to your computer and use it in GitHub Desktop.
Save flyher/3ce88c9e77af2c6cd423380d10390927 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.
# This code will not work
# filename_queue = tf.train.string_input_producer(
# tf.train.match_filenames_once("./images/*.jpg"))
filename_queue = tf.train.string_input_producer(['./*.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