Search code examples
tensorflowvisualizationtensorboardtensorflow2

How to save histogram plots from Tensorboard 2 to disk, just like you can do with scalars?


I am using Tensorboard 2 to visualize my training data and I am able to save scalar plots to disk. However, I am unable to find a way to do this for histogram plots (tf.summary.histogram).

Is it possible to save histogram plots from Tensorboard 2 to disk, just like it is possible to do with scalars? I have looked through the documentation and it seems like this is not supported, but I wanted to confirm with the community before giving up. Any help or suggestions would be greatly appreciated.


Solution

  • There is an open issue to add a download button for histograms. However, this issue is open for more than 4 years, so I doubt it is getting resolved soon.

    A workaround is to use the url that tensorboard would use to get the data. A short example:

    # writing some data to tensorboard
    from torch.utils.tensorboard import SummaryWriter
    import numpy as np
    
    writer = SummaryWriter('./tmp')
    
    writer.add_histogram('hist', np.arange(10), 0)
    

    Open tensorboard in the browser (here localhost:6006):

    Tensorboard

    Get data as JSON using the template
    http://<tb-host>/data/plugin/histograms/histograms?run=<run-name>&tag=<tag-name>.
    Here http://localhost:6006/data/plugin/histograms/histograms/?run=.&tag=hist:

    Data

    Now you can download the data as JSON.
    Quick comparison with matplotlib:

    import pandas as pd
    import json
    import matplotlib.pyplot as plt
    
    with open('histograms.json', 'r') as f:
        d = pd.DataFrame(json.load(f)[0][2])
    
    fix, axes = plt.subplots(1, 2, figsize=(10, 3))
    axes[0].bar(d[1], d[2])
    axes[0].set_title('tb')
    
    axes[1].hist(data)
    axes[1].set_title('original')
    

    Hist