How to inspect a Tensorflow .tfrecord file?

こ雲淡風輕ζ 提交于 2019-12-18 10:10:12

问题


I have a .tfrecord but I don't know how it is structured. How can I inspect the schema to understand what the .tfrecord file contains?

All Stackoverflow answers or documentation seem to assume I know the structure of the file.

reader = tf.TFRecordReader()
file = tf.train.string_input_producer("record.tfrecord")
_, serialized_record = reader.read(file)

...HOW TO INSPECT serialized_record...

回答1:


Found it!

import tensorflow as tf

for example in tf.python_io.tf_record_iterator("data/foobar.tfrecord"):
    print(tf.train.Example.FromString(example))

You can also add:

from google.protobuf.json_format import MessageToJson
...
jsonMessage = MessageToJson(tf.train.Example.FromString(example))



回答2:


Use TensorFlow tf.TFRecordReader with the tf.parse_single_example decoder as specified in https://www.tensorflow.org/programmers_guide/reading_data

PS, tfrecord contains 'Example' records defined in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/example/example.proto

Once you extract the record into a string, parsing it is something like this

a=tf.train.Example()
result = a.ParseFromString(binary_string_with_example_record)

However, I'm not sure where's the raw support for extracting individual records from a file, you can track it down in TFRecordReader




回答3:


If your .tftrecord contains SequenceExample, the accepted answer won't show you everything. You can use:

import tensorflow as tf

for example in tf.python_io.tf_record_iterator("data/foobar.tfrecord"):
    result = tf.train.SequenceExample.FromString(example)
    break
print(result)

This will show you the content of the first example.

Then you can also inspect individual Features using their keys:

result.context.feature["foo_key"]

And for FeatureLists:

result.feature_lists.feature_list["bar_key"]


来源:https://stackoverflow.com/questions/42394585/how-to-inspect-a-tensorflow-tfrecord-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!