使用Python将PyTorch的TensorBoard数据导出为CSV

3
我有Tensorboard数据,并希望下载数据背后的所有csv文件,但我在官方文档中没有找到任何信息。从StackOverflow上,我只找到了这个问题,而且它是7年前的问题,而且它是关于TensorFlow的,而我正在使用PyTorch。
我们可以手动执行此操作,正如我们在屏幕截图中所见,手动存在一个选项。 我想知道是否可以通过代码完成,或者不可能? 因为我有很多数据要处理。

enter image description here


我在GitHub上找到了一个脚本,可以将TensorBoard事件日志转换为CSV格式脚本 - gerda die gandalfziege
1个回答

6
通过这个脚本的帮助,下面是最短的可用代码,它可以获取dataframe中的所有数据,然后您可以进一步操作。
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")

2
请注意,EventAccumulator 默认限制内存中存储的事件数量。因此,您的 event_list 可能不包含文件中的每个事件。传递 size_guidance={"scalars": 0} 参数给构造函数以解除标量数据的限制。 - Dudly01
如果有人需要延长时间,只需修改/添加以下行:(1) runlog_data = pd.DataFrame({"metric": [], "value": [], "step": [], "wall_time": []}) (2) wall_time = list(map(lambda x: x.wall_time, event_list)) (3) r = {"metric": [tag] * len(step), "value": values, "step": step, "wall_time": wall_time} - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接