TensorBoard EventAccumulator not showing scalar data while TensorBoard UI displays graphs correctly

285 Views Asked by At

I am using the following code to read the contents of the event files generated by TensorFlow and display the scalar data. The code is able to list all the tags correctly but doesn't show the contents of those tags.

import os
import tensorflow as tf
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator

def process_event_file(event_file):
    print(f"Processing event file: {event_file}")
    
    event_acc = EventAccumulator(event_file)
    event_acc.Reload()
    
    print(event_acc.Tags())
    scalar_tags = event_acc.Tags()['scalars']
    for tag in scalar_tags:
        scalar_events = event_acc.Scalars(tag)
        print(f"\nData for tag '{tag}':")
        for event in scalar_events:
            print(f"Step {event.step}, Wall time {event.wall_time}: {event.value}")

def find_and_process_event_files(directory):
    for root, _, files in os.walk(directory):
        for file in files:
            if file.startswith("events.out.tfevents"):
                event_file_path = os.path.join(root, file)
                process_event_file(event_file_path)
                print("\n" + "=" * 80 + "\n")

find_and_process_event_files("/content/drive/MyDrive/logs")

However, when I run TensorBoard in my browser, all the graphs are plotted correctly. Why is the code unable to print the contents of the scalar data, even though TensorBoard displays the graphs correctly? What am I missing, or how can I fix this issue?

Update: The print output looks

Processing event file: /content/drive/some-folder/logs/2023-04-13 13:52:36.450081/tensorboard/trainer/UNIQUE_ID_HERE/events.out.tfevents.xxxxxx.localhost.localdomain.xxxx.x.v2

{'images': [], 'audio': [], 'histograms': [], 'scalars': [], 'distributions': [], 'tensors': ........}
1

There are 1 best solutions below

1
Christophe Tallet On

I had the same issue, for summaries written by TensorFlow 2.x the scalars are no longer under the "scalars" key but under the "tensors" key (you can see this with a debugger inspecting your EventAccumulator object, after the Reload()).

You can retrieve the data via the .Tensors() method and then pass it through tf.make_ndarray() to get back the scalar value.

See nfelt's comment here : https://github.com/tensorflow/tensorboard/issues/5395