首页 > 技术文章 > 制作 tensorflow record 文件

wdmx 2018-12-19 13:43 原文

 

 http://warmspringwinds.github.io/tensorflow/tf-slim/2016/12/21/tfrecords-guide/

https://medium.com/mostly-ai/tensorflow-records-what-they-are-and-how-to-use-them-c46bc4bbb564

https://medium.com/ymedialabs-innovation/how-to-use-tfrecord-with-datasets-and-iterators-in-tensorflow-with-code-samples-ffee57d298af

http://leix.me/2017/01/09/tensorflow-practical-guides/#非序列数据

tensorflow 的 record 文件生成

 

FastGFile 这个功能已被弃用。它将在未来版本中删除。更新说明:使用tf.gfile.GFile。

 对于tf.read_file 和 tf.write_file 两个操作,是tensorflow 图体系中的正常节点.

 对于 tf.gfile 更像是本地的文件及其文件夹操作。

 

 看看,如何解析tensorflow records 不难发现,特征的数值使用一个列表来装,列表不能嵌套,其元素是int,float,string 三种类型!

解析的时候,若按照固定长度的特征解析,shape参数的值需要与写入的特征的长度一致,若灭有值的话可以设置默认值,但就是不能长度不一致

否则需要使用变长特征来解析。

 1 f1=tf.train.Feature(int64_list=tf.train.Int64List(value=[13,12]))
 2 f2=tf.train.Feature(int64_list=tf.train.Int64List(value=[12]))
 3 f3=tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"abcde",b"abcde"]))
 4 
 5 example = tf.train.Example(features=_make_named_features({"f1":f1,"f2":f2,"f3":f3}))
 6 
 7 #f1=tf.train.Feature(int64_list=tf.train.Int64List(value=[]))
 8 f2=tf.train.Feature(int64_list=tf.train.Int64List(value=[22]))
 9 f3=tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"cde",b"abcde"]))
10 
11 example2 = tf.train.Example(features=_make_named_features({"f2":f2,"f3":f3}))
12 
13 _features = {
14     'f1': tf.FixedLenFeature([2], dtype=tf.int64,default_value=[3,5]),
15     'f2': tf.FixedLenFeature([1], dtype=tf.int64),
16     'f3': tf.FixedLenFeature([2], dtype=tf.string),
17 }

 

f1=tf.train.Feature(int64_list=tf.train.Int64List(value=[12]))
f2=tf.train.Feature(int64_list=tf.train.Int64List(value=[34]))
f3=tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"abcde",b"abcde"]))

example = tf.train.Example(features=_make_named_features({"f1":f1,"f2":f2,"f3":f3}))

f1=tf.train.Feature(int64_list=tf.train.Int64List(value=[45,12]))
f2=tf.train.Feature(int64_list=tf.train.Int64List(value=[34]))
f3=tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"cde",b"abcde"]))

example2 = tf.train.Example(features=_make_named_features({"f1":f1,"f2":f2,"f3":f3}))

_features = {
'f1': tf.VarLenFeature(dtype=tf.int64),
'f2': tf.FixedLenFeature([1], dtype=tf.int64),
'f3': tf.FixedLenFeature([2], dtype=tf.string),
}

 

 

 

推荐阅读