我的代码是从 tfrecord 文件中读取图像
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt data_path='christmas.tfrecords'with tf.Session() as sess: feature={'image':tf.FixedLenFeature([],tf.string),'label':tf.FixedLenFeature([],tf.int64)} # 创建文件名列表并传递给队列 filename_queue = tf.train.string_input_producer([data_path], num_epochs=1) # 定义读取器并读取下一条记录 reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) # 解码读取器读取的记录 features = tf.parse_single_example(serialized_example, features=feature) # 将图像数据从字符串转换回数字 image = tf.decode_raw(features['image'], tf.float32) # 将标签数据转换为 int32 label = tf.cast(features['label'], tf.int32) # 将图像数据重塑为原始形状 image = tf.reshape(image, [224, 224, 1]) # 在此处进行任何预处理... # 通过随机洗牌张量创建批次 images, labels = tf.train.shuffle_batch([image, label], batch_size=10, capacity=30, num_threads=1, min_after_dequeue=10) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) # 创建协调器并运行所有 QueueRunner 对象 coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) for batch_index in range(5): img, lbl = sess.run([images, labels]) img = img.astype(np.uint8) for j in range(6): plt.subplot(2, 3, j+1) plt.imshow(img[j, ...]) plt.title('猫' if lbl[j]==0 else '狗') plt.show() # 停止线程 coord.request_stop() # 等待线程停止 coord.join(threads) sess.close()
错误信息
Traceback (most recent call last): File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1323, in _do_call return fn(*args) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1302, in _run_fn status, run_metadata) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__ c_api.TF_GetCode(self.status.status))tensorflow.python.framework.errors_impl.OutOfRangeError: RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' 已关闭且元素不足(请求 10,当前大小 0) [[Node: shuffle_batch = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]在处理上述异常时,发生了另一个异常:Traceback (most recent call last): File "read_tfrecords.py", line 35, in <module> img, lbl = sess.run([images, labels]) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 889, in run run_metadata_ptr) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1120, in _run feed_dict_tensor, options, run_metadata) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1317, in _do_run options, run_metadata) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1336, in _do_call raise type(e)(node_def, op, message)tensorflow.python.framework.errors_impl.OutOfRangeError: RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' 已关闭且元素不足(请求 10,当前大小 0) [[Node: shuffle_batch = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]由操作 'shuffle_batch' 引起,定义于: File "read_tfrecords.py", line 27, in <module> images, labels = tf.train.shuffle_batch([image, label], batch_size=10, capacity=30, num_threads=1, min_after_dequeue=10) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/training/input.py", line 1225, in shuffle_batch name=name) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/training/input.py", line 796, in _shuffle_batch dequeued = queue.dequeue_many(batch_size, name=name) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/ops/data_flow_ops.py", line 464, in dequeue_many self._queue_ref, n=n, component_types=self._dtypes, name=name) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/ops/gen_data_flow_ops.py", line 2418, in _queue_dequeue_many_v2 component_types=component_types, timeout_ms=timeout_ms, name=name) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper op_def=op_def) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2956, in create_op op_def=op_def) File "/home/robinreni/Documents/pythonprojects/cnn/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1470, in __init__ self._traceback = self._graph._extract_stack() # pylint: disable=protected-accessOutOfRangeError(参见上面的回溯):RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' 已关闭且元素不足(请求 10,当前大小 0) [[Node: shuffle_batch = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_INT32], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]
我将所有图像和标签转换为 tfrecord 文件,并在训练过程中需要将文件转换为原始图像和标签。我尝试从 tfrecords 中读取图像,但出现了 OutOfRange 错误。请问如何修复这个错误?
回答: