在tensorflow中使用队列的输入管道出错

时间:2017-07-05 07:47:03

标签: python tensorflow fifo

我收到下面提到的代码的错误。请帮我解决这个问题。 每次运行此代码时,此代码都是打印变量批次数。我无法弄清楚错误。

  

OutOfRangeError(参见上面的回溯):FIFOQueue   '_2_batch / fifo_queue'已关闭且元素不足   (要求15,当前大小0)[[节点:批次=   QueueDequeueManyV2 [component_types = [DT_FLOAT],timeout_ms = -1,   _device =“/ job:localhost / replica:0 / task:0 / cpu:0”](batch / fifo_queue,batch / n)]]

import tensorflow as tf 
import numpy as np 
import os

batch_size = 16 
min_queue = 256


def load_image():
    length = calculate_size("./Coco_subset_5356")

    names = tf.train.match_filenames_once("./Coco_subset_5356/*.jpg")

    # Extracted names from the file  
    filename_queue = tf.train.string_input_producer(names)

    #Initialised the File reader
    reader = tf.WholeFileReader()

    key, value = reader.read(filename_queue)
    my_img = tf.image.decode_jpeg(value, channels = 3)
    my_img = tf.image.resize_images(my_img, [256,256])
    my_img.set_shape((256,256,3))


    print(length)

    images = tf.train.batch(
      [my_img],
      batch_size=batch_size,
      num_threads=1,
      capacity=min_queue + 3*batch_size)

     print(images)

with tf.Session() as sess:  
    #sess.run(init_op)  
    tf.local_variables_initializer().run()
    #print(tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES))

    #For coordination between queue runner and the reader
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord = coord)
    for j in range(length/batch_size):
        #optimizer.run(feed_dict={input_batch: images[i])
        x = sess.run(images)    
        print(j)
        print(x.shape)

    coord.request_stop()
    coord.join(threads)

def calculate_size(img_dir):
    file = []
    for subdir, dirs, files in os.walk(img_dir):
        for i in files:
            i = os.path.join(img_dir, i)
            file.append(i)
        length = len(file)
    return length

load_image()

1 个答案:

答案 0 :(得分:0)

免责声明:这不是一个正确的答案,但我无法在评论中发布代码,这可能会指向正确的方向。

正如我对该问题的评论所预期的那样,我的一个数据集上出现了同样的错误。就我而言,问题在于并非我的数据集中的所有图像都可以解码为jpg文件。截断的图像,在我的情况下,1x1像素图像隐藏在我的数据中,每当我试图访问数据集时,我就会让队列关闭报告错误,我很难见到。

我解决了编写一个小过滤器脚本的问题,该脚本逐个遍历所有文件并记录它无法处理的文件:

import tensorflow as tf
import glob

image_data_placeholder = tf.placeholder(tf.string)

filenames = glob.glob(r'C:\my_test_folder\*.jpg')
decoded_img_op = tf.image.decode_jpeg(image_data_placeholder)

with tf.Session() as sess:
  for fn in filenames:
    with open(fn, 'rb') as fp:
      image_data = fp.read()
    try:
      sess.run(decoded_img_op, feed_dict={image_data_placeholder:image_data})
    except:
      # log the filename o the broken image (or delete/move/etc)
      print('Cannot decode file {}'.format(fn))

这不是最有效的实现,但对我的用例来说已经足够了。