使用TFRecord存取多个数据案例
作者:gaoyueace 时间:2023-07-14 22:26:34
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 = 1
Int64List int64_list = 1
}
}
可以看出Example是一个嵌套的数据结构,其中属性名称可以为一个字符串,其取值可以是字符串BytesList、实数列表FloatList或整数列表Int64List。
将数据转化为TFRecord格式
以下代码是将MNIST输入数据转化为TFRecord格式:
# -*- coding: utf-8 -*-
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 _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
#若想保存为数组,则要改成value=value即可
# 生成字符串型的属性
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
mnist = input_data.read_data_sets("/tensorflow_google", dtype=tf.uint8, one_hot=True)
images = mnist.train.images
# 训练数据所对应的正确答案,可以作为一个属性保存在TFRecord中
labels = mnist.train.labels
# 训练数据的图像分辨率,这可以作为Example中的一个属性
pixels = images.shape[1]
num_examples = mnist.train.num_examples
# 输出TFRecord文件的地址
filename = "/tensorflow_google/mnist_output.tfrecords"
# 创建一个writer来写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()
本程序将MNIST数据集中所有的训练数据存储到了一个TFRecord文件中,若数据量较大,也可以存入多个文件。
从TFRecord文件中读取数据
以下代码可以从上面代码中的TFRecord中读取单个或多个训练数据:
# -*- coding: utf-8 -*-
import tensorflow as tf
# 创建一个reader来读取TFRecord文件中的样例
reader = tf.TFRecordReader()
# 创建一个队列来维护输入文件列表
filename_queue = tf.train.string_input_producer(["/Users/gaoyue/文档/Program/tensorflow_google/chapter7"
"/mnist_output.tfrecords"])
# 从文件中读出一个样例,也可以使用read_up_to函数一次性读取多个样例
# _, serialized_example = reader.read(filename_queue)
_, serialized_example = reader.read_up_to(filename_queue, 6) #读取6个样例
# 解析读入的一个样例,如果需要解析多个样例,可以用parse_example函数
# features = tf.parse_single_example(serialized_example, features={
# 解析多个样例
features = tf.parse_example(serialized_example, features={
# TensorFlow提供两种不同的属性解析方法
# 第一种是tf.FixedLenFeature,得到的解析结果为Tensor
# 第二种是tf.VarLenFeature,得到的解析结果为SparseTensor,用于处理稀疏数据
# 解析数据的格式需要与写入数据的格式一致
'image_raw': tf.FixedLenFeature([], tf.string),
'pixels': tf.FixedLenFeature([], tf.int64),
'label': tf.FixedLenFeature([], tf.int64),
})
# tf.decode_raw可以将字符串解析成图像对应的像素数组
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)
# 每次运行可以读取TFRecord中的一个样例,当所有样例都读完之后,会重头读取
# for i in range(10):
# image, label, pixel = sess.run([images, labels, pixels])
# # print(image, label, pixel)
# print(label, pixel)
# 读取TFRecord中的前6个样例,若加入循环,则会每次从上次输出的地方继续顺序读6个样例
image, label, pixel = sess.run([images, labels, pixels])
print(label, pixel)
sess.close()
>> [7 3 4 6 1 8] [784 784 784 784 784 784]
输出结果显示,从TFRecord文件中顺序读出前6个样例。
来源:https://blog.csdn.net/gaoyueace/article/details/79271747
标签:TFRecord,存取,数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python catplot函数自定义颜色的方法
2021-01-29 16:25:01
![](https://img.aspxhome.com/file/2023/1/95491_0s.png)
SQL Server2019安装后出现连接失败的解决方法
2024-01-23 05:16:24
![](https://img.aspxhome.com/file/2023/7/94437_0s.png)
《HTML5设计原理》读后随记
2011-01-25 12:26:00
swfobject2.1居中问题
2008-12-15 17:18:00
微信小程序跳一跳游戏 python脚本跳一跳刷高分技巧
2023-01-31 22:30:44
![](https://img.aspxhome.com/file/2023/7/66807_0s.png)
利用Python校准本地时间的方法教程
2021-06-24 06:51:41
![](https://img.aspxhome.com/file/2023/6/95796_0s.jpg)
layui 富文本图片上传接口与普通按钮 文件上传接口的例子
2024-05-22 10:36:28
C#命名空间System.ComponentModel属性方法汇总
2024-06-05 09:24:02
![](https://img.aspxhome.com/file/2023/3/122833_0s.jpg)
js实现的捐赠管理完整实例
2023-08-22 05:25:14
数字人组件反写[asp组件开发实例2]
2009-06-09 13:15:00
![](https://img.aspxhome.com/file/UploadPic/20093/9/200939182821210s.gif)
Python爬取豆瓣视频信息代码实例
2021-10-28 06:41:46
![](https://img.aspxhome.com/file/2023/3/84993_0s.jpg)
远古幻想ICON 1套+创作思路
2007-09-30 20:33:00
![](https://img.aspxhome.com/file/UploadPic/20079/30/2007930203644880s.jpg)
Python随机生成带特殊字符的密码
2021-10-07 02:48:05
python 获取毫秒数,计算调用时长的方法
2021-10-13 02:30:12
mysql慢查询使用详解
2024-01-25 05:07:18
![](https://img.aspxhome.com/file/2023/5/118455_0s.jpg)
NCCL深度学习Bootstrap网络连接建立源码解析
2022-02-25 22:28:10
![](https://img.aspxhome.com/file/2023/3/82823_0s.jpg)
MySQL最基本的命令使用汇总
2024-01-28 06:18:30
对Python的Django框架中的项目进行单元测试的方法
2021-02-23 03:17:04
利用Python实现定时程序的方法
2021-04-16 08:28:04
CSS应用的必要步骤:样式重设
2008-06-11 13:29:00