tensorflow 读、存取图像数据的 TFRecord 方法

1.     利用TFRecord 格式   读、存 取    Mnist数据集的方法

存取   Mnist数据集的方法     (TFRecord格式

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np

def _float32_feature(value):
    return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))

def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

mnist=input_data.read_data_sets('./data', dtype=tf.uint8, one_hot=True)
"""
print(mnist.train.images)
print(mnist.train.labels)
print(mnist.test.images)
print(mnist.test.labels)
"""
train_images=mnist.train.images
train_labels=mnist.train.labels
#test_images=mnist.test.images
#test_labels=mnist.test.labels

train_num=mnist.train.num_examples 
#test_num=mnist.test.num_examples 


pixels=train_images.shape[1]   # 784 = 28*28


file_out='./data/output.tfrecords'
writer=tf.python_io.TFRecordWriter(file_out)


for index in range(train_num):
    image_raw=train_images[index].tostring() #转换为bytes序列   

    example=tf.train.Example(features=tf.train.Features(feature={
               'pixels': _int64_feature(pixels),
               'label':_int64_feature(np.argmax(train_labels[index])),
               'x':_float32_feature(0.1),
               'image_raw':_bytes_feature(image_raw)}))

    writer.write(example.SerializeToString())
writer.close()



 

读取   Mnist数据集的方法     (TFRecord格式

import tensorflow as tf

reader=tf.TFRecordReader()

files=tf.train.match_filenames_once('./data/output.*')

#filename_queue=tf.train.string_input_producer(['./data/output.tfrecords'])
filename_queue=tf.train.string_input_producer(files)

_, serialized_example=reader.read(filename_queue)

features=tf.parse_single_example(serialized_example,
                   features={
                           'image_raw':tf.FixedLenFeature([], tf.string),
                           'pixels':tf.FixedLenFeature([], tf.int64),
                           'label':tf.FixedLenFeature([], tf.int64),
                           'x':tf.FixedLenFeature([], tf.float32)
                            })

#print(features['image_raw'])    # tensor string (bytes tensor      string tensor)

# necessary operation
# bytes_list   to   uint8_list
image=tf.decode_raw(features['image_raw'], tf.uint8) 
#print(image)    # tensor uint8

label=tf.cast(features['label'], tf.int32)
pixels=tf.cast(features['pixels'], tf.int32)
#image.set_shape([pixels**0.5, pixels**0.5])
image.set_shape([784])

batch_size=128
image_batch, label_batch, pixels_batch=tf.train.batch([image, label, pixels], batch_size=batch_size, capacity=1000+3*batch_size)




coord=tf.train.Coordinator()

with tf.Session() as sess:
    sess.run(tf.local_variables_initializer())
    threads=tf.train.start_queue_runners(sess=sess, coord=coord)


    for i in range(3):
        print(sess.run([image_batch, label_batch, pixels_batch]))


    coord.request_stop()
    coord.join(threads)

猜你喜欢

转载自www.cnblogs.com/devilmaycry812839668/p/12749799.html