读取TFRecords文件时队列关闭并清空

时间:2017-12-12 09:59:04

标签: python tensorflow tfrecord

我创建了一个数据集并将其转换为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],甚至尝试将其保留为一维形状,但我仍然得到错误。我没有想法,所以我请你帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

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