Skip to content

Instantly share code, notes, and snippets.

@eerwitt
Created January 31, 2016 05:52
Show Gist options
  • Star 99 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
  • Save eerwitt/518b0c9564e500b4b50f to your computer and use it in GitHub Desktop.
Save eerwitt/518b0c9564e500b4b50f 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)
@flyher
Copy link

flyher commented May 18, 2018

Hi all, the code will work after you change the code from

filename_queue = tf.train.string_input_producer(
    tf.train.match_filenames_once("./images/*.jpg"))

to

filename_queue = tf.train.string_input_producer(['./*.jpg'])

I donnot know why, so who can explain it.

@shivangiiert
Copy link

shivangiiert commented Apr 3, 2019

filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once('./apple2orange/trainA/*.jpg'))
gives me error TypeError: Fetch argument <tensorflow.python.ops.data_flow_ops.FIFOQueue object at 0xb1a840ef0> has invalid type <class 'tensorflow.python.ops.data_flow_ops.FIFOQueue'>, must be a string or Tensor. (Can not convert a FIFOQueue into a Tensor or Operation.)

can anybody tell me why tf.train.match_filenames_once is giving me error of Can not convert a FIFOQueue into a Tensor or Operation?
how to solve this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment