|
| 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