Machine learning TensorFlow图像读取队列为空

Machine learning TensorFlow图像读取队列为空,machine-learning,tensorflow,computer-vision,Machine Learning,Tensorflow,Computer Vision,我正在尝试使用管道向CNN读取图像。我使用了string\u input\u producer()来获取文件名队列,但它似乎什么也不做就挂在那里。下面是我的代码,请给我一些如何使它工作的建议 def read_image_file(filename_queue, labels): reader = tf.WholeFileReader() key, value = reader.read(filename_queue) image = tf.image.decode_pn

我正在尝试使用管道向CNN读取图像。我使用了
string\u input\u 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\u label
方法,您的代码不是完全独立的

但您遇到的问题很可能来自
read\u image\u file
方法中的以下几行:

with tf.Session() as sess:
    label = getLabel(labels, key.eval())
key.eval
部分尝试将尚未启动的队列元素出列。 在定义输入管道之前,不应创建任何会话(也不应尝试求值
(可能还有
标签
)。
get\u label
方法应仅对
标签
执行张量操作,并返回一个
标签
张量


例如,您可以使用它们,使它们成为图形的一部分。

我在代码中定义了
getLabel
,但没有附加在这里,它基本上是从文件名(字符串)提取标签,但键是张量。所以我做了
key.eval()
来获取文件名的字符串。现在它似乎不起作用了,有没有其他方法从张量中获取字符串?您可能需要用字符串张量操作替换所有字符串操作,因此它们将成为图形的一部分,并将在运行时执行。出于未知原因,我无法打开tensorflow的网站。你知道我可以用什么操作从字符串张量中提取标签吗?