【深度学习】TensorFlow的TFRecord存储
2018-01-17 19:08
369 查看
TFRecord:TensorFlow提供一种统一的格式来存储数据
存储的格式为属性名到取值的字典,属性名为字符串,取值可为字符串、实数列表、整数列表。
将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数据。
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[1]
num_examples = mnist.train.num_examples
# 输出TFRecord文件的地址。
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()
print ("TFRecord文件已保存。")
读取TFRecord数据程序:
import tensorflow as tf
# 读取文件。创建一个 reader 读取 TFRecord
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])
存储的格式为属性名到取值的字典,属性名为字符串,取值可为字符串、实数列表、整数列表。
将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数据。
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[1]
num_examples = mnist.train.num_examples
# 输出TFRecord文件的地址。
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()
print ("TFRecord文件已保存。")
读取TFRecord数据程序:
import tensorflow as tf
# 读取文件。创建一个 reader 读取 TFRecord
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])
相关文章推荐
- TensorFlow学习记录-- 7.TensorFlow高效读取数据之tfrecord详细解读
- 深度学习笔记——深度学习框架TensorFlow(四)[高级API tf.contrib.learn]
- 学习笔记TF045:人工智能、深度学习、TensorFlow、比赛、公司
- 深入浅出的TensorFlow数据格式化存储工具TFRecord用法教程
- TensorFlow入门深度学习–09.tf.contrib.slim用法详解
- Tensorflow实战学习(四十二)【TF.Learn、分布式Estimator、深度学习Estimator】
- Tensorflow实战学习(十六)【CNN实现、数据集、TFRecord、加载图像、模型、训练、调试】
- TFRecord —— tensorflow 下的统一数据存储格式
- 深度学习笔记——深度学习框架TensorFlow(九)[Building Input Functions with tf.contrib.learn]
- 深度学习之TFRecord数据集读、写的制作、读取及验证 具体操作过程
- 深度学习笔记——深度学习框架TensorFlow(十)[Creating Estimators in tf.contrib.learn]
- 学习笔记TF049:TensorFlow 模型存储加载、队列线程、加载数据、自定义操作
- Tensorflow学习笔记-通过slim读取TFRecord文件
- 学习笔记TF049:TensorFlow 模型存储加载、队列线程、加载数据、自定义操作
- 学习笔记TF045:人工智能、深度学习、TensorFlow、比赛、公司
- TensorFlow 学习(二) 制作自己的TFRecord数据集,读取,显示及代码详解
- Tensorflow教程学习笔记(一)----将自己的数据集转换成TFRecord
- TF之NN:matplotlib动态演示深度学习之tensorflow将神经网络系统自动学习并优化修正并且将输出结果可视化—Jason niu
- 深度学习笔记——深度学习框架TensorFlow(八)[Logging and Monitoring Basics with tf.contrib.learn]
- TensorFlow学习(十一):保存TFRecord文件