Tensorflow:OutOfRangeError tf.train.string_input_producer已关闭且元素不足

时间:2017-07-02 19:01:46

标签: tensorflow deep-learning

我一直在尝试以下代码段来试验输入管道

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)

运行上面的代码会收到以下错误消息,看起来它是由filename_queue生成的string_input_producer引起的。但我不清楚问题是什么以及如何纠正它。感谢。

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

2 个答案:

答案 0 :(得分:3)

queue runners应始终与Coordinator一起使用。

  

协调员帮助多个线程停在一起并报告   等待它们停止的程序的例外。

它们还捕获并处理队列生成的异常,包括tf.errors.OutOfRangeError异常,该异常用于报告队列已关闭。

所以你的训练应该是:

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)

答案 1 :(得分:0)

这段代码:

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)

创建一个文件名队列,允许将3个文件中的每个文件正好出列5次。查看文档(突出显示是我的):

  

num_epochs:一个整数(可选)。 如果指定,string_input_producer会在生成OutOfRange错误之前从string_tensor num_epochs次生成每个字符串。如果未指定,string_input_producer可以循环遍历{{1}中的字符串无限次。

在无限循环中进行15次出列操作后,会引发异常string_tensor。如果您没有指定OutOfRangeError,则循环将一直运行,直到您以其他方式停止它。