Tensorflow基础:统一的数据存储格式
2017-10-13 10:27
363 查看
在很多图像识别问题中,图像的亮度、对比度等属性都不应该影响最后的识别结果。本文将介绍如何对图像数据进行预处理使训练得到的神经网络模型尽可能小地被无关因素所影响。
有些程序中,使用了一个从类别名称到所有数据列表的词典来维护图像和类别的关系。这种方式的可扩展性非常差,当数据来源更加复杂、每一个样例中的信息更加丰富之后,这种方式就很难有效地记录输入数据中的信息了。于是Tensorflow提供了TFRecord的格式来统一存储数据。
TFRecord输入数据格式
来自实际问题的数据往往有很多格式和属性,TFRecord格式可以统一不同的原始数据格式,并更加有效地管理不同的属性。有些程序中,使用了一个从类别名称到所有数据列表的词典来维护图像和类别的关系。这种方式的可扩展性非常差,当数据来源更加复杂、每一个样例中的信息更加丰富之后,这种方式就很难有效地记录输入数据中的信息了。于是Tensorflow提供了TFRecord的格式来统一存储数据。
TFRecord格式介绍
TFRecord文件中的数据都是通过tf.train.Example Protocol Buffer的格式存储的。以下代码给出了tf.train.Example的定义:message Example{ Features features = 1 }; message Features{ map<string, Feature>feature = 1; }; message Feature{ oneof kind{ BytesList bytes_list = 1; FloatList float_list = 2; Int64List int64_list = 3; } };
TFRecord样例程序
这里将给出具体地样例程序来读写TFRecord文件。下面的程序给出了如何将MNIST输入数据转化为TFRecord的格式:import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import numpy as np 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("E:\科研\TensorFlow教程\MNIST_data", dtype=tf.uint8, one_hot=True) images = mnist.train.images labels = mnist.train.labels pixels = images.shape[1] num_examples = mnist.train.num_examples filename = "./output.tfrecords" writer = tf.python_io.TFRecordWriter(filename) for index in range(num_examples): image_raw = images[index].tostring() 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) })) writer.write(example.SerializeToString()) writer.close()
读取TFRecord文件
Tensorflow对从文件列表中读取数据提供了很好的支持。以下程序给出了如何读取TFRecord文件中的数据:import tensorflow as tf reader = tf.TFRecordReader() 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), 'pixels': tf.FixedLenFeature([], tf.int64), 'label': tf.FixedLenFeature([], tf.int64) } ) images = tf.decode_raw(features['image_raw'], tf.uint8) labels = tf.cast(features['label'], tf.int32) pixels = tf.cast(features['pixels'], tf.int32) sess = tf.Session() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(10): image, label, pixel = sess.run([images, labels, pixels])
相关文章推荐
- TFRecord —— tensorflow 下的统一数据存储格式
- Python 数据分析(一) 本实验将学习 pandas 基础,数据加载、存储与文件格式,数据规整化,绘图和可视化的知识
- Python 数据分析(一) 本实验将学习 pandas 基础,数据加载、存储与文件格式,数据规整化,绘图和可视化的知识
- Oracle基础数据的存储格式
- Hadoop基础教程-第11章 Hive:SQL on Hadoop(11.4 数据类型和存储格式)(草稿)
- Android游戏开发基础part9--游戏数据存储
- Android如何把json格式的数据存储到xml中
- android基础---使用文件进行数据存储
- 统一前后台数据交互格式
- 统一存储:数据存储领域中的另一股力量
- YUV422数据格式和存储
- python多种格式数据加载、处理与存储
- Core Data数据持久性存储基础教程-备用
- python数据分析之:数据加载,存储与文件格式
- 驰骋工作流引擎 - 表单数据存储格式
- C语言实现:1、实现文本文件内数据格式的统一;2、实现文本文件导入MySQL数据库。
- Android基础_数据存储一_文件存储
- 利用存储过程实现交叉表格式数据查询的一种通用方法
- Hive数据类型与文件存储格式
- 利用Python进行数据分析--数据加载、存储与文件格式