filename = './train.tfrecords'
filename_queue = tf.train.string_input_producer([filename]) # 读入流中
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) # 返回文件名和文件
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw': tf.FixedLenFeature([], tf.string),
}) # 取出包含image和label的feature对象
image = tf.decode_raw(features['img_raw'], tf.uint8)
image = tf.reshape(image, [28, 28, 1])
label = tf.cast(features['label'], tf.int32)
with tf.Session() as sess: # 开始一个会话
init_op = tf.global_variables_initializer()
sess.run(init_op)
coord = tf.train.Coordinator() # 线程管理器
threads = tf.train.start_queue_runners(coord=coord)
for i in range(20):
example, l = sess.run([image, label]) # 在会话中取出image和label
img = Image.fromarray(example) # 这里Image是之前提到的
img.save('./Save_test/' + str(i) + '_''Label_' + str(l) + '.jpg') # 存下图片
print(example, l)
coord.request_stop()
coord.join(threads)
!!! img = Image.fromarray(example) 一直报错,原来如果tfrecode里面是灰度图,image = tf.reshape(image, [28, 28, 1])改为image = tf.reshape(image, [28, 28])即可