python文件怎么看代码表_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)
}
return tf.parse_single_example(example_proto, features)
# TFRecord數據按照Feature解析出對應的真實數據
ds = ds.map(lambda x : _parse_image(x), num_parallel_calls=4)
上面是一個標準的TFRecord數據的寫入和讀取部分過程,大家應該發現了,讀取TFRecord數據的時候,得知道TFRecord數據保存的屬性名和類型,任何一項不匹配,都會導致無法獲取數據。
如果數據的寫入和讀取都是自己一個人完成,那就沒問題。但是如果寫入和讀取是跨團隊合作時候,如果每次讀取數據都得讓對方給完整的屬性名和屬性類型,那效率就太低了。畢竟TFRecord數據已經包含了一切,自己動手豐衣足食。
那么怎么查看TFRecord數據呢?使用python tf.train.Example.FromString(serialized_example)方法,方法的入參是TFRecord包含的數據字符串。
然后,我直接將上訴查看的過程寫成了一個py腳本,需要自取。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import tensorflow as tf
# 用法:python trackTFRecord.py True file1 file2
# trackTFRecord.py 就是當前這個py文件
# True 表示是否輸出具體的數據
# file1 file2 表示的是需要查看的TFRecord文件的絕對路徑
# 輸出說明:tf.float32對應TFRecord的FloatList,tf.int64對應Int64List,tf.string對應BytesList
def main():
print('TFRecord文件個數為{0}個'.format(len(sys.argv)-2))
for i in range(2, len(sys.argv)):
filepath = sys.argv[i]
with tf.Session() as sess:
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))
for key in features.feature:
feature = features.feature[key]
ftype = None
fvalue = None
if len(feature.bytes_list.value) > 0:
ftype = 'bytes_list'
fvalue = feature.bytes_list.value
if len(feature.float_list.value) > 0:
ftype = 'float_list'
fvalue = feature.float_list.value
if len(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數據
import tensorflow as tf
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("finish to write data to tfrecord file!")
然后,使用上面的腳本看下這個TFRecord數據定義了哪些屬性,以及對應的格式,先進入到腳本的目錄下,因為圖像數據內容太大,影響閱讀,就只看屬性名和type了:
python trackTFRecord.py False /Users/zhanhaitao/Desktop/a.tfrecord
# 結果,其中bytes_list對應tf.string,int64_list對應tf.int64 float_list對應tf.float32
# image_raw : bytes_list
# width : int64_list
# float_val : float_list
# height : int64_list
以上這篇TFRecord文件查看包含的所有Features代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持我們。
本文標題: TFRecord文件查看包含的所有Features代碼
本文地址: http://www.cppcns.com/jiaoben/python/300692.html
總結
以上是生活随笔為你收集整理的python文件怎么看代码表_TFRecord文件查看包含的所有Features代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: W3C 验证的是是非非
- 下一篇: python系统命令切换目录_Windo