Reading TF2 summary file with tf.data.TFRecordDataset

Viewed 1169

In TF1, i could use summary_iterator to read summary files. But now, it will throw a warning

WARNING:tensorflow: tf_record_iterator (from tensorflow.python.lib.io.tf_record) is deprecated and will be removed in a future version.
Instructions for updating:
Use eager execution and: 
`tf.data.TFRecordDataset(path)`

So i'm wondering how to use tf.data.TFRecordDataset(path) to read tfevent files generated by TF2.

2 Answers

Actually, this works for me

from tensorflow.core.util import event_pb2

serialized_examples = tf.data.TFRecordDataset(path)
for serialized_example in serialized_examples:
    event = event_pb2.Event.FromString(serialized_example.numpy())
    for value in event.summary.value:
        t = tf.make_ndarray(value.tensor)
        print(value.tag, event.step, t, type(t))

I do not really know what you try to achieve, but if TFEvent files are regular TFRecord files you can use the new API this way.

event_files = tf.data.Dataset.list_files("./outputs/events.out.tfevents*")
serialized_examples = tf.data.TFRecordDataset(events_files)
for serialized_example in serialized_examples:
  ...

Hope this can help.

Kind.

Related