25

After following this tutorial on summaries and TensorBoard, I've been able to successfully save and look at data with TensorBoard. Is it possible to open this data with something other than TensorBoard?

By the way, my application is to do off-policy learning. I'm currently saving each state-action-reward tuple using SummaryWriter. I know I could manually store/train on this data, but I thought it'd be nice to use TensorFlow's built in logging features to store/load this data.

Veech
  • 1,397
  • 2
  • 12
  • 20
  • Does this answer your question? [TensorFlow - Importing data from a TensorBoard TFEvent file?](https://stackoverflow.com/questions/37304461/tensorflow-importing-data-from-a-tensorboard-tfevent-file) – J3soon Dec 01 '21 at 14:07

9 Answers9

48

As of March 2017, the EventAccumulator tool has been moved from Tensorflow core to the Tensorboard Backend. You can still use it to extract data from Tensorboard log files as follows:

from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
event_acc = EventAccumulator('/path/to/summary/folder')
event_acc.Reload()
# Show all tags in the log file
print(event_acc.Tags())

# E. g. get wall clock, number of steps and value for a scalar 'Accuracy'
w_times, step_nums, vals = zip(*event_acc.Scalars('Accuracy'))
Chris Cundy
  • 581
  • 4
  • 3
  • Nice @Chris. I could print all the event_acc: `{'scalars': [u'objective/weight-decay/l2_logits_layer/weights_0', u'objective/weight-decay/l2_conv_1/weights_0', u'objective/weight-decay/l2_dot_1/weights_0', u'objective/l1-regularization/l1_conv_2/weights_0', u'test/accuracy_1', u'objective/l1-regularization/l1_dot_1/weights_0', u'objective/weight-decay/l2_dot_2/weights_0', ...} How am I gonna print the value for instance for: ` u'objective/weight-decay/l2_dot_2/weights_0'`. Thanks – Amir Aug 29 '17 at 01:32
  • 1
    This is definitely the answer that gets me off the ground. thx – WestCoastProjects Sep 06 '19 at 23:49
  • 2
    Please note that by default `EventAccumulator` only load at most 10000 data points from file so you will not get all the data points in this way unless you set `size_guidance`. See the code: https://github.com/tensorflow/tensorboard/blob/master/tensorboard/backend/event_processing/event_accumulator.py#L186 – Qin Heyang Jan 04 '21 at 14:15
8

Easy, the data can actually be exported to a .csv file within TensorBoard under the Events tab, which can e.g. be loaded in a Pandas dataframe in Python. Make sure you check the Data download links box.

For a more automated approach, check out the TensorBoard readme:

If you'd like to export data to visualize elsewhere (e.g. iPython Notebook), that's possible too. You can directly depend on the underlying classes that TensorBoard uses for loading data: python/summary/event_accumulator.py (for loading data from a single run) or python/summary/event_multiplexer.py (for loading data from multiple runs, and keeping it organized). These classes load groups of event files, discard data that was "orphaned" by TensorFlow crashes, and organize the data by tag.

As another option, there is a script (tensorboard/scripts/serialize_tensorboard.py) which will load a logdir just like TensorBoard does, but write all of the data out to disk as json instead of starting a server. This script is setup to make "fake TensorBoard backends" for testing, so it is a bit rough around the edges.

nom
  • 148
  • 1
  • 6
  • 2
    As Tensorflow version 1.1, these files (event_accumulator.py/event_multiplexer.py) do not exist, they have been refactored. – shark8me May 11 '17 at 09:47
6

I think the data are encoded protobufs RecordReader format. To get serialized strings out of files you can use py_record_reader or build a graph with TFRecordReader op, and to deserialize those strings to protobuf use Event schema. If you get a working example, please update this q, since we seem to be missing documentation on this.

Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197
3

I did something along these lines for a previous project. As mentioned by others, the main ingredient is tensorflows event accumulator

from tensorflow.python.summary import event_accumulator as ea

acc = ea.EventAccumulator("folder/containing/summaries/")
acc.Reload()

# Print tags of contained entities, use these names to retrieve entities as below
print(acc.Tags())

# E. g. get all values and steps of a scalar called 'l2_loss'
xy_l2_loss = [(s.step, s.value) for s in acc.Scalars('l2_loss')]

# Retrieve images, e. g. first labeled as 'generator'
img = acc.Images('generator/image/0')
with open('img_{}.png'.format(img.step), 'wb') as f:
  f.write(img.encoded_image_string)
panmari
  • 3,627
  • 3
  • 28
  • 48
1

You can also use the tf.train.summaryiterator: To extract events in a ./logs-Folder where only classic scalars lr, acc, loss, val_acc and val_loss are present you can use this GIST: tensorboard_to_csv.py

Phil
  • 301
  • 2
  • 7
1

Chris Cundy's answer works well when you have less than 10000 data points in your tfevent file. However, when you have a large file with over 10000 data points, Tensorboard will automatically sampling them and only gives you at most 10000 points. It is a quite annoying underlying behavior as it is not well-documented. See https://github.com/tensorflow/tensorboard/blob/master/tensorboard/backend/event_processing/event_accumulator.py#L186.

To get around it and get all data points, a bit hacky way is to:

from tensorboard.backend.event_processing.event_accumulator import EventAccumulator

class FalseDict(object):
    def __getitem__(self,key):
        return 0
    def __contains__(self, key):
        return True

event_acc = EventAccumulator('path/to/your/tfevents',size_guidance=FalseDict())
Qin Heyang
  • 1,456
  • 1
  • 16
  • 18
1

It looks like for tb version >=2.3 you can streamline the process of converting your tb events to a pandas dataframe using tensorboard.data.experimental.ExperimentFromDev(). It requires you to upload your logs to TensorBoard.dev, though, which is public. There are plans to expand the capability to locally stored logs in the future. https://www.tensorflow.org/tensorboard/dataframe_api

user3826929
  • 401
  • 4
  • 5
1

You can also use the EventFileLoader to iterate through a tensorboard file

from tensorboard.backend.event_processing.event_file_loader import EventFileLoader

for event in EventFileLoader('path/to/events.out.tfevents.xxx').Load():
    print(event)
popcorny
  • 1,710
  • 16
  • 16
1

Surprisingly, the python package tb_parse has not been mentioned yet.

From documentation:

Installation:

pip install tensorflow # or tensorflow-cpu pip install -U tbparse # requires Python >= 3.7

Note: If you don't want to install TensorFlow, see Installing without TensorFlow.

We suggest using an additional virtual environment for parsing and plotting the tensorboard events. So no worries if your training code uses Python 3.6 or older versions.

Reading one or more event files with tbparse only requires 5 lines of code:

from tbparse import SummaryReader
log_dir = "<PATH_TO_EVENT_FILE_OR_DIRECTORY>"
reader = SummaryReader(log_dir)
df = reader.scalars
print(df)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '23 at 06:36