【发布时间】:2017-11-16 18:58:27
【问题描述】:
Tensorflow 1.3 Windows 7、Python 3.6
我遇到了这个错误:
OutOfRangeError (see above for traceback): RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 3, current size 0)
[[Node: shuffle_batch = QueueDequeueManyV2[component_types=[DT_FLOAT], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]
我看过类似的帖子:
尝试了列出的解决方案(除了注释一个,我不明白)
这是我的代码:
#more testing tfrecords
创建 TFREcords
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread, imresize
import os
import tensorflow as tf
import numpy
import cv2
#Gather image paths
DIR = r'C:\Users\Moondra\Desktop\DATA\IMAGE_2'
images = [os.path.join(DIR, image) for image in os.listdir(DIR)]
# len(images) is 41
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
tfrecords_filename = 'testing_2.tfrecords'
writer = tf.python_io.TFRecordWriter(tfrecords_filename)
for image in images:
img = cv2.imread(image)
img = cv2.resize(img, (300, 300), interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.astype(np.float32)
feature ={'train/image': _bytes_feature(tf.compat.as_bytes(img.tostring()))}
example = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(example.SerializeToString())
writer.close()
将 TFREcords 加载到批次中
#absolute path
tfrecords_filename = r'C:\Users\Moondra\Desktop\Transfer Learning Tutorials\testing_2.tfrecords'
with tf.Session() as sess:
feature = {'train/image': tf.FixedLenFeature([],tf.string)
}
filename_queue = tf.train.string_input_producer([tfrecords_filename],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['train/image'], tf.float32)
image = tf.reshape(image, [299, 299, 3])
images = tf.train.shuffle_batch([image], batch_size=3, capacity=12, num_threads=1, min_after_dequeue=10)
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for batch_index in range(2):
img = sess.run([images])
print(img.shape)
img = img.astype(np.uint8)
for j in range(6):
plt.subplot(2, 3, j+1)
plt.imshow(img[j, ...])
plt.show()
coord.request_stop()
coord.join(threads)
sess.close()
如果需要,我已将图像作为 zip 文件上传到我的 github:
https://github.com/moondra2017/Testing_Cat_classifier_Tensorflow
标记为 IMAGE_2.7z
谢谢。
【问题讨论】:
标签: python-3.x tensorflow deep-learning