日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TF2.0 TFRecord创建和读取

發(fā)布時間:2023/12/20 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TF2.0 TFRecord创建和读取 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本人為在職研究生,希望能夠有志同道合的學(xué)習(xí)伙伴一起學(xué)習(xí)分享和交流,本人領(lǐng)域?yàn)楣饫w傳感和人工智能,希望可以一起學(xué)習(xí)。
微信公眾號:Deepthinkerr(文章末尾有圖)

文章目錄

  • 1. TFRecord
    • 1.1 數(shù)據(jù)寫入TFRecord
    • 1.2 讀取TFRecord
  • 2. 微信公眾號

1. TFRecord

這里不太建議看《Tensorflow2.0卷積神經(jīng)網(wǎng)絡(luò)實(shí)戰(zhàn)》王曉華這本書,講的內(nèi)容并沒有什么問題,但是代碼我嘗試了,很多報(bào)錯(報(bào)錯說函數(shù)沒有參數(shù),不知道是不是我的tf版本問題),而且一些地方?jīng)]有講清楚,這里建議看rensorflow的官網(wǎng)教程,看了一遍整體還是蠻不錯的,一些函數(shù)和書上講的不一樣,這里建議看官方的。

Tensorflow官網(wǎng)TFRecord鏈接:https://tensorflow.google.cn/tutorials/load_data/tfrecord?hl=zh_cn

1.1 數(shù)據(jù)寫入TFRecord

將數(shù)據(jù)寫入TFrecord步驟較為固定,個人總結(jié)為以下幾個步驟:

  • step1:將每個值轉(zhuǎn)換為包含三種兼容類型之一的 tf.train.Feature
  • step2:創(chuàng)建一個從特征名稱字符串到第 1 步中生成的編碼特征值的映射(字典)
  • step3:將第 2 步中生成的映射轉(zhuǎn)換為Features消息
  • step4:創(chuàng)建example
  • step5:寫入TFRecords
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]))def image_feature(image, label):image_shape = image.shapefeature = {'height': _int64_feature(image_shape[0]), # step1'width': _int64_feature(image_shape[1]),'depth': _int64_feature(image_shape[2]),'label': _int64_feature(label),'image_raw': _bytes_feature(image.tobytes()) # 這里需要將圖片信息改為byte數(shù)據(jù)類型,我用的numpy所以使用image.tobytes()} # step2return tf.train.Features(feature=feature) # step3# data, label data = np.random.random([100, 28, 28, 3]) # image [n, h, w, c] label = np.random.randint(0, 10, 100) # label [1] record_file = 'test.tfrecord' # TFRecord name with tf.io.TFRecordWriter(record_file) as writer:for i in range(100):tf_example = tf.train.Example(features=image_feature(data[i], label[i])) # step4writer.write(tf_example.SerializeToString()) # step5

注意:step1使用的是tf.train.Feature函數(shù),函數(shù)參數(shù)為bytes_list、float_list、int64_list三種,是tf.train.Feature接受的三種類型,step3使用的是tf.train.Features,有個s,這里需要注意,而且函數(shù)參數(shù)為feature

個人理解:tf.train.Feature和tf.train.Features區(qū)別,前者是對一個數(shù)據(jù)進(jìn)行Feature消息處理,后者是對多個Feature組成的dict進(jìn)行處理,所以多一個s。

1.2 讀取TFRecord

讀取TFRecord文件步驟也比較固定,主要是需要對讀取的byte數(shù)據(jù)進(jìn)行解析,這里必須和創(chuàng)建TFRecord時相對應(yīng),步驟如下:

  • step1:創(chuàng)建解析字典(和寫入相對應(yīng))
  • step2:解析函數(shù)(將讀取的數(shù)據(jù)解析為寫入的數(shù)據(jù))
  • step3:讀取TFRecords文件
  • step4:解析數(shù)據(jù)
image_feature_description = {'height': tf.io.FixedLenFeature([], tf.int64),'width': tf.io.FixedLenFeature([], tf.int64),'depth': tf.io.FixedLenFeature([], tf.int64),'label': tf.io.FixedLenFeature([], tf.int64),'image_raw': tf.io.FixedLenFeature([], tf.string), } # step1 def _parse_image_function(example_proto):# Parse the input tf.Example proto using the dictionary above.return tf.io.parse_single_example(example_proto, image_feature_description) # step2raw_image_dataset = tf.data.TFRecordDataset('test.tfrecord') # step3 parsed_image_dataset = raw_image_dataset.map(_parse_image_function) # step4

讀取出來的圖片數(shù)據(jù)是string,也就是byte格式,如果是顯示的話直接通過IPython.display.display即可顯示圖片,如果要轉(zhuǎn)換為array需要進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換和reshape。

2. 微信公眾號

總結(jié)

以上是生活随笔為你收集整理的TF2.0 TFRecord创建和读取的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。