Skip to content

Commit d1c626e

Browse files
committed
Support old-style TensorFlow events (tensorboard)
Fixes: #2466
1 parent 3363964 commit d1c626e

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

pkg/metricscollector/v1beta1/tfevent-metricscollector/tfevent_loader.py

+32-14
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,23 @@
3030
import rfc3339
3131
import tensorflow as tf
3232
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
33-
from tensorboard.backend.event_processing.tag_types import TENSORS
33+
from tensorboard.backend.event_processing.tag_types import SCALARS, TENSORS
3434

3535
from pkg.metricscollector.v1beta1.common import const
3636

3737

38+
def _should_consider(tag: str, metric_name: str, tfefile: str) -> bool:
39+
tfefile_parent_dir = (
40+
os.path.dirname(metric_name)
41+
if len(metric_name.split("/")) >= 2
42+
else os.path.dirname(tfefile)
43+
)
44+
basedir_name = os.path.dirname(tfefile)
45+
return tag.startswith(metric_name.split("/")[-1]) and basedir_name.endswith(
46+
tfefile_parent_dir
47+
)
48+
49+
3850
class TFEventFileParser:
3951
def __init__(self, metric_names):
4052
self.metric_names = metric_names
@@ -47,21 +59,15 @@ def find_all_files(directory):
4759

4860
def parse_summary(self, tfefile):
4961
metric_logs = []
50-
event_accumulator = EventAccumulator(tfefile, size_guidance={TENSORS: 0})
62+
event_accumulator = EventAccumulator(
63+
tfefile, size_guidance={SCALARS: 0, TENSORS: 0}
64+
)
5165
event_accumulator.Reload()
52-
for tag in event_accumulator.Tags()[TENSORS]:
66+
tags = event_accumulator.Tags()
67+
for tag in tags[TENSORS]:
5368
for m in self.metric_names:
54-
tfefile_parent_dir = (
55-
os.path.dirname(m)
56-
if len(m.split("/")) >= 2
57-
else os.path.dirname(tfefile)
58-
)
59-
basedir_name = os.path.dirname(tfefile)
60-
if not tag.startswith(m.split("/")[-1]) or not basedir_name.endswith(
61-
tfefile_parent_dir
62-
):
69+
if not _should_consider(tag, m, tfefile):
6370
continue
64-
6571
for tensor in event_accumulator.Tensors(tag):
6672
ml = api_pb2.MetricLog(
6773
time_stamp=rfc3339.rfc3339(
@@ -72,7 +78,19 @@ def parse_summary(self, tfefile):
7278
),
7379
)
7480
metric_logs.append(ml)
75-
81+
# support old-style tensorboard metrics too
82+
for tag in tags[SCALARS]:
83+
for m in self.metric_names:
84+
if not _should_consider(tag, m, tfefile):
85+
continue
86+
for scalar in event_accumulator.Scalars(tag):
87+
ml = api_pb2.MetricLog(
88+
time_stamp=rfc3339.rfc3339(
89+
datetime.fromtimestamp(scalar.wall_time)
90+
),
91+
metric=api_pb2.Metric(name=m, value=str(scalar.value)),
92+
)
93+
metric_logs.append(ml)
7694
return metric_logs
7795

7896

0 commit comments

Comments
 (0)