1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
import tensorflow as tf import numpy as np
data_arr = [ { 'int_data': 108, 'float_data': 2.45, 'str_data': 'String 100', 'float_list_data': [256.78, 13.9], 'image_data': np.random.uniform(0, 255, size=(3, 4, 2)) }, { 'int_data': 37, 'float_data': 84.3, 'str_data': 'String 200', 'float_list_data': [1.34, 843.9, 65.22], 'image_data': np.random.uniform(0, 255, size=(3, 4, 2)) } ]
tf.reset_default_graph()
def get_example_object(data_record): int_list1 = tf.train.Int64List(value=[data_record['int_data']]) float_list1 = tf.train.FloatList(value=[data_record['float_data']]) str_list1 = tf.train.BytesList(value=[data_record['str_data'].encode('utf-8')]) float_list2 = tf.train.FloatList(value=data_record['float_list_data']) image_list = tf.train.BytesList(value=[data_record['image_data'].tostring()])
feature_key_value_pair = { 'int_list1': tf.train.Feature(int64_list=int_list1), 'float_list1': tf.train.Feature(float_list=float_list1), 'str_list1': tf.train.Feature(bytes_list=str_list1), 'float_list2': tf.train.Feature(float_list=float_list2), 'image_list': tf.train.Feature(bytes_list=image_list) }
features = tf.train.Features(feature=feature_key_value_pair)
example = tf.train.Example(features=features) return example
with tf.python_io.TFRecordWriter('example.tfrecord') as tfwriter: for data_record in data_arr: example = get_example_object(data_record)
tfwriter.write(example.SerializeToString())
|