Skip to content

Commit 875fcb3

Browse files
authored
Merge pull request #3619 from qlzh727/model_test
Add benchmark utility functions for metric logging
2 parents 5a02f05 + 0308e7e commit 875fcb3

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

official/utils/logging/logger.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
16+
"""Logging utilities for benchmark."""
17+
from __future__ import absolute_import
18+
from __future__ import division
19+
from __future__ import print_function
20+
21+
import datetime
22+
import json
23+
import numbers
24+
import os
25+
26+
import tensorflow as tf
27+
28+
_METRIC_LOG_FILE_NAME = "metric.log"
29+
_DATE_TIME_FORMAT_PATTERN = "%Y-%m-%dT%H:%M:%S.%fZ"
30+
31+
32+
class BenchmarkLogger(object):
33+
"""Class to log the benchmark information to local disk."""
34+
35+
def __init__(self, logging_dir):
36+
self._logging_dir = logging_dir
37+
if not tf.gfile.IsDirectory(self._logging_dir):
38+
tf.gfile.MakeDirs(self._logging_dir)
39+
40+
def log_metric(self, name, value, unit=None, global_step=None, extras=None):
41+
"""Log the benchmark metric information to local file.
42+
43+
Currently the logging is done in a synchronized way. This should be updated
44+
to log asynchronously.
45+
46+
Args:
47+
name: string, the name of the metric to log.
48+
value: number, the value of the metric. The value will not be logged if it
49+
is not a number type.
50+
unit: string, the unit of the metric, E.g "image per second".
51+
global_step: int, the global_step when the metric is logged.
52+
extras: map of string:string, the extra information about the metric.
53+
"""
54+
if not isinstance(value, numbers.Number):
55+
tf.logging.warning(
56+
"Metric value to log should be a number. Got %s", type(value))
57+
return
58+
59+
with tf.gfile.GFile(
60+
os.path.join(self._logging_dir, _METRIC_LOG_FILE_NAME), "a") as f:
61+
metric = {
62+
"name": name,
63+
"value": float(value),
64+
"unit": unit,
65+
"global_step": global_step,
66+
"timestamp": datetime.datetime.now().strftime(
67+
_DATE_TIME_FORMAT_PATTERN),
68+
"extras": extras}
69+
try:
70+
json.dump(metric, f)
71+
f.write("\n")
72+
except (TypeError, ValueError) as e:
73+
tf.logging.warning("Failed to dump metric to log file: name %s, value %s, error %s",
74+
name, value, e)
75+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
16+
"""Tests for benchmark logger."""
17+
18+
from __future__ import absolute_import
19+
from __future__ import division
20+
from __future__ import print_function
21+
22+
23+
import json
24+
import os
25+
import tempfile
26+
27+
28+
from official.utils.logging import logger
29+
import tensorflow as tf
30+
31+
32+
class BenchmarkLoggerTest(tf.test.TestCase):
33+
34+
def tearDown(self):
35+
super(BenchmarkLoggerTest, self).tearDown()
36+
tf.gfile.DeleteRecursively(self.get_temp_dir())
37+
38+
def test_create_logging_dir(self):
39+
non_exist_temp_dir = os.path.join(self.get_temp_dir(), "unknown_dir")
40+
self.assertFalse(tf.gfile.IsDirectory(non_exist_temp_dir))
41+
42+
logger.BenchmarkLogger(non_exist_temp_dir)
43+
self.assertTrue(tf.gfile.IsDirectory(non_exist_temp_dir))
44+
45+
def test_log_metric(self):
46+
log_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
47+
log = logger.BenchmarkLogger(log_dir)
48+
log.log_metric("accuracy", 0.999, global_step=1e4, extras={"name": "value"})
49+
50+
metric_log = os.path.join(log_dir, "metric.log")
51+
self.assertTrue(tf.gfile.Exists(metric_log))
52+
with tf.gfile.GFile(metric_log) as f:
53+
metric = json.loads(f.readline())
54+
self.assertEqual(metric["name"], "accuracy")
55+
self.assertEqual(metric["value"], 0.999)
56+
self.assertEqual(metric["unit"], None)
57+
self.assertEqual(metric["global_step"], 1e4)
58+
self.assertEqual(metric["extras"], {"name": "value"})
59+
60+
def test_log_multiple_metrics(self):
61+
log_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
62+
log = logger.BenchmarkLogger(log_dir)
63+
log.log_metric("accuracy", 0.999, global_step=1e4, extras={"name": "value"})
64+
log.log_metric("loss", 0.02, global_step=1e4)
65+
66+
metric_log = os.path.join(log_dir, "metric.log")
67+
self.assertTrue(tf.gfile.Exists(metric_log))
68+
with tf.gfile.GFile(metric_log) as f:
69+
accuracy = json.loads(f.readline())
70+
self.assertEqual(accuracy["name"], "accuracy")
71+
self.assertEqual(accuracy["value"], 0.999)
72+
self.assertEqual(accuracy["unit"], None)
73+
self.assertEqual(accuracy["global_step"], 1e4)
74+
self.assertEqual(accuracy["extras"], {"name": "value"})
75+
76+
loss = json.loads(f.readline())
77+
self.assertEqual(loss["name"], "loss")
78+
self.assertEqual(loss["value"], 0.02)
79+
self.assertEqual(loss["unit"], None)
80+
self.assertEqual(loss["global_step"], 1e4)
81+
82+
def test_log_non_nubmer_value(self):
83+
log_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
84+
log = logger.BenchmarkLogger(log_dir)
85+
const = tf.constant(1)
86+
log.log_metric("accuracy", const)
87+
88+
metric_log = os.path.join(log_dir, "metric.log")
89+
self.assertFalse(tf.gfile.Exists(metric_log))
90+
91+
if __name__ == "__main__":
92+
tf.test.main()

0 commit comments

Comments
 (0)