aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/kernel_tests/summary_tensor_op_test.py
blob: 0f4643393a12a2b1d72faaf22683698be3ee6f3b (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Copyright 2016 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" BAvSIS,
# 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.
# ==============================================================================
"""Tests for summary ops."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import six

from tensorflow.core.framework import summary_pb2
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import summary_ops
from tensorflow.python.platform import test


class SummaryOpsTest(test.TestCase):

  def _SummarySingleValue(self, s):
    summ = summary_pb2.Summary()
    summ.ParseFromString(s)
    self.assertEqual(len(summ.value), 1)
    return summ.value[0]

  def _AssertNumpyEq(self, actual, expected):
    self.assertTrue(np.array_equal(actual, expected))

  def testTags(self):
    with self.cached_session() as sess:
      c = constant_op.constant(1)
      s1 = summary_ops.tensor_summary("s1", c)
      with ops.name_scope("foo"):
        s2 = summary_ops.tensor_summary("s2", c)
        with ops.name_scope("zod"):
          s3 = summary_ops.tensor_summary("s3", c)
          s4 = summary_ops.tensor_summary("TensorSummary", c)
      summ1, summ2, summ3, summ4 = sess.run([s1, s2, s3, s4])

    v1 = self._SummarySingleValue(summ1)
    self.assertEqual(v1.tag, "s1")

    v2 = self._SummarySingleValue(summ2)
    self.assertEqual(v2.tag, "foo/s2")

    v3 = self._SummarySingleValue(summ3)
    self.assertEqual(v3.tag, "foo/zod/s3")

    v4 = self._SummarySingleValue(summ4)
    self.assertEqual(v4.tag, "foo/zod/TensorSummary")

  def testScalarSummary(self):
    with self.cached_session() as sess:
      const = constant_op.constant(10.0)
      summ = summary_ops.tensor_summary("foo", const)
      result = sess.run(summ)

    value = self._SummarySingleValue(result)
    n = tensor_util.MakeNdarray(value.tensor)
    self._AssertNumpyEq(n, 10)

  def testStringSummary(self):
    s = six.b("foobar")
    with self.cached_session() as sess:
      const = constant_op.constant(s)
      summ = summary_ops.tensor_summary("foo", const)
      result = sess.run(summ)

    value = self._SummarySingleValue(result)
    n = tensor_util.MakeNdarray(value.tensor)
    self._AssertNumpyEq(n, s)

  def testManyScalarSummary(self):
    with self.cached_session() as sess:
      const = array_ops.ones([5, 5, 5])
      summ = summary_ops.tensor_summary("foo", const)
      result = sess.run(summ)
    value = self._SummarySingleValue(result)
    n = tensor_util.MakeNdarray(value.tensor)
    self._AssertNumpyEq(n, np.ones([5, 5, 5]))

  def testManyStringSummary(self):
    strings = [[six.b("foo bar"), six.b("baz")], [six.b("zoink"), six.b("zod")]]
    with self.cached_session() as sess:
      const = constant_op.constant(strings)
      summ = summary_ops.tensor_summary("foo", const)
      result = sess.run(summ)
    value = self._SummarySingleValue(result)
    n = tensor_util.MakeNdarray(value.tensor)
    self._AssertNumpyEq(n, strings)

  def testManyBools(self):
    bools = [True, True, True, False, False, False]
    with self.cached_session() as sess:
      const = constant_op.constant(bools)
      summ = summary_ops.tensor_summary("foo", const)
      result = sess.run(summ)

    value = self._SummarySingleValue(result)
    n = tensor_util.MakeNdarray(value.tensor)
    self._AssertNumpyEq(n, bools)

  def testSummaryDescriptionAndDisplayName(self):
    with self.cached_session() as sess:

      def get_description(summary_op):
        summ_str = sess.run(summary_op)
        summ = summary_pb2.Summary()
        summ.ParseFromString(summ_str)
        return summ.value[0].metadata

      const = constant_op.constant(1)
      # Default case; no description or display name
      simple_summary = summary_ops.tensor_summary("simple", const)

      descr = get_description(simple_summary)
      self.assertEqual(descr.display_name, "")
      self.assertEqual(descr.summary_description, "")

      # Values are provided via function args
      with_values = summary_ops.tensor_summary(
          "simple",
          const,
          display_name="my name",
          summary_description="my description")

      descr = get_description(with_values)
      self.assertEqual(descr.display_name, "my name")
      self.assertEqual(descr.summary_description, "my description")

      # Values are provided via the SummaryMetadata arg
      metadata = summary_pb2.SummaryMetadata()
      metadata.display_name = "my name"
      metadata.summary_description = "my description"

      with_metadata = summary_ops.tensor_summary(
          "simple", const, summary_metadata=metadata)
      descr = get_description(with_metadata)
      self.assertEqual(descr.display_name, "my name")
      self.assertEqual(descr.summary_description, "my description")

      # If both SummaryMetadata and explicit args are provided, the args win
      overwrite = summary_ops.tensor_summary(
          "simple",
          const,
          summary_metadata=metadata,
          display_name="overwritten",
          summary_description="overwritten")
      descr = get_description(overwrite)
      self.assertEqual(descr.display_name, "overwritten")
      self.assertEqual(descr.summary_description, "overwritten")


if __name__ == "__main__":
  test.main()