Tensorflow:OutOfRangeError tf.train.string_input_producer已关闭且元素不足 [英] Tensorflow: OutOfRangeError tf.train.string_input_producer is closed and has insufficient elements
问题描述
我一直在尝试以下代码段尝试输入管道
I have been trying the following code segment to experiment with the input pipeline
import tensorflow as tf
with tf.Session() as sess:
filename = ['/data/read/A.JPG', '/data/read/B.JPG', '/data/read/C.JPG']
filename_queue = tf.train.string_input_producer(filename, shuffle=False, num_epochs=5)
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
tf.local_variables_initializer().run()
threads = tf.train.start_queue_runners(sess=sess)
i = 0
while True:
i += 1
image_data = sess.run(value)
with open('/data/read/test_%d.jpg' % i, 'wb') as f:
f.write(image_data)
运行以上代码得到以下错误消息,看起来是由 string_input_producer
生成的 filename_queue
引起的。但我不清楚问题出在哪里以及如何解决。
Running the above code gets the following error message, looks like it is caused by filename_queue
generated by string_input_producer
. But I am not clear what's the problem and how to correct it. Thanks.
caused by op 'ReaderReadV2', defined at:
File "test_input.py", line 12, in <module>
key, value = reader.read(filename_queue)
File "lib/python3.6/site-packages/tensorflow/python/ops/io_ops.py", line 193, in read
return gen_io_ops._reader_read_v2(self._reader_ref, queue_ref, name=name)
File "lib/python3.6/site-packages/tensorflow/python/ops/gen_io_ops.py", line 411, in _reader_read_v2
queue_handle=queue_handle, name=name)
File "lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
op_def=op_def)
File "lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
original_op=self._default_original_op, op_def=op_def)
File "lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
self._traceback = _extract_stack()
OutOfRangeError (see above for traceback): FIFOQueue '_1_input_producer' is closed and has insufficient elements (requested 1, current size 0)
[[Node: ReaderReadV2 = ReaderReadV2[_device="/job:localhost/replica:0/task:0/cpu:0"](WholeFileReaderV2, input_producer)]]
推荐答案
队列运行器
应该始终与 Coordinator
。
协调器帮助多个线程一起停止,并向等待它们停止的程序报告
异常。
The Coordinator helps multiple threads stop together and report exceptions to a program that waits for them to stop.
它们还捕获并处理队列生成的异常,包括 tf.errors.OutOfRangeError
异常,用于报告队列已关闭。
They also catch and handle exceptions generated by queues, including the tf.errors.OutOfRangeError
exception, which is used to report that a queue was closed.
所以您的训练应该是:
with tf.Session() as sess:
filename = ['/data/read/A.JPG', '/data/read/B.JPG', '/data/read/C.JPG']
filename_queue = tf.train.string_input_producer(filename, shuffle=False, num_epochs=5)
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
tf.local_variables_initializer().run()
# Create a coordinator, launch the queue runner threads.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
i = 0
try:
while not coord.should_stop():
while True:
i += 1
image_data = sess.run(value)
with open('test_%d.jpg' % i, 'wb') as f:
f.write(image_data)
except tf.errors.OutOfRangeError:
# When done, ask the threads to stop.
print('Done training -- epoch limit reached')
finally:
coord.request_stop()
# Wait for threads to finish.
coord.join(threads)
这篇关于Tensorflow:OutOfRangeError tf.train.string_input_producer已关闭且元素不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!