scopes.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. """Contains the new arg_scope used for TF-Slim ops.
  16. Allows one to define models much more compactly by eliminating boilerplate
  17. code. This is accomplished through the use of argument scoping (arg_scope).
  18. Example of how to use scopes.arg_scope:
  19. with slim.arg_scope(ops.conv2d, padding='SAME',
  20. stddev=0.01, weight_decay=0.0005):
  21. net = ops.conv2d(inputs, 64, [11, 11], 4, padding='VALID', scope='conv1')
  22. net = ops.conv2d(net, 256, [5, 5], scope='conv2')
  23. The first call to conv2d will use predefined args:
  24. ops.conv2d(inputs, 64, [11, 11], 4, padding='VALID',
  25. stddev=0.01, weight_decay=0.0005, scope='conv1')
  26. The second call to Conv will overwrite padding:
  27. ops.conv2d(inputs, 256, [5, 5], padding='SAME',
  28. stddev=0.01, weight_decay=0.0005, scope='conv2')
  29. Example of how to use scopes.add_arg_scope:
  30. @scopes.add_arg_scope
  31. def conv2d(*args, **kwargs)
  32. """
  33. from __future__ import absolute_import
  34. from __future__ import division
  35. from __future__ import print_function
  36. import contextlib
  37. import functools
  38. from tensorflow.python.framework import ops
  39. _ARGSTACK_KEY = ("__arg_stack",)
  40. _DECORATED_OPS = set()
  41. def _get_arg_stack():
  42. stack = ops.get_collection(_ARGSTACK_KEY)
  43. if stack:
  44. return stack[0]
  45. else:
  46. stack = [{}]
  47. ops.add_to_collection(_ARGSTACK_KEY, stack)
  48. return stack
  49. def _current_arg_scope():
  50. stack = _get_arg_stack()
  51. return stack[-1]
  52. def _add_op(op):
  53. key_op = (op.__module__, op.__name__)
  54. if key_op not in _DECORATED_OPS:
  55. _DECORATED_OPS.add(key_op)
  56. @contextlib.contextmanager
  57. def arg_scope(list_ops, **kwargs):
  58. """Stores the default arguments for the given set of list_ops.
  59. Args:
  60. list_ops: List or tuple of operations to set argument scope for. Every op in
  61. list_ops need to be decorated with @add_arg_scope to work.
  62. **kwargs: keyword=value that will define the defaults for each op in
  63. list_ops. All the ops need to accept the given set of arguments.
  64. Yields:
  65. the current_scope, which is a dictionary of {op: {arg: value}}
  66. Raises:
  67. TypeError: if list_ops is not a list or a tuple.
  68. ValueError: if any op in list_ops has not be decorated with @add_arg_scope.
  69. """
  70. if not isinstance(list_ops, (list, tuple)):
  71. raise TypeError("list_ops is not a list or a tuple")
  72. try:
  73. current_scope = _current_arg_scope().copy()
  74. for op in list_ops:
  75. key_op = (op.__module__, op.__name__)
  76. if not has_arg_scope(op):
  77. raise ValueError("%s is not decorated with @add_arg_scope", key_op)
  78. if key_op in current_scope:
  79. current_kwargs = current_scope[key_op].copy()
  80. current_kwargs.update(kwargs)
  81. current_scope[key_op] = current_kwargs
  82. else:
  83. current_scope[key_op] = kwargs.copy()
  84. _get_arg_stack().append(current_scope)
  85. yield current_scope
  86. finally:
  87. _get_arg_stack().pop()
  88. def add_arg_scope(func):
  89. """Decorates a function with args so it can be used within an arg_scope.
  90. Args:
  91. func: function to decorate.
  92. Returns:
  93. A tuple with the decorated function func_with_args().
  94. """
  95. @functools.wraps(func)
  96. def func_with_args(*args, **kwargs):
  97. current_scope = _current_arg_scope()
  98. current_args = kwargs
  99. key_func = (func.__module__, func.__name__)
  100. if key_func in current_scope:
  101. current_args = current_scope[key_func].copy()
  102. current_args.update(kwargs)
  103. return func(*args, **current_args)
  104. _add_op(func)
  105. return func_with_args
  106. def has_arg_scope(func):
  107. """Checks whether a func has been decorated with @add_arg_scope or not.
  108. Args:
  109. func: function to check.
  110. Returns:
  111. a boolean.
  112. """
  113. key_op = (func.__module__, func.__name__)
  114. return key_op in _DECORATED_OPS