TFRecord

  • 1. tf.train.Example
    • 1.1 tfrecord 数据范式转化
    • 1.2 demo 数据集构建
  • 2. TFRecord 读写
    • 2.1 写入1-tf.io.TFRecordWriter()
    • 2.3 读取-tf.data.TFRecordDataset()
    • 2.3 data -> dataset -> 存储-tf.data.experimental.TFRecordWriter()

tfrecord 用于存储二进制序列数据的一种范式,按顺序存,按顺序取。里面存的每一条数据都是一个 byte-string, 最常用的转byte-string的方式是tf.train.Example 。tf.train.Example (or protobuf) 以字典{“string”: value}的形式存储消息,这种消息存储机制可读性高。

demo1–tfrecord存储

value can be a num / list / array
pybyte_value = np.array(value).tobytes()                     # 0.转Python字节数据
tfbyte_value = tf.train.BytesList(value=[pybyte_value])      # 1.转tf.train 字节数据
feature_dict[key] = tf.train.Feature(bytes_list=tfbyte_value)# 2.转tf.train.Feature()注意是tf.train.Feature()没有s
..........
feature_example = tf.train.Example(features=tf.train.Features(feature=tffeature_dict))# 3.转tf.train.Example()  注意tf.train.Features()s
exmp_serial = feature_example.SerializeToString()           # 序列化feature_example tf_writer = tf.python_io.TFRecordWriter(tfrecord_path)      # 构建tf写句柄
tf_writer.write(exmp_serial)                                # 写入tf文件
tf_writer.close()                                           # 关闭句柄

np.array().tobytes()构造包含数组中原始数据的Python字节数据

1. tf.train.Example

须将用户数据转化为tfrecord 约定的格式,才能使用tfrecord 格式存储数据。

1.1 tfrecord 数据范式转化

1-> tfrecord支持写入三种格式的数据:string,int64,float32,分别通过tf.train.BytesList、tf.train.Int64List、tf.train.FloatList写入tf.train.Feature中。【就是说数据要写入tf.train.Feature前必须使用tf.train.BytesList,tf.train.Int64List,tf.train.FloatList必须使用强制类型转换】

# python 数据类型转tf.train.BytesList、tf.train.Int64List、tf.train.FloatList
# tf.train.BytesList:string、byte
# tf.train.FloatList:float (float32)、double (float64)
# tf.train.Int64List :bool、enum、int32、uint32、int64、uint64
# 强制类型转换
value = 1
value_ed = tf.train.Int64List(value=[value])

2-> tf.train.Feature 接受tf.train.BytesList、tf.train.Int64List、tf.train.FloatList 类型的数据。以下为scalar 转 tf.train.Feature 的快捷函数。 not scalar 的数据只需要用np.array().tobytes()/tf.io.serialize_tensor 转换成binary-strings,然后使用以下借口函数封装成 tf.train.Feature 即可。

# input : a scalar input
# output: tf.train.Feature
def _bytes_feature(value):           """Returns a bytes_list from a string / byte."""if isinstance(value, type(tf.constant(0))):value = value.numpy() # BytesList won't unpack a string from an EagerTensor.return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))def _float_feature(value):"""Returns a float_list from a float / double."""return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))def _int64_feature(value):"""Returns an int64_list from a bool / enum / int / uint."""return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

3->tf.train.Feature 构成特征字典 -> 特征字典 转 Features message -> Features message 转 tf.train.Example -> tf.train.Example 序列化后可以存入tfrecord 文件。【 Note that the tf.train.Example message is just a wrapper around the Features message:】

1.2 demo 数据集构建

构建一个包含10000个观测数据的数据集,每条数据包含4个特征:[bool, label_index, lable_string, random_score]

n_observations = int(1e4)              # The number of observations in the dataset.
feature0 = np.random.choice([False, True], n_observations)  #Boolean feature, encoded as False or True.
feature1 = np.random.randint(0, 5, n_observations) # Integer feature, random from 0 to 4.
strings = np.array([b'cat', b'dog', b'chicken', b'horse', b'goat']) # String feature.
feature2 = strings[feature1]
feature3 = np.random.randn(n_observations) # Float feature, from a standard normal distribution.

单个样本转tf.train.Feature-> tf.train.Features -> tf.train.Example()->SerializeToString() 接口函数

def serialize_example(feature0, feature1, feature2, feature3):#  Create a Feature dict : {key: tf.train.Feature}feature = {'feature0': _int64_feature(feature0),'feature1': _int64_feature(feature1),'feature2': _bytes_feature(feature2),'feature3': _float_feature(feature3),}#  Create a Features message and conver to tf.train.Example.example_proto = tf.train.Example(features=tf.train.Features(feature=feature))return example_proto.SerializeToString()

观测序列化[serialized_example ]和反序列化[tf.train.Example()]的结果

for i in range(n_observations):f0, f1, f2, f3 = feature0[i], feature1[i], feature2[i], feature3[i]# 序列化 tf.train.Example 消息serialized_example = serialize_example(f0, f1, f2, f3) # b'\nR\n\x14\n\x08feature2\x12\x08\n\x06\.....# 反序列化 tf.train.Exampleexample_proto = tf.train.Example.FromString(serialized_example) '''features {feature {key: "feature0"value {int64_list {value: 0}}}feature {key: "feature1"value {int64_list {value: 4}}}feature {key: "feature2"value {bytes_list {value: "goat"}}}feature {key: "feature3"value {float_list {value: 0.9876000285148621}}}
}'''

2. TFRecord 读写

tfrecord 中每一条record按照下面的范式存储。tfrecord 文件中并非只能存tf.train.Example 序列化的结果,tf.train.Example 只是将字典序列化的一种方法。任何 byte-string都能够存入TFRecord file。

uint64 length
uint32 masked_crc32_of_length
byte   data[length]
uint32 masked_crc32_of_data

2.1 写入1-tf.io.TFRecordWriter()

# Write the `tf.train.Example` observations to the file.
with tf.io.TFRecordWriter(filename) as writer:     # 获取写入句柄for i in range(n_observations):example = serialize_example(feature0[i], feature1[i], feature2[i], feature3[i])writer.write(example)

2.3 读取-tf.data.TFRecordDataset()

# 读取tfrecord文件, 获取序列化的样本
filenames = [filename]
raw_dataset = tf.data.TFRecordDataset(filenames)  # tf.data.Dataset 对象
for raw_record in raw_dataset.take(10):           # 读取前10 条print(repr(raw_record))                         # raw_record序列化的样本# 序列化样本反序列化
# tf.data.Dataset 在图中执行,feature_description能够建立数据集shape和type的signature。
feature_description = {'feature0': tf.io.FixedLenFeature([], tf.int64, default_value=0),'feature1': tf.io.FixedLenFeature([], tf.int64, default_value=0),'feature2': tf.io.FixedLenFeature([], tf.string, default_value=''),'feature3': tf.io.FixedLenFeature([], tf.float32, default_value=0.0),
}
def _parse_function(example_proto):# Parse the input `tf.train.Example` proto using the dictionary above.# 一次只解析一条数据: use tf.parse example 可以一次解析一个batch的数据return tf.io.parse_single_example(example_proto, feature_description)# 利用tf.data.Dataset.map 函数将_parse_function 应用于数据集raw_dataset中的每一个元素parsed_dataset = raw_dataset.map(_parse_function)      # 可以用的数据
# {'feature0': <tf.Tensor: shape=(), dtype=int64, numpy=0>, 'feature1': <tf.Tensor: shape=(), dtype=int64, numpy=4>, 'feature2': <tf.Tensor: shape=(), dtype=string, numpy=b'goat'>, 'feature3': <tf.Tensor: shape=(), dtype=float32, numpy=0.5251196>}
# 读取
filenames = [filename]
raw_dataset = tf.data.TFRecordDataset(filenames)
# tf.train.Example.ParseFromString反序列化 得到的是tf.train.Example features, 很难直接使用
for raw_record in raw_dataset.take(1):example = tf.train.Example()example.ParseFromString(raw_record.numpy())
# tf.train.Example features 转 dict of numpy array
result = {}
for key, feature in example.features.feature.items():# The values are the Feature objects which contain a `kind` which contains:# one of three fields: bytes_list, float_list, int64_listkind = feature.WhichOneof('kind')result[key] = np.array(getattr(feature, kind).value)

2.3 data -> dataset -> 存储-tf.data.experimental.TFRecordWriter()

from_tensor_slices 将data 转成dataset-> 序列化dataset 中的每一个元素-> 存入tf record 文件

features_dataset = tf.data.Dataset.from_tensor_slices((feature0, feature1, feature2, feature3))
for f0,f1,f2,f3 in features_dataset.take(1):    # 逐个获取数据print(f0, f1, f2, f3)# tf.Tensor(False, shape=(), dtype=bool),tf.Tensor(4, shape=(), dtype=int64),tf.Tensor(b'goat', shape=(), dtype=string),tf.Tensor(0.5251196235602504, shape=(), dtype=float64)# 序列化方式1:tf.data.Dataset.map 映射数据集中的每一个元素
# 对于自定义的序列化操作函数serialize_example。为了使其成为TensorFlow graph 的节点,须使用 tf.py_function封装;之后再使用tf.data.Dataset.map 映射序列化数据集中的每一个元素。
def tf_serialize_example(f0,f1,f2,f3):# (自定义函数,函数输入,函数输出)tf_string = tf.py_function(serialize_example,(f0, f1, f2, f3),tf.string)return tf.reshape(tf_string, ()) # The result is a scalar.
serialized_features_dataset = features_dataset.map(tf_serialize_example)# 序列化方式2:tf.data.Dataset.from_generator()映射数据集中的每一个元素
def generator():for features in features_dataset:yield serialize_example(*features)
serialized_features_dataset = tf.data.Dataset.from_generator(generator, output_types=tf.string, output_shapes=())

整个序列化的数据集写入tfrecord.

# 整个写入tfrecord
filename = 'test.tfrecord'
writer = tf.data.experimental.TFRecordWriter(filename)    # 与1.0 的接口有些不太一样
writer.write(serialized_features_dataset)

参考资料:TFRecord and tf.train.Example

TensorFlow(4)-TFRecord相关推荐

  1. Tensorflow之TFRecord的原理和使用心得

    作者|对白 出品|公众号:对白的算法屋 大家好,我是对白. 目前,越来越多的互联网公司内部都有自己的一套框架去训练模型,而模型训练时需要的数据则都保存在分布式文件系统(HDFS)上.Hive作为构建在 ...

  2. 【TensorFlow】TFRecord数据集的制作:读取、显示及代码详解

    在跑通了官网的mnist和cifar10数据之后,笔者尝试着制作自己的数据集,并保存,读入,显示. TensorFlow可以支持cifar10的数据格式, 也提供了标准的TFRecord 格式. te ...

  3. tensorflow之tfrecord

    转自下面的 https://blog.csdn.net/happyhorizion/article/details/77894055 https://blog.csdn.net/sinat_29957 ...

  4. [Tensorflow]关于TFRecord和tf.Example的使用

     为了高效地读取数据,可以将数据进行序列化存储,这样也便于网络流式读取数据.TFRecord是一种比较常用的存储二进制序列数据的方法,基于Google的Protocol buffers格式的数据.   ...

  5. tensorflow 使用 TFRecord 读取图片(二)

    import os import tensorflow as tf import cv2 import random import numpy as nprandom.seed(0) 获取所有图片及标 ...

  6. TensorFlow 之 TFRecord

    文章目录 为什么每个TensorFlow开发人员都应该了解TFRecord! 什么是TFRecord? 让我们看看代码之间的区别 - Naive vs Tfrecord Naive普通方式 Tfrec ...

  7. tensorflow中tfrecord数据操作

    前言: 为了更加展示tfrecord数据的相关操作,笔者后续又写了一个实践的简单例子进一步解释,具体可以看: TFrecords 制作数据集小例子(多标签)_爱吃火锅的博客-CSDN博客 正文: tf ...

  8. TensorFlow学习——TFRecord格式转换

    TFRecord TensorFlow提供了TFRecord的格式来统一存储数据,TFRecord格式是一种将图像数据和标签放在一起的二进制文件,能更好的利用内存,在tensorflow中快速的复制, ...

  9. Tensorflow—TFRecord文件生成与读取

    Tensorflow-TFRecord文件生成与读取 微信公众号:幼儿园的学霸 个人的学习笔记,关于OpenCV,关于机器学习, -.问题或建议,请公众号留言; 目录 文章目录 Tensorflow- ...

最新文章

  1. Linux之绝对路径和相对路径
  2. 【译】IPFS — The Permanent, Distributed Web Continues…..
  3. 算法竞赛入门经典 写题笔记(第五章 图论算法与模型2)
  4. c#中跨线程调用windows窗体控件 .我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题。然而我们并不能用传统方法来做这个问题,下面我将详细的介绍。...
  5. 任正非就注册姚安娜商标道歉;人人影视字幕组因盗版被查;JIRA、Confluence 等产品本月停售本地化版本 | 极客头条...
  6. mysql 访问速度_操作mysql第一次访问速度慢(远程)
  7. AD画电路的踩坑点——继电器线圈的正负性以G6K-2F-Y为例
  8. eNSP华为路由器与交换机连接
  9. JavaScript 每日一题 #9
  10. 畅购商城(三):商品管理
  11. Unity VR:如何修改fbx动画
  12. C语言布斯乘法算法,布斯Booth算法带符号位的乘法verilog语言实现booth算法
  13. mysql时间和时间戳转换工具_时间戳转换工具
  14. 【每日早报】2019/06/25
  15. 2019年大厂面试题合集:Java架构师技术栈为什么竞争越来越激烈?程序员必看!
  16. 对于雷诺数,你了解多少?
  17. SVM支持向量机的应用
  18. Adobe新漏洞 金山清理专家急推专补工具
  19. 西门子SMART,模拟量滤波,消抖子程序,能实现电流电压和热电阻模拟量信号的采集
  20. 东方Project题目 1975 红魔馆爆炸了

热门文章

  1. 基于Asterisk的VoIP开发指南(2)——Asterisk AGI程序编写指南
  2. DMA及cache一致性的学习心得
  3. mysql查当前用户的的命令_mysql命令大全用户管理相关命令
  4. html怎么导入js编辑,three.js怎么导入html
  5. 【转】解密Qt安装目录的结构
  6. 【转】应用架构之道:分离业务逻辑和技术细节
  7. 【转】C#搭建Oauth2.0认证流程以及代码示例
  8. ASP.Net请求处理机制初步探索之旅 - Part 2 核心
  9. ABP入门系列(10)——扩展AbpSession
  10. iar stm32_树莓派玩转STM32开发(一)——介绍篇