scopes_test.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright 2016 Google Inc. 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 slim.scopes."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import tensorflow as tf
  20. from inception.slim import scopes
  21. @scopes.add_arg_scope
  22. def func1(*args, **kwargs):
  23. return (args, kwargs)
  24. @scopes.add_arg_scope
  25. def func2(*args, **kwargs):
  26. return (args, kwargs)
  27. class ArgScopeTest(tf.test.TestCase):
  28. def testEmptyArgScope(self):
  29. with self.test_session():
  30. self.assertEqual(scopes._current_arg_scope(), {})
  31. def testCurrentArgScope(self):
  32. func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
  33. key_op = (func1.__module__, func1.__name__)
  34. current_scope = {key_op: func1_kwargs.copy()}
  35. with self.test_session():
  36. with scopes.arg_scope([func1], a=1, b=None, c=[1]) as scope:
  37. self.assertDictEqual(scope, current_scope)
  38. def testCurrentArgScopeNested(self):
  39. func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
  40. func2_kwargs = {'b': 2, 'd': [2]}
  41. key = lambda f: (f.__module__, f.__name__)
  42. current_scope = {key(func1): func1_kwargs.copy(),
  43. key(func2): func2_kwargs.copy()}
  44. with self.test_session():
  45. with scopes.arg_scope([func1], a=1, b=None, c=[1]):
  46. with scopes.arg_scope([func2], b=2, d=[2]) as scope:
  47. self.assertDictEqual(scope, current_scope)
  48. def testReuseArgScope(self):
  49. func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
  50. key_op = (func1.__module__, func1.__name__)
  51. current_scope = {key_op: func1_kwargs.copy()}
  52. with self.test_session():
  53. with scopes.arg_scope([func1], a=1, b=None, c=[1]) as scope1:
  54. pass
  55. with scopes.arg_scope(scope1) as scope:
  56. self.assertDictEqual(scope, current_scope)
  57. def testReuseArgScopeNested(self):
  58. func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
  59. func2_kwargs = {'b': 2, 'd': [2]}
  60. key = lambda f: (f.__module__, f.__name__)
  61. current_scope1 = {key(func1): func1_kwargs.copy()}
  62. current_scope2 = {key(func1): func1_kwargs.copy(),
  63. key(func2): func2_kwargs.copy()}
  64. with self.test_session():
  65. with scopes.arg_scope([func1], a=1, b=None, c=[1]) as scope1:
  66. with scopes.arg_scope([func2], b=2, d=[2]) as scope2:
  67. pass
  68. with scopes.arg_scope(scope1):
  69. self.assertDictEqual(scopes._current_arg_scope(), current_scope1)
  70. with scopes.arg_scope(scope2):
  71. self.assertDictEqual(scopes._current_arg_scope(), current_scope2)
  72. def testSimpleArgScope(self):
  73. func1_args = (0,)
  74. func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
  75. with self.test_session():
  76. with scopes.arg_scope([func1], a=1, b=None, c=[1]):
  77. args, kwargs = func1(0)
  78. self.assertTupleEqual(args, func1_args)
  79. self.assertDictEqual(kwargs, func1_kwargs)
  80. def testSimpleArgScopeWithTuple(self):
  81. func1_args = (0,)
  82. func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
  83. with self.test_session():
  84. with scopes.arg_scope((func1,), a=1, b=None, c=[1]):
  85. args, kwargs = func1(0)
  86. self.assertTupleEqual(args, func1_args)
  87. self.assertDictEqual(kwargs, func1_kwargs)
  88. def testOverwriteArgScope(self):
  89. func1_args = (0,)
  90. func1_kwargs = {'a': 1, 'b': 2, 'c': [1]}
  91. with scopes.arg_scope([func1], a=1, b=None, c=[1]):
  92. args, kwargs = func1(0, b=2)
  93. self.assertTupleEqual(args, func1_args)
  94. self.assertDictEqual(kwargs, func1_kwargs)
  95. def testNestedArgScope(self):
  96. func1_args = (0,)
  97. func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
  98. with scopes.arg_scope([func1], a=1, b=None, c=[1]):
  99. args, kwargs = func1(0)
  100. self.assertTupleEqual(args, func1_args)
  101. self.assertDictEqual(kwargs, func1_kwargs)
  102. func1_kwargs['b'] = 2
  103. with scopes.arg_scope([func1], b=2):
  104. args, kwargs = func1(0)
  105. self.assertTupleEqual(args, func1_args)
  106. self.assertDictEqual(kwargs, func1_kwargs)
  107. def testSharedArgScope(self):
  108. func1_args = (0,)
  109. func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
  110. with scopes.arg_scope([func1, func2], a=1, b=None, c=[1]):
  111. args, kwargs = func1(0)
  112. self.assertTupleEqual(args, func1_args)
  113. self.assertDictEqual(kwargs, func1_kwargs)
  114. args, kwargs = func2(0)
  115. self.assertTupleEqual(args, func1_args)
  116. self.assertDictEqual(kwargs, func1_kwargs)
  117. def testSharedArgScopeTuple(self):
  118. func1_args = (0,)
  119. func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
  120. with scopes.arg_scope((func1, func2), a=1, b=None, c=[1]):
  121. args, kwargs = func1(0)
  122. self.assertTupleEqual(args, func1_args)
  123. self.assertDictEqual(kwargs, func1_kwargs)
  124. args, kwargs = func2(0)
  125. self.assertTupleEqual(args, func1_args)
  126. self.assertDictEqual(kwargs, func1_kwargs)
  127. def testPartiallySharedArgScope(self):
  128. func1_args = (0,)
  129. func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
  130. func2_args = (1,)
  131. func2_kwargs = {'a': 1, 'b': None, 'd': [2]}
  132. with scopes.arg_scope([func1, func2], a=1, b=None):
  133. with scopes.arg_scope([func1], c=[1]), scopes.arg_scope([func2], d=[2]):
  134. args, kwargs = func1(0)
  135. self.assertTupleEqual(args, func1_args)
  136. self.assertDictEqual(kwargs, func1_kwargs)
  137. args, kwargs = func2(1)
  138. self.assertTupleEqual(args, func2_args)
  139. self.assertDictEqual(kwargs, func2_kwargs)
  140. if __name__ == '__main__':
  141. tf.test.main()