Tensorboard: Reading TF2 summary file with tf.data.TFRecordDataset

Created on 8 Oct 2019  路  4Comments  路  Source: tensorflow/tensorboard

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.

summaries awaiting response support

Most helpful comment

@BlueFisher This works in Tensorflow 2.0:

import os

import tensorflow as tf
from tensorflow.python.framework import tensor_util

summary_dir = 'tmp/summaries'
summary_writer = tf.summary.create_file_writer('tmp/summaries')

with summary_writer.as_default():
  tf.summary.scalar('loss', 0.1, step=42)
  tf.summary.scalar('loss', 0.2, step=43)
  tf.summary.scalar('loss', 0.3, step=44)
  tf.summary.scalar('loss', 0.4, step=45)


from tensorflow.core.util import event_pb2
from tensorflow.python.lib.io import tf_record

def my_summary_iterator(path):
    for r in tf_record.tf_record_iterator(path):
        yield event_pb2.Event.FromString(r)

for filename in os.listdir(summary_dir):
    path = os.path.join(summary_dir, filename)
    for event in my_summary_iterator(path):
        for value in event.summary.value:
            t = tensor_util.MakeNdarray(value.tensor)
            print(value.tag, event.step, t, type(t))

You can also refer to github gist here

All 4 comments

@BlueFisher summary_iterator is also available in Tensorflow 2.0. You can find it here.

@gowthamkpr summary_iterator is indeed accessible in Tensorflow 2.0. However, there is no simple_value in summary anymore. Like the following code

for e in summary_iterator(path):
    for v in e.summary.value:
        if v.tag == 'tag':
            print(v.simple_value)

It prints zeros all the time.

@BlueFisher This works in Tensorflow 2.0:

import os

import tensorflow as tf
from tensorflow.python.framework import tensor_util

summary_dir = 'tmp/summaries'
summary_writer = tf.summary.create_file_writer('tmp/summaries')

with summary_writer.as_default():
  tf.summary.scalar('loss', 0.1, step=42)
  tf.summary.scalar('loss', 0.2, step=43)
  tf.summary.scalar('loss', 0.3, step=44)
  tf.summary.scalar('loss', 0.4, step=45)


from tensorflow.core.util import event_pb2
from tensorflow.python.lib.io import tf_record

def my_summary_iterator(path):
    for r in tf_record.tf_record_iterator(path):
        yield event_pb2.Event.FromString(r)

for filename in os.listdir(summary_dir):
    path = os.path.join(summary_dir, filename)
    for event in my_summary_iterator(path):
        for value in event.summary.value:
            t = tensor_util.MakeNdarray(value.tensor)
            print(value.tag, event.step, t, type(t))

You can also refer to github gist here

@gowthamkpr Thanks a lot, 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))
Was this page helpful?
0 / 5 - 0 ratings

Related issues

dniku picture dniku  路  3Comments

wchargin picture wchargin  路  3Comments

iron9light picture iron9light  路  4Comments

neouyghur picture neouyghur  路  3Comments

wengqi123 picture wengqi123  路  3Comments