30
30
import rfc3339
31
31
import tensorflow as tf
32
32
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
34
34
35
35
from pkg .metricscollector .v1beta1 .common import const
36
36
37
37
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
+
38
50
class TFEventFileParser :
39
51
def __init__ (self , metric_names ):
40
52
self .metric_names = metric_names
@@ -47,21 +59,15 @@ def find_all_files(directory):
47
59
48
60
def parse_summary (self , tfefile ):
49
61
metric_logs = []
50
- event_accumulator = EventAccumulator (tfefile , size_guidance = {TENSORS : 0 })
62
+ event_accumulator = EventAccumulator (
63
+ tfefile , size_guidance = {SCALARS : 0 , TENSORS : 0 }
64
+ )
51
65
event_accumulator .Reload ()
52
- for tag in event_accumulator .Tags ()[TENSORS ]:
66
+ tags = event_accumulator .Tags ()
67
+ for tag in tags [TENSORS ]:
53
68
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 ):
63
70
continue
64
-
65
71
for tensor in event_accumulator .Tensors (tag ):
66
72
ml = api_pb2 .MetricLog (
67
73
time_stamp = rfc3339 .rfc3339 (
@@ -72,7 +78,19 @@ def parse_summary(self, tfefile):
72
78
),
73
79
)
74
80
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 )
76
94
return metric_logs
77
95
78
96
0 commit comments