Search code examples
pythontensorflowtfrecord

How to inspect a Tensorflow .tfrecord file?


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...

Solution

  • 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))