Tensorflow 1.3 Windows 7,Python 3.6
我遇到了这个错误:
#more testing 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()
#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()
{{1}}
如果需要,我已将图像作为zip文件上传到我的github:
https://github.com/moondra2017/Testing_Cat_classifier_Tensorflow
标记为IMAGE_2.7z
谢谢。
答案 0 :(得分:2)
我有类似的问题,它是由num_epochs = 1参数引起的(我增加到我的实际纪元数)。
请参阅此帖子:TensorFlow random_shuffle_queue is closed and has insufficient elements
答案 1 :(得分:0)
弄清楚错误。
我保存的尺寸为[300,300]
我试图加载的尺寸是[299,299]
#Image = tf.reshape(image, [299, 299, 3])
答案 2 :(得分:0)
我之前也有同样的问题,但是很多答案都不好,最终我发现答案是,如果要使用num_epochs,我们应该在[tf.Session()作为sess:]之前生成队列。