tensorflow学习笔记(10) mnist格式数据转换为TFrecords
2017-11-21 14:37
591 查看
本程序
(1)mnist的图片转换成TFrecords格式
(2) 读取TFrecords格式
(1)mnist的图片转换成TFrecords格式
(2) 读取TFrecords格式
# coding:utf-8 # 将MNIST输入数据转化为TFRecord的格式 # http://blog.csdn.net/u014182497/article/details/74376224 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import numpy as np from PIL import Image #把传入的value转化为整数型的属性,int64_list对应着 tf.train.Example 的定义 def _int64_feature(value): return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) #把传入的value转化为字符串型的属性,bytes_list对应着 tf.train.Example 的定义 def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) def genmnsit_tfreords(): #读取MNIST数据 mnist = input_data.read_data_sets("./mnist_data", dtype=tf.uint8, one_hot=True) #训练数据的图像,可以作为一个属性来存储 images = mnist.train.images #训练数据所对应的正确答案,可以作为一个属性来存储 labels = mnist.train.labels #训练数据的图像分辨率,可以作为一个属性来存储 pixels = images.shape[0] #训练数据的个数 num_examples = mnist.train.num_examples #指定要写入TFRecord文件的地址 filename = "./output.tfrecords" #创建一个write来写TFRecord文件 writer = tf.python_io.TFRecordWriter(filename) for index in range(num_examples): #把图像矩阵转化为字符串 image_raw = images[index].tostring() #将一个样例转化为Example Protocol Buffer,并将所有的信息写入这个数据结构 example = tf.train.Example(features=tf.train.Features(feature={ #'pixels': _int64_feature(pixels), 'label': _int64_feature(np.argmax(labels[index])), 'image_raw': _bytes_feature(image_raw)})) #将 Example 写入TFRecord文件 writer.write(example.SerializeToString()) writer.close() #读取TFRecord文件中的数据 def read_tfrecords(): #创建一个reader来读取TFRecord文件中的样例 reader = tf.TFRecordReader() #通过 tf.train.string_input_producer 创建输入队列 filename_queue = tf.train.string_input_producer(["./output.tfrecords"]) #从文件中读取一个样例 _, serialized_example = reader.read(filename_queue) #解析读入的一个样例 features = tf.parse_single_example( serialized_example, features={ #这里解析数据的格式需要和上面程序写入数据的格式一致 'image_raw': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64), }) #tf.decode_raw可以将字符串解析成图像对应的像素数组 images = tf.decode_raw(features['image_raw'], tf.uint8) images = tf.reshape(images, [28, 28, 1]) #tf.cast可以将传入的数据转化为想要改成的数据类型 labels = tf.cast(features['label'], tf.int32) sess = tf.Session() #启动多线程处理输入数据 coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) num_preprocess_threads = 1 batch_size = 1 min_queue_examples = 50 images_batch, label_batch = tf.train.shuffle_batch( [images, labels], batch_size=batch_size, num_threads=num_preprocess_threads, capacity=min_queue_examples + 3 * batch_size, min_after_dequeue=min_queue_examples) image = tf.reshape(images_batch, [28, 28]) with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) # 每次运行可以读取TFRecord文件中的一个样例。当所有样例都读完之后,再次样例中的程序会重头读取 for i in range(5): data, label = sess.run([image, label_batch]) result = Image.fromarray(data) result.save(str(i) + '.png') pass pass coord.request_stop() coord.join(threads) if __name__ == '__main__': genmnsit_tfreords() read_tfrecords()
相关文章推荐
- Windows Caffe中MNIST数据格式转换实现
- Windows Caffe中MNIST数据格式转换实现
- R语言数据格式 长数据 和 宽数据 之间的转换
- JS对象与JSON格式数据相互转换
- 后台传送时间格式数据json转换
- Caffe下将mnist手写图片数据转化成lmdb格式
- spring MVC 返回数据自动转换为json格式
- 数据库中查询出来的数据转换成json格式
- 浅析json与jsonp区别及通过ajax获得json数据后格式的转换
- 在Salesforce中将 Decimal 数据转换成美元格式
- 将数据转换为json格式代码
- 取模数据格式转换源码 宽高8*16 低位在前 第二字节由右边改为下边
- php从数据库中取数据转换为json格式
- MATLAB把ISE中产生的1QN格式的十六进制数据转换为十进制数进行分析
- 把含一字节变量的16进制格式数据转换为字符串显示
- 10_基本数据类型自动转换_强制转型_类型提升问题
- ABAP和XML数据格式互相转换的两种方式
- Python将JSON格式数据转换为SQL语句以便导入MySQL数据库
- Java中将JSON格式的数据转换成对应的Bean、Map、List数据
- 10-10C#基础---数据类型之间的转换