Skip to content

Commit affff3d

Browse files
authored
[tensorboard] add rename and remove event tools (#269)
* [tensorboard] add rename and remove event tools * [tensorboard] add rename and remove event tools * cleanup
1 parent 26feccc commit affff3d

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

tools/tb/tb-remove-events-by-group.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# this script removes events from tensorboard log files by group names
5+
# it does the removal in place (so make back ups!)
6+
#
7+
# example:
8+
#
9+
# find . -name "*.tfevents*" -exec tb-remove-events-by-group.py {} "batch-size" \;
10+
#
11+
# which wold match any of "batch-size/batch-size", "batch-size/batch-size vs samples", etc.
12+
#
13+
# more than one group can be removed - use `;` as a separator:
14+
#
15+
# tb-remove-events-by-group.py events.out.tfevents.1 "batch-size;grad-norm"
16+
#
17+
# this script is derived from https://stackoverflow.com/a/60080531/9201239
18+
#
19+
# Important: this script requires CUDA environment.
20+
21+
from pathlib import Path
22+
import os
23+
import re
24+
import shlex
25+
import sys
26+
27+
# avoid using the GPU
28+
os.environ['CUDA_VISIBLE_DEVICES'] = ''
29+
# disable logging
30+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
31+
import tensorflow as tf
32+
from tensorflow.core.util.event_pb2 import Event
33+
34+
35+
def is_tag_matching_group(tag, groups_to_remove):
36+
for group in groups_to_remove:
37+
if tag.startswith(group):
38+
return True
39+
return False
40+
41+
42+
def remove_events(input_file, groups_to_remove):
43+
new_file = input_file + ".new"
44+
# Make a record writer
45+
with tf.io.TFRecordWriter(new_file) as writer:
46+
# Iterate event records
47+
for rec in tf.data.TFRecordDataset([input_file]):
48+
# Read event
49+
ev = Event()
50+
ev.MergeFromString(rec.numpy())
51+
# Check if it is a summary event
52+
if ev.summary:
53+
orig_values = [v for v in ev.summary.value]
54+
filtered_values = [v for v in orig_values if not is_tag_matching_group(v.tag, groups_to_remove)]
55+
#print(f"filtered_values={len(filtered_values)}, orig_values={len(orig_values)}")
56+
if len(filtered_values) != len(orig_values):
57+
# for v in orig_values:
58+
# print(v)
59+
del ev.summary.value[:]
60+
ev.summary.value.extend(filtered_values)
61+
writer.write(ev.SerializeToString())
62+
os.rename(new_file, input_file)
63+
64+
def remove_events_dir(input_file, groups_to_remove):
65+
# Write removed events
66+
remove_events(input_file, groups_to_remove)
67+
68+
if __name__ == '__main__':
69+
if len(sys.argv) != 3:
70+
print(f'{sys.argv[0]} <input file> <tags to remove>',
71+
file=sys.stderr)
72+
sys.exit(1)
73+
input_file, groups_to_remove = sys.argv[1:]
74+
print(input_file, shlex.quote(groups_to_remove))
75+
groups_to_remove = groups_to_remove.split(';')
76+
remove_events_dir(input_file, groups_to_remove)

tools/tb/tb-remove-events-by-tag.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# this script removes events from tensorboard log files by specific tag names
5+
# it does the removal in place (so make back ups!)
6+
#
7+
# example:
8+
#
9+
# find . -name "*.tfevents*" -exec tb-remove-events-by-tag.py {} "batch-size/batch-size" \;
10+
#
11+
# more than one tag can be removed - use `;` as a separator:
12+
#
13+
# tb-remove-events-by-tag.py events.out.tfevents.1 "batch-size/batch-size;batch-size/batch-size vs samples"
14+
#
15+
# this script is derived from https://stackoverflow.com/a/60080531/9201239
16+
#
17+
# Important: this script requires CUDA environment.
18+
19+
import shlex
20+
import sys
21+
from pathlib import Path
22+
import os
23+
# avoid using the GPU
24+
os.environ['CUDA_VISIBLE_DEVICES'] = ''
25+
# disable logging
26+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
27+
import tensorflow as tf
28+
from tensorflow.core.util.event_pb2 import Event
29+
30+
def remove_events(input_file, tags_to_remove):
31+
new_file = input_file + ".new"
32+
# Make a record writer
33+
with tf.io.TFRecordWriter(new_file) as writer:
34+
# Iterate event records
35+
for rec in tf.data.TFRecordDataset([input_file]):
36+
# Read event
37+
ev = Event()
38+
ev.MergeFromString(rec.numpy())
39+
# Check if it is a summary event
40+
if ev.summary:
41+
orig_values = [v for v in ev.summary.value]
42+
filtered_values = [v for v in orig_values if v.tag not in tags_to_remove]
43+
#print(f"filtered_values={len(filtered_values)}, orig_values={len(orig_values)}")
44+
if len(filtered_values) != len(orig_values):
45+
# for v in orig_values:
46+
# print(v)
47+
del ev.summary.value[:]
48+
ev.summary.value.extend(filtered_values)
49+
writer.write(ev.SerializeToString())
50+
os.rename(new_file, input_file)
51+
52+
def remove_events_dir(input_file, tags_to_remove):
53+
# Write removed events
54+
remove_events(input_file, tags_to_remove)
55+
56+
if __name__ == '__main__':
57+
if len(sys.argv) != 3:
58+
print(f'{sys.argv[0]} <input file> <tags to remove>',
59+
file=sys.stderr)
60+
sys.exit(1)
61+
input_file, tags_to_remove = sys.argv[1:]
62+
print(input_file, shlex.quote(tags_to_remove))
63+
tags_to_remove = tags_to_remove.split(';')
64+
remove_events_dir(input_file, tags_to_remove)

tools/tb/tb-rename-events.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# this script renames event names in tensorboard log files
5+
# it does the rename in place (so make back ups!)
6+
#
7+
# example:
8+
#
9+
# find . -name "*.tfevents*" -exec tb-rename-events.py {} "iteration-time" "iteration-time/iteration-time" \;
10+
#
11+
# more than one old tag can be remapped to one new tag - use `;` as a separator:
12+
#
13+
# tb-rename-events.py events.out.tfevents.1 "training loss;validation loss" "loss"
14+
#
15+
# this script is derived from https://stackoverflow.com/a/60080531/9201239
16+
#
17+
# Important: this script requires CUDA environment.
18+
19+
import shlex
20+
import sys
21+
from pathlib import Path
22+
import os
23+
# avoid using the GPU
24+
os.environ['CUDA_VISIBLE_DEVICES'] = ''
25+
# disable logging
26+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
27+
import tensorflow as tf
28+
from tensorflow.core.util.event_pb2 import Event
29+
30+
def rename_events(input_file, old_tags, new_tag):
31+
new_file = input_file + ".new"
32+
# Make a record writer
33+
with tf.io.TFRecordWriter(new_file) as writer:
34+
# Iterate event records
35+
for rec in tf.data.TFRecordDataset([input_file]):
36+
# Read event
37+
ev = Event()
38+
ev.MergeFromString(rec.numpy())
39+
# Check if it is a summary
40+
#print(ev)
41+
if ev.summary:
42+
# Iterate summary values
43+
for v in ev.summary.value:
44+
#print(v)
45+
# Check if the tag should be renamed
46+
if v.tag in old_tags:
47+
# Rename with new tag name
48+
v.tag = new_tag
49+
writer.write(ev.SerializeToString())
50+
os.rename(new_file, input_file)
51+
52+
def rename_events_dir(input_file, old_tags, new_tag):
53+
# Write renamed events
54+
rename_events(input_file, old_tags, new_tag)
55+
56+
if __name__ == '__main__':
57+
if len(sys.argv) != 4:
58+
print(f'{sys.argv[0]} <input file> <old tags> <new tag>',
59+
file=sys.stderr)
60+
sys.exit(1)
61+
input_file, old_tags, new_tag = sys.argv[1:]
62+
print(input_file, shlex.quote(old_tags), shlex.quote(new_tag))
63+
old_tags = old_tags.split(';')
64+
rename_events_dir(input_file, old_tags, new_tag)

0 commit comments

Comments
 (0)