1# Copyright 2016 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"""Tests for specs-related summarization functions."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import numpy as np
22
23from tensorflow.contrib.specs.python import specs
24from tensorflow.contrib.specs.python import summaries
25from tensorflow.python.framework import constant_op
26from tensorflow.python.ops import variables
27from tensorflow.python.platform import test
28
29
30def _rand(*size):
31  return np.random.uniform(size=size).astype("f")
32
33
34class SummariesTest(test.TestCase):
35
36  def testStructure(self):
37    with self.cached_session():
38      inputs_shape = (1, 18, 19, 5)
39      inputs = constant_op.constant(_rand(*inputs_shape))
40      spec = "net = Cr(64, [5, 5])"
41      outputs = specs.create_net(spec, inputs)
42      variables.global_variables_initializer().run()
43      result = outputs.eval()
44      self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
45      self.assertEqual(
46          summaries.tf_spec_structure(
47              spec, input_shape=inputs_shape),
48          "_ variablev2 conv variablev2 biasadd relu")
49
50  def testStructureFromTensor(self):
51    with self.cached_session():
52      inputs = constant_op.constant(_rand(1, 18, 19, 5))
53      spec = "net = Cr(64, [5, 5])"
54      outputs = specs.create_net(spec, inputs)
55      variables.global_variables_initializer().run()
56      result = outputs.eval()
57      self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
58      self.assertEqual(
59          summaries.tf_spec_structure(spec, inputs),
60          "_ variablev2 conv variablev2 biasadd relu")
61
62  def testPrint(self):
63    with self.cached_session():
64      inputs = constant_op.constant(_rand(1, 18, 19, 5))
65      spec = "net = Cr(64, [5, 5])"
66      outputs = specs.create_net(spec, inputs)
67      variables.global_variables_initializer().run()
68      result = outputs.eval()
69      self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
70      summaries.tf_spec_print(spec, inputs)
71
72  def testSummary(self):
73    with self.cached_session():
74      inputs = constant_op.constant(_rand(1, 18, 19, 5))
75      spec = "net = Cr(64, [5, 5])"
76      outputs = specs.create_net(spec, inputs)
77      variables.global_variables_initializer().run()
78      result = outputs.eval()
79      self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
80      summaries.tf_spec_summary(spec, inputs)
81
82
83if __name__ == "__main__":
84  test.main()
85