0

I copied a test script to load a directory of images into Tensorflow:

# Typical setup to include TensorFlow.
import tensorflow as tf
from sys import argv
# 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(argv[1] + "/*.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_orig = tf.image.decode_jpeg(image_file)
image = tf.image.resize_images(image_orig, [224, 224])
image.set_shape((224, 224, 3))

# Start a new session to show example output.
with tf.Session() as sess: 

However, when I ran the script, I received an odd bug:

OutOfRangeError (see above for traceback): FIFOQueue '_1_input_producer' is closed and has insufficient elements (requested 1, current size 0)

And when I tried to look up a solution, I got several different answers:

tf.initialize_all_variables().run()
tf.local_variables_initializer().run()
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)

I have tried all of those options, and all of them have failed. The original script (https://gist.github.com/eerwitt/518b0c9564e500b4b50f) has barely 40 lines. What solution am I missing?

UPDATE
I'm now running this:

# Start a new session to show example output.
with tf.Session() as sess:
    # Required to get the filename matching to run.
    sess.run(tf.local_variables_initializer())
    sess.run(tf.global_variables_initializer())

    # 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)

And the error still occurs.

Rich
  • 1,103
  • 1
  • 15
  • 36

1 Answers1

0

You need to initialize both local and global variables for some reason. I don't know exactly why though. Anyway match_filenames_once returns a local variable which is not initialized simply by using tf.global_variables_initializer().

So, to your problem adding:

with tf.Session() as sess:
    sess.run(tf.local_variables_initializer())
    sess.run(tf.global_variables_initializer())
    # actual code should go here
    coord.request_stop()
    coord.join(threads)

should solve the problem.

tf.initialize_all_variables() is the old way of initialization and I think it used to initialize both global and local variables when it was the legal way of initialization. Nowadays it's considered deprecated and initializes only the global variables. So, some sources that use old style coding do not report any problem in executing the code but in newer tensorflow versions the same breaks down.

Eypros
  • 5,370
  • 6
  • 42
  • 75
  • And yes, I tried running it without `Coordinate the loading of image files`, there was no effect. – Rich Jan 23 '18 at 09:28
  • Your code works for me. Check with `glob.glob(argv[1] + "/*.jpg")` that it can actually locates the files. – Eypros Jan 23 '18 at 10:02