1 回答
TA贡献1806条经验 获得超5个赞
正如如何在 Tensorboard 中加载选定范围的样本中所述,TensorBoard 事件实际上是存储的记录文件,因此您可以读取它们并按原样处理它们。这是一个与那里发布的脚本类似的脚本,但用于重命名事件,并更新为在 TF 2.x 中工作。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# rename_events.py
import sys
from pathlib import Path
import os
# Use this if you want to avoid using the GPU
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import tensorflow as tf
from tensorflow.core.util.event_pb2 import Event
def rename_events(input_path, output_path, old_tags, new_tag):
# Make a record writer
with tf.io.TFRecordWriter(str(output_path)) as writer:
# Iterate event records
for rec in tf.data.TFRecordDataset([str(input_path)]):
# Read event
ev = Event()
ev.MergeFromString(rec.numpy())
# Check if it is a summary
if ev.summary:
# Iterate summary values
for v in ev.summary.value:
# Check if the tag should be renamed
if v.tag in old_tags:
# Rename with new tag name
v.tag = new_tag
writer.write(ev.SerializeToString())
def rename_events_dir(input_dir, output_dir, old_tags, new_tag):
input_dir = Path(input_dir)
output_dir = Path(output_dir)
# Make output directory
output_dir.mkdir(parents=True, exist_ok=True)
# Iterate event files
for ev_file in input_dir.glob('**/*.tfevents*'):
# Make directory for output event file
out_file = Path(output_dir, ev_file.relative_to(input_dir))
out_file.parent.mkdir(parents=True, exist_ok=True)
# Write renamed events
rename_events(ev_file, out_file, old_tags, new_tag)
if __name__ == '__main__':
if len(sys.argv) != 5:
print(f'{sys.argv[0]} <input dir> <output dir> <old tags> <new tag>',
file=sys.stderr)
sys.exit(1)
input_dir, output_dir, old_tags, new_tag = sys.argv[1:]
old_tags = old_tags.split(';')
rename_events_dir(input_dir, output_dir, old_tags, new_tag)
print('Done')
你会像这样使用它:
> python rename_events.py my_log_dir renamed_log_dir "Training Loss;Validation Loss" loss
添加回答
举报