【问题标题】:Queue closed and empty when reading TFRecords file读取 TFRecords 文件时队列关闭并为空
【发布时间】:2018-01-26 15:37:36
【问题描述】:

我创建了一个数据集并将其转换为 TFRecords 文件。这是我用来编写文件的代码的一部分:

example = tf.train.Example(features=tf.train.Features(feature={
        'height': _int64_feature(rows),
        'width': _int64_feature(cols),
        'depth': _int64_feature(depth),
        'label': _int64_feature(int(labels[index])),
        'name': _bytes_feature(imagePaths[index].encode(encoding='utf-8')),
        'image_raw': _bytes_feature(imageRaw.tostring())}))

当我使用 tensorflow 的 python_io 模块时,记录中的数据读取得很好,所有数据都与原始图像和标签相同。当我现在尝试在图表中读取文件时,我收到以下错误消息:

OutOfRangeError (see above for traceback): RandomShuffleQueue '_0_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 10, current size 0)

当添加了错误元素时,其他线程建议队列正在关闭,所以我放弃了除了基本的重塑和演员之外的所有内容。错误仍然存​​在。这是我的测试代码:

testInput.py

import tensorflow as tf


def inputs(dataDir):
    feature = {'image_raw': tf.FixedLenFeature([], tf.string),
               'label': tf.FixedLenFeature([], tf.int64)}
    # Create a list of filenames and pass it to a queue
    filename_queue = tf.train.string_input_producer([dataDir], num_epochs=1)
    # Define a reader and read the next record
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    # Decode the record read by the reader
    features = tf.parse_single_example(serialized_example, features=feature)
    # Convert the image data from string back to the numbers
    image = tf.decode_raw(features['image_raw'], tf.uint8)

    # Cast label data into int32
    label = tf.cast(features['label'], tf.int32)
    # Reshape image data into the original shape
    image = tf.reshape(image, [64, 64, 3])

    # Any preprocessing here ...

    # Creates batches by randomly shuffling tensors
    images, labels = tf.train.shuffle_batch([image, label],
                                        batch_size=10,
                                        capacity=30,
                                        num_threads=1,
                                        min_after_dequeue=10)

    return images, labels

test.py

import tensorflow as tf
from PIL import Image
import skimage.io as io
import testInput as data
import numpy as np


images, labels = data.inputs('./train.tfrecords')

with tf.Session() as sess:
    tf.local_variables_initializer()
    tf.global_variables_initializer()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    img, lab = sess.run([images, labels])
    print(img[0, :, :, :].shape)

    io.imshow(img[0, :, :, :])
    io.show()
    input('Press key...')

    coord.join(threads)
    sess.close()

我确保图像的形状确实是 [64, 64, 3],甚至尝试将其保留为一维形状,但我仍然得到错误。我没有想法,所以我向你寻求帮助。提前致谢。

【问题讨论】:

    标签: python tensorflow tfrecord


    【解决方案1】:

    忘记运行初始化操作。没关系-.-

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 2018-06-14
      • 2019-06-28
      • 1970-01-01
      • 2014-07-18
      • 2020-02-06
      相关资源
      最近更新 更多