1. 程式人生 > >Tensorflow 讀寫 tfrecord 檔案(Python3)

Tensorflow 讀寫 tfrecord 檔案(Python3)

寫入tfrecord檔案

import tensorflow as tf
# 寫入的檔案的路徑
file_path = ''
# 等待寫入的陣列
list = []
writer = tf.python_io.TFRecordWriter(file_path)
example = tf.train.Example(features=tf.train.Features(feature={
    "label": tf.train.Feature(int64_list=tf.train.Int64List(value=list))
}))
writer.write(example.SerializeToString())

讀取tfrecord檔案

import tensorflow as tf

for serialized_example in tf.python_io.tf_record_iterator("檔案的完整路徑"):
    example = tf.train.Example()
    example.ParseFromString(serialized_example)

    label = example.features.feature['label'].int64_list.value
    # 可以做一些預處理之類的
    print(label)