2017-07-02 62 views
1

我一直在試圖將下面的代碼段與所述輸入管線Tensorflow:OutOfRangeError異常tf.train.string_input_producer閉合並且沒有足夠的元件

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造成的。但我不清楚是什麼問題以及如何糾正它。謝謝。

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

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) 
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:一個整數(可選)。 如果指定,則在生成OutOfRange錯誤之前,string_input_producer會從string_tensor num_epochs次生成每個字符串。如果未指定,則string_input_producer可循環訪問string_tensor中的字符串,無限次數。

在您的無限循環中執行了15次出列操作後,會引發異常OutOfRangeError。如果您未指定num_epochs,則循環將運行,直到您以其他方式停止循環。

相關問題