我复制了一个测试脚本,将图像目录加载到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:
然而,当我运行脚本时,我收到了一个奇怪的错误:
OutOfRangeError (see above for traceback): FIFOQueue '_1_input_producer' is closed and has insufficient elements (requested 1, current size 0)
当我尝试查找解决方案时,我得到了几个不同的答案:
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)
我已经尝试了所有这些选项,但所有选项都失败了。原始脚本(https://gist.github.com/eerwitt/518b0c9564e500b4b50f)只有40行。我错过了什么解决方案?
更新
我现在正在运行这个:
# 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)
错误仍然存在。
答案 0 :(得分:0)
由于某种原因,您需要初始化本地和全局变量。我不确切地知道why。无论如何match_filenames_once
返回一个仅使用tf.global_variables_initializer()
初始化的局部变量。
所以,对你的问题添加:
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)
应该解决问题。
tf.initialize_all_variables()
是旧的初始化方式,我认为它是初始化的合法方式时用于初始化全局变量和局部变量。现在它被认为已被弃用并仅初始化全局变量。因此,一些使用旧式编码的源在执行代码时不会报告任何问题,但在较新的tensorflow版本中,同样会发生故障。