Can I export a tensorflow summary to CSV?

后端 未结 4 815
太阳男子
太阳男子 2020-12-14 18:07

Is there a way to extract scalar summaries to CSV (preferably from within tensorboard) from tfevents files?

Example code

The following code generates tfeve

4条回答
  •  臣服心动
    2020-12-14 18:39

    While the answer here is as requested within tensorboard it only allows to download a csv for a single run of a single tag. If you have for example 10 tags and 20 runs (what is not at all much) you would need to do the above step 200 times (that alone will probably take you more than a hour). If now you for some reason would like to actually do something with the data for all runs for a single tag you would need to write some weird CSV accumulation script or copy everything by hand (what will probably cost you more than a day).

    Therefore I would like to add a solution that extracts a CSV file for every tag with all runs contained. Column headers are the run path names and row indices are the run step numbers.

    import os
    import numpy as np
    import pandas as pd
    
    from collections import defaultdict
    from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
    
    
    def tabulate_events(dpath):
        summary_iterators = [EventAccumulator(os.path.join(dpath, dname)).Reload() for dname in os.listdir(dpath)]
    
        tags = summary_iterators[0].Tags()['scalars']
    
        for it in summary_iterators:
            assert it.Tags()['scalars'] == tags
    
        out = defaultdict(list)
        steps = []
    
        for tag in tags:
            steps = [e.step for e in summary_iterators[0].Scalars(tag)]
    
            for events in zip(*[acc.Scalars(tag) for acc in summary_iterators]):
                assert len(set(e.step for e in events)) == 1
    
                out[tag].append([e.value for e in events])
    
        return out, steps
    
    
    def to_csv(dpath):
        dirs = os.listdir(dpath)
    
        d, steps = tabulate_events(dpath)
        tags, values = zip(*d.items())
        np_values = np.array(values)
    
        for index, tag in enumerate(tags):
            df = pd.DataFrame(np_values[index], index=steps, columns=dirs)
            df.to_csv(get_file_path(dpath, tag))
    
    
    def get_file_path(dpath, tag):
        file_name = tag.replace("/", "_") + '.csv'
        folder_path = os.path.join(dpath, 'csv')
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        return os.path.join(folder_path, file_name)
    
    
    if __name__ == '__main__':
        path = "path_to_your_summaries"
        to_csv(path)
    

    My solution builds upon: https://stackoverflow.com/a/48774926/2230045


    EDIT:

    I created a more sophisticated version and released it on GitHub: https://github.com/Spenhouet/tensorboard-aggregator

    This version aggregates multiple tensorboard runs and is able to save the aggregates to a new tensorboard summary or as a .csv file.

提交回复
热议问题