读取 TFRecords 文件时队列关闭并为空
Queue closed and empty when reading TFRecords file
我创建了一个数据集并将其转换为 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] 甚至试图让它保持一维形状,但我仍然得到错误。我没有想法,所以我向你寻求帮助。提前致谢。
忘记运行初始化操作。没关系-.-
我创建了一个数据集并将其转换为 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] 甚至试图让它保持一维形状,但我仍然得到错误。我没有想法,所以我向你寻求帮助。提前致谢。
忘记运行初始化操作。没关系-.-