使用以下代碼直接查看TFRecord裏面的數據情況,無需解析。
import sys
import tensorflow as tf
def main():
filepath = './train.tfrecord'
with tf.Session() as sess:
filenames = [filepath]
# 加載TFRecord數據
ds = tf.data.TFRecordDataset(filenames)
# 讀取N條
ds = ds.batch(8)
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)
for i in range(0, res.shape[0]):
print('-------------------第%d條數據-----------------'%i)
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, fvalue = None, 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} : {2}'.format(key, ftype, fvalue)
print(result)
if __name__ == "__main__":
main()