TFRecord文件查看包含的所有Features代码
TFRecord作为tensorflow中广泛使用的数据格式,它跨平台,省空间,效率高。因为Tensorflow开发者众多,统一训练时数据的文件格式是一件很有意义的事情,也有助于降低学习成本和迁移成本。
但是TFRecord数据是二进制格式,没法直接查看。因此,如何能够方便的查看TFRecord格式和数据,就显得尤为重要了。
为什么需要查看TFReocrd数据?首先我们先看下常规的写入和读取TFRecord数据的关键过程。
#1.写入过程
#一张图片,我写入了其内容,label,长和宽几个信息
tf_example=tf.train.Example(
features=tf.train.Features(feature={
'encoded':bytes_feature(encoded_jpg),
'label':int64_feature(label),
'height':int64_feature(height),
'width':int64_feature(width)}))
#2.读取过程
#定义解析的TFRecord数据格式
def_parse_image(example_proto):
features={'encoded':tf.FixedLenFeature((),tf.string),
'label':tf.FixedLenFeature((),tf.int64),
'height':tf.FixedLenFeature((),tf.int64),
'width':tf.FixedLenFeature((),tf.int64)
}
returntf.parse_single_example(example_proto,features)
#TFRecord数据按照Feature解析出对应的真实数据
ds=ds.map(lambdax:_parse_image(x),num_parallel_calls=4)
上面是一个标准的TFRecord数据的写入和读取部分过程,大家应该发现了,读取TFRecord数据的时候,得知道TFRecord数据保存的属性名和类型,任何一项不匹配,都会导致无法获取数据。
如果数据的写入和读取都是自己一个人完成,那就没问题。但是如果写入和读取是跨团队合作时候,如果每次读取数据都得让对方给完整的属性名和属性类型,那效率就太低了。毕竟TFRecord数据已经包含了一切,自己动手丰衣足食。
那么怎么查看TFRecord数据呢?使用pythontf.train.Example.FromString(serialized_example)方法,方法的入参是TFRecord包含的数据字符串。
然后,我直接将上诉查看的过程写成了一个py脚本,需要自取。
#!/usr/bin/python
#-*-coding:utf-8-*-
importsys
importtensorflowastf
#用法:pythontrackTFRecord.pyTruefile1file2
#trackTFRecord.py就是当前这个py文件
#True表示是否输出具体的数据
#file1file2表示的是需要查看的TFRecord文件的绝对路径
#输出说明:tf.float32对应TFRecord的FloatList,tf.int64对应Int64List,tf.string对应BytesList
defmain():
print('TFRecord文件个数为{0}个'.format(len(sys.argv)-2))
foriinrange(2,len(sys.argv)):
filepath=sys.argv[i]
withtf.Session()assess:
filenames=[filepath]
#加载TFRecord数据
ds=tf.data.TFRecordDataset(filenames)
ds=ds.batch(10)
ds=ds.prefetch(buffer_size=tf.contrib.data.AUTOTUNE)
iterator=ds.make_one_shot_iterator()
#为了加快速度,仅仅简单拿一组数据看下结构
batch_data=iterator.get_next()
res=sess.run(batch_data)
serialized_example=res[0]
example_proto=tf.train.Example.FromString(serialized_example)
features=example_proto.features
print('{0}信息如下:'.format(filepath))
forkeyinfeatures.feature:
feature=features.feature[key]
ftype=None
fvalue=None
iflen(feature.bytes_list.value)>0:
ftype='bytes_list'
fvalue=feature.bytes_list.value
iflen(feature.float_list.value)>0:
ftype='float_list'
fvalue=feature.float_list.value
iflen(feature.int64_list.value)>0:
ftype='int64_list'
fvalue=feature.int64_list.value
result='{0}:{1}'.format(key,ftype)
if'True'==sys.argv[1]:
result='{0}:{1}'.format(result,fvalue)
print(result)
if__name__=="__main__":
main()
下面给大家实例演示,首先先随便找个图片,写入到TFRecord数据
importtensorflowastf
filename="/Users/zhanhaitao/Desktop/1.png"
#使用tf.read_file读进图片数据
image=tf.read_file(filename)
#主要是为了获取图片的宽高
image_jpeg=tf.image.decode_jpeg(image,channels=3,name="decode_jpeg_picture")
#reshape图片到原始大小2500x2000x3
image_jpeg=tf.reshape(image_jpeg,shape=(2500,2000,3))
#获取图片shape数据
img_shape=image_jpeg.shape
width=img_shape[0]
height=img_shape[1]
#将原图片tensor生成bytes对象,image将保存到tfrecord
sess=tf.Session()
image=sess.run(image)
sess.close()
#定义TFRecords文件的保存路径及其文件名
path_none="/Users/zhanhaitao/Desktop/a.tfrecord"
#定义不同压缩选项的TFRecordWriter
writer_none=tf.python_io.TFRecordWriter(path_none,options=None)
#将外层features生成特定格式的example
example_none=tf.train.Example(features=tf.train.Features(feature={
"float_val":tf.train.Feature(float_list=tf.train.FloatList(value=[9.99])),
"width":tf.train.Feature(int64_list=tf.train.Int64List(value=[width])),
"height":tf.train.Feature(int64_list=tf.train.Int64List(value=[height])),
"image_raw":tf.train.Feature(bytes_list=tf.train.BytesList(value=[image]))
}))
#example系列化字符串
example_str_none=example_none.SerializeToString()
#将系列化字符串写入协议缓冲区
writer_none.write(example_str_none)
#关闭TFRecords文件操作接口
writer_none.close()
print("finishtowritedatatotfrecordfile!")
然后,使用上面的脚本看下这个TFRecord数据定义了哪些属性,以及对应的格式,先进入到脚本的目录下,因为图像数据内容太大,影响阅读,就只看属性名和type了:
pythontrackTFRecord.pyFalse/Users/zhanhaitao/Desktop/a.tfrecord #结果,其中bytes_list对应tf.string,int64_list对应tf.int64float_list对应tf.float32 #image_raw:bytes_list #width:int64_list #float_val:float_list #height:int64_list
以上这篇TFRecord文件查看包含的所有Features代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。