logger.py 3.38 KB
Newer Older
Scott Zhu's avatar
Scott Zhu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Logging utilities for benchmark."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import datetime
import json
import numbers
import os

import tensorflow as tf

_METRIC_LOG_FILE_NAME = "metric.log"
_DATE_TIME_FORMAT_PATTERN = "%Y-%m-%dT%H:%M:%S.%fZ"


class BenchmarkLogger(object):
  """Class to log the benchmark information to local disk."""

  def __init__(self, logging_dir):
    self._logging_dir = logging_dir
    if not tf.gfile.IsDirectory(self._logging_dir):
      tf.gfile.MakeDirs(self._logging_dir)

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  def log_estimator_evaluation_result(self, eval_results):
    """Log the evaluation result for a estimator.

    The evaluate result is a directory that contains metrics defined in
    model_fn. It also contains a entry for global_step which contains the value
    of the global step when evaluation was performed.

    Args:
      eval_results: dict, the result of evaluate() from a estimator.
    """
    if not isinstance(eval_results, dict):
      tf.logging.warning("eval_results should be directory for logging. Got %s",
                         type(eval_results))
      return
    global_step = eval_results[tf.GraphKeys.GLOBAL_STEP]
    for key in eval_results:
      if key != tf.GraphKeys.GLOBAL_STEP:
        self.log_metric(key, eval_results[key], global_step=global_step)

Scott Zhu's avatar
Scott Zhu committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  def log_metric(self, name, value, unit=None, global_step=None, extras=None):
    """Log the benchmark metric information to local file.

    Currently the logging is done in a synchronized way. This should be updated
    to log asynchronously.

    Args:
      name: string, the name of the metric to log.
      value: number, the value of the metric. The value will not be logged if it
        is not a number type.
      unit: string, the unit of the metric, E.g "image per second".
      global_step: int, the global_step when the metric is logged.
      extras: map of string:string, the extra information about the metric.
    """
    if not isinstance(value, numbers.Number):
      tf.logging.warning(
Karmel Allison's avatar
Karmel Allison committed
75
          "Metric value to log should be a number. Got %s", type(value))
Scott Zhu's avatar
Scott Zhu committed
76
77
78
79
80
81
      return

    with tf.gfile.GFile(
        os.path.join(self._logging_dir, _METRIC_LOG_FILE_NAME), "a") as f:
      metric = {
          "name": name,
Scott Zhu's avatar
Scott Zhu committed
82
          "value": float(value),
Scott Zhu's avatar
Scott Zhu committed
83
84
85
86
87
          "unit": unit,
          "global_step": global_step,
          "timestamp": datetime.datetime.now().strftime(
              _DATE_TIME_FORMAT_PATTERN),
          "extras": extras}
Scott Zhu's avatar
Scott Zhu committed
88
89
90
91
      try:
        json.dump(metric, f)
        f.write("\n")
      except (TypeError, ValueError) as e:
Karmel Allison's avatar
Karmel Allison committed
92
93
        tf.logging.warning("Failed to dump metric to log file: "
                           "name %s, value %s, error %s", name, value, e)