简体   繁体   English

在tensorflow中使用队列运行器时出现错误

[英]error arise when using queue runner in tensorflow

I am new to tensorflow and I am now learning how to use queue runner. 我是tensorflow的新手,现在正在学习如何使用队列运行器。 What I want to do is to read binary files from a dir and make each file an array. 我想做的是从目录中读取二进制文件,并使每个文件成为数组。 I use two threads and make 4 arrays a batch. 我使用两个线程并批量处理4个数组。 The code is as follows. 代码如下。

  import glob

  import tensorflow as tf

  def readfile(filenames_queue):

        filename = filenames_queue.dequeue()
        value_strings = tf.read_file(filename)
        array = tf.decode_raw(value_strings,tf.uint8)
        return [array]
 def input_pipeline(filenames,batch_size,num_threads=2):

       filenames_queue = tf.train.string_input_producer(filenames)
       thread_lists = [readfile(filenames_queue) for _ in range(num_threads)] 
       min_after_dequeue = 1000 
       capacity = min_after_dequeue+3*batch_size
       arrays = tf.train.shuffle_batch_join(thread_lists,batch_size,capacity,min_after_dequeue)
       return arrays
if __name__ == "__main__":

      filenames = glob.glob('dir/*')
      arrays_batch = input_pipeline(filenames,4)
      with tf.Session() as sess:
           tf.global_variables_initializer().run()
           coord = tf.train.Coordinator()
           threads = tf.train.start_queue_runners(sess,coord)
           for i in range(100):
                 print sess.run(arrays_batch)
           coord.request_stop()
           coord.join(threads)

I have fixed the error pointed out by Victor and Sorin, but a new error rise: 我已经修正了维克多和索林指出的错误,但是又出现了新的错误:

File "input_queue.py", line 36, in print sess.run(im_arrays_batch) 文件“ input_queue.py”,行36,在打印sess.run(im_arrays_batch)中

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 889, in run run_metadata_ptr) 在运行run_metadata_ptr中,文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py”,行889)

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1120, in _run feed_dict_tensor, options, run_metadata) 文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py”,行1120,在_run feed_dict_tensor中,选项,run_metadata)

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1317, in _do_run options, run_metadata) _do_run选项(run_metadata)中的文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py”,行1317

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1336, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.OutOfRangeError: RandomShuffleQueue '_1_shuffle_batch_join/random_shuffle_queue' is closed and has insufficient elements (requested 2, current size 0) [[Node: shuffle_batch_join = QueueDequeueManyV2[component_types=[DT_UINT8], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](shuffle_batch_join/random_shuffle_queue, shuffle_batch_join/n)]] _do_call中的文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py”,行1336提高类型(e)(node_def,op,message)tensorflow.python .framework.errors_impl.OutOfRangeError:RandomShuffleQueue'_1_shuffle_batch_join / random_shuffle_queue'已关闭并且元素不足(要求2,当前大小为0)[[节点:shuffle_batch_join = QueueDequeueManyV2 [component_types = [DT_UINT8] /,time_device = -1,作业:本地主机/副本:0 /任务:0 /设备:CPU:0“](shuffle_batch_join / random_shuffle_queue,shuffle_batch_join / n)]]

Caused by op u'shuffle_batch_join', defined at: 由op u'shuffle_batch_join'引起,定义为:

File "input_queue.py", line 30, in im_arrays_batch = input_pipeline(filenames,2) 文件“ input_queue.py”,第30行,位于im_arrays_batch = input_pipeline(filenames,2)中

File "input_queue.py", line 23, in input_pipeline arrays_batch = tf.train.shuffle_batch_join(thread_lists,batch_size,capacity,min_after_dequeue) 在input_pipeline arrays_batch = tf.train.shuffle_batch_join(thread_lists,batch_size,capacity,min_after_dequeue)中的文件“ input_queue.py”,第23行,

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/input.py", line 1367, in shuffle_batch_join name=name) 文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/input.py”,行1367,在shuffle_batch_join name = name中)

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/input.py", line 833, in _shuffle_batch_join dequeued = queue.dequeue_many(batch_size, name=name) _shuffle_batch_join中的文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/input.py”,行833,dequeued = queue.dequeue_many(batch_size,name = name)

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/data_flow_ops.py", line 464, in dequeue_many self._queue_ref, n=n, component_types=self._dtypes, name=name) 文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/data_flow_ops.py”,行464,位于dequeue_many self._queue_ref,n = n,component_types = self._dtypes,名称=名称)

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_data_flow_ops.py", line 2418, in _queue_dequeue_many_v2 component_types=component_types, timeout_ms=timeout_ms, name=name) 文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_data_flow_ops.py”,第2418行,位于_queue_dequeue_many_v2中,component_types = component_types,timeout_ms = timeout_ms,name = name)

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper op_def=op_def) 文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py”,行787,位于_apply_op_helper op_def = op_def)

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2956, in create_op op_def=op_def) 文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py”,行2956,在create_op op_def = op_def中)

File "/usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1470, in init self._traceback = self._graph._extract_stack() # pylint: disable=protected-access 文件“ /usr/local/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py”,行1470, init self._traceback = self._graph._extract_stack()#pylint:禁用=受保护的访问

OutOfRangeError (see above for traceback): RandomShuffleQueue '_1_shuffle_batch_join/random_shuffle_queue' is closed and has insufficient elements (requested 2, current size 0) [[Node: shuffle_batch_join = QueueDequeueManyV2[component_types=[DT_UINT8], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](shuffle_batch_join/random_shuffle_queue, shuffle_batch_join/n)]] OutOfRangeError(请参见上面的回溯):RandomShuffleQueue'_1_shuffle_batch_join / random_shuffle_queue'已关闭且元素不足(要求2,当前大小为0)[[节点:shuffle_batch_join = QueueDequeueManyV2 [component_types = [DT_UINT8],timeout_ms = -1,DT_device = / job:本地主机/副本:0 /任务:0 /设备:CPU:0“](shuffle_batch_join / random_shuffle_queue,shuffle_batch_join / n)]]

Your readfile(...): function is supposed to return an iterable so that you can return features and labels or other things like that. 您的readfile(...):函数应该返回一个可迭代的函数,以便您可以返回功能和标签或类似的东西。

So to fix your code change readfile(...): to 因此,要解决您的代码更改readfile(...):

return [arrays]

From tf.train.shuffle_batch_join : tf.train.shuffle_batch_join

The tensors_list argument is a list of tuples of tensors tensors_list参数是张量元组的列表

Here, your calls to tf.decode_raw produces Tensor instances , and you put them in a list with thread_lists = [readfile(filenames_queue) for _ in range(num_threads)] . 在这里,您对tf.decode_raw的调用将产生Tensor实例 ,并将它们放入带有thread_lists = [readfile(filenames_queue) for _ in range(num_threads)]

It is therefore not a list of tuples of tensors you give but a list of tensors, and consequently the tensors are trying to be iterated on, hence the error TypeError: 'Tensor' object is not iterable . 因此,它不是您给出的张量的元组列表,而是张量的列表,因此这些张量试图进行迭代,因此错误TypeError: 'Tensor' object is not iterable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM