您好,登錄后才能下訂單哦!
TFRecord作為tensorflow中廣泛使用的數(shù)據(jù)格式,它跨平臺(tái),省空間,效率高。因?yàn)?Tensorflow開發(fā)者眾多,統(tǒng)一訓(xùn)練時(shí)數(shù)據(jù)的文件格式是一件很有意義的事情,也有助于降低學(xué)習(xí)成本和遷移成本。
但是TFRecord數(shù)據(jù)是二進(jìn)制格式,沒法直接查看。因此,如何能夠方便的查看TFRecord格式和數(shù)據(jù),就顯得尤為重要了。
為什么需要查看TFReocrd數(shù)據(jù)?首先我們先看下常規(guī)的寫入和讀取TFRecord數(shù)據(jù)的關(guān)鍵過程。
# 1. 寫入過程 # 一張圖片,我寫入了其內(nèi)容,label,長和寬幾個(gè)信息 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數(shù)據(jù)格式 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數(shù)據(jù)按照Feature解析出對應(yīng)的真實(shí)數(shù)據(jù) ds = ds.map(lambda x : _parse_image(x), num_parallel_calls=4)
上面是一個(gè)標(biāo)準(zhǔn)的TFRecord數(shù)據(jù)的寫入和讀取部分過程,大家應(yīng)該發(fā)現(xiàn)了,讀取TFRecord數(shù)據(jù)的時(shí)候,得知道TFRecord數(shù)據(jù)保存的屬性名和類型,任何一項(xiàng)不匹配,都會(huì)導(dǎo)致無法獲取數(shù)據(jù)。
如果數(shù)據(jù)的寫入和讀取都是自己一個(gè)人完成,那就沒問題。但是如果寫入和讀取是跨團(tuán)隊(duì)合作時(shí)候,如果每次讀取數(shù)據(jù)都得讓對方給完整的屬性名和屬性類型,那效率就太低了。畢竟TFRecord數(shù)據(jù)已經(jīng)包含了一切,自己動(dòng)手豐衣足食。
那么怎么查看TFRecord數(shù)據(jù)呢?使用python tf.train.Example.FromString(serialized_example)方法,方法的入?yún)⑹荰FRecord包含的數(shù)據(jù)字符串。
然后,我直接將上訴查看的過程寫成了一個(gè)py腳本,需要自取。
#!/usr/bin/python # -*- coding: utf-8 -*- import sys import tensorflow as tf # 用法:python trackTFRecord.py True file1 file2 # trackTFRecord.py 就是當(dāng)前這個(gè)py文件 # True 表示是否輸出具體的數(shù)據(jù) # file1 file2 表示的是需要查看的TFRecord文件的絕對路徑 # 輸出說明:tf.float32對應(yīng)TFRecord的FloatList,tf.int64對應(yīng)Int64List,tf.string對應(yīng)BytesList def main(): print('TFRecord文件個(gè)數(shù)為{0}個(gè)'.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數(shù)據(jù) ds = tf.data.TFRecordDataset(filenames) ds = ds.batch(10) ds = ds.prefetch(buffer_size=tf.contrib.data.AUTOTUNE) iterator = ds.make_one_shot_iterator() # 為了加快速度,僅僅簡單拿一組數(shù)據(jù)看下結(jié)構(gòu) 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()
下面給大家實(shí)例演示,首先先隨便找個(gè)圖片,寫入到TFRecord數(shù)據(jù)
import tensorflow as tf filename = "/Users/zhanhaitao/Desktop/1.png" # 使用tf.read_file讀進(jìn)圖片數(shù)據(jù) 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數(shù)據(jù) 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" # 定義不同壓縮選項(xiàng)的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() # 將系列化字符串寫入?yún)f(xié)議緩沖區(qū) writer_none.write(example_str_none) # 關(guān)閉TFRecords文件操作接口 writer_none.close() print("finish to write data to tfrecord file!")
然后,使用上面的腳本看下這個(gè)TFRecord數(shù)據(jù)定義了哪些屬性,以及對應(yīng)的格式,先進(jìn)入到腳本的目錄下,因?yàn)閳D像數(shù)據(jù)內(nèi)容太大,影響閱讀,就只看屬性名和type了:
python trackTFRecord.py False /Users/zhanhaitao/Desktop/a.tfrecord # 結(jié)果,其中bytes_list對應(yīng)tf.string,int64_list對應(yīng)tf.int64 float_list對應(yīng)tf.float32 # image_raw : bytes_list # width : int64_list # float_val : float_list # height : int64_list
以上這篇TFRecord文件查看包含的所有Features代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。