Search code examples
pythonpytorchtensorboard

Export tensorboard (with pytorch) data into csv with python


I have Tensorboard data and want it to download all of the csv files behind the data, but I could not find anything from the official documentation. From StackOverflow, I found only this question which is 7 years old and also it's about TensorFlow while I am using PyTorch.

We can do this manually, as we can see in the screenshot, manually there is an option. I wonder if we can do that via code or it is not possible? As I have a lot of data to process.

enter image description here


Solution

  • With the help of this script Below is the shortest working code it gets all of the data in dataframe then you can play further.

    import traceback
    import pandas as pd
    from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
    
    # Extraction function
    def tflog2pandas(path):
        runlog_data = pd.DataFrame({"metric": [], "value": [], "step": []})
        try:
            event_acc = EventAccumulator(path)
            event_acc.Reload()
            tags = event_acc.Tags()["scalars"]
            for tag in tags:
                event_list = event_acc.Scalars(tag)
                values = list(map(lambda x: x.value, event_list))
                step = list(map(lambda x: x.step, event_list))
                r = {"metric": [tag] * len(step), "value": values, "step": step}
                r = pd.DataFrame(r)
                runlog_data = pd.concat([runlog_data, r])
        # Dirty catch of DataLossError
        except Exception:
            print("Event file possibly corrupt: {}".format(path))
            traceback.print_exc()
        return runlog_data
    path="Run1" #folderpath
    df=tflog2pandas(path)
    #df=df[(df.metric != 'params/lr')&(df.metric != 'params/mm')&(df.metric != 'train/loss')] #delete the mentioned rows
    df.to_csv("output.csv")