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...
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))
Above solutions didn't work for me so for TF 2.0 use this:
import tensorflow as tf
raw_dataset = tf.data.TFRecordDataset("path-to-file")
for raw_record in raw_dataset.take(1):
example = tf.train.Example()
example.ParseFromString(raw_record.numpy())
print(example)
https://www.tensorflow.org/tutorials/load_data/tfrecord#reading_a_tfrecord_file_2
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"]