TensorFlow 图片读取队列为空

TensorFlow image reading queue empty

我正在尝试使用管道将图像读取到 CNN。我使用 string_input_producer() 来获取文件名队列,但它似乎没有做任何事情就挂在那里。下面是我的代码,请给我一些如何让它工作的建议。

def read_image_file(filename_queue, labels):
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
    image = tf.image.decode_png(value, channels=3)
    image = tf.cast(image, tf.float32)
    resized_image = tf.image.resize_images(image, [224, 112])
    with tf.Session() as sess:
        label = getLabel(labels, key.eval())
    return resized_image, label

def input_pipeline(filename_queue, queue_names, batch_size, num_epochs, labels):
    image, label = read_image_file(filename_queue, labels)
    min_after_dequeue = 10 * batch_size
    capacity = 20 * batch_size
    image_batch, label_batch = tf.train.shuffle_batch(
        [image, label], batch_size=batch_size, num_threads=1, capacity=capacity,
        min_after_dequeue=min_after_dequeue)
    return image_batch, label_batch

train_queue = tf.train.string_input_producer(trainnames, shuffle=True, num_epochs=epochs)

train_batch, train_label = input_pipeline(train_queue, trainnames, batch_size, epochs, labels)

prediction = AlexNet(x)

#Training
with tf.name_scope("cost_function") as scope:
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=train_label, logits=prediction(train_batch)))
    tf.summary.scalar("cost_function", cost)

    train_step = tf.train.MomentumOptimizer(learning_rate, 0.9).minimize(cost)

#Accuracy
with tf.name_scope("accuracy") as scope:
    correct_prediction = tf.equal(tf.argmax(prediction,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar("accuracy", accuracy)

    merged = tf.summary.merge_all()

#Session
with tf.Session() as sess:
    print('started')
    sess.run(tf.global_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord, start=True)
    sess.run(threads)

    try:
        for step in range(steps_per_epch * epochs):
            print('step: %d' %step)
            sess.run(train_step)
    except tf.errors.OutOfRangeError as ex:
        pass

    coord.request_stop()
    coord.join(threads)

您的代码不完全独立,因为 get_label 方法未定义。

但是您遇到的问题很可能来自 read_image_file 方法中的这些行:

with tf.Session() as sess:
    label = getLabel(labels, key.eval())

key.eval 部分尝试使尚未开始的队列元素出队。 在定义输入管道之前,您不应创建任何会话(也不应尝试评估 key(也可能 labels))。 get_label 方法应该只对 labelskey 和 return 一个 label 张量执行张量运算..

例如,您可以使用这些 tensor string operations 以便它们成为图表的一部分。