使用张量流中的队列的输入管道中出错



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

超出范围错误(回溯见上文):FIFOQueue "_2_batch/fifo_queue"已关闭且元素不足 (请求 15,当前大小 0) [[节点:批处理 = QueueDequeueManyV2[component_types=[DT_FLOAT], timeout_ms=-1, _device="/作业:本地主机/副本:0/任务:0/CPU:0"](批处理/fifo_queue,批处理/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()

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

正如我在对该问题的评论中所预期的那样,我的一个数据集上也出现了同样的错误。就我而言,问题在于并非数据集中的所有图像都可以解码为 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))

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

最新更新