Commit 73ae9c5a authored by Yuxin Wu's avatar Yuxin Wu

Add under_nam_socpe decorator

parent 42f2c644
...@@ -44,7 +44,8 @@ class GoogleNetResize(imgaug.ImageAugmentor): ...@@ -44,7 +44,8 @@ class GoogleNetResize(imgaug.ImageAugmentor):
out = img[y1:y1 + hh, x1:x1 + ww] out = img[y1:y1 + hh, x1:x1 + ww]
out = cv2.resize(out, (224, 224), interpolation=cv2.INTER_CUBIC) out = cv2.resize(out, (224, 224), interpolation=cv2.INTER_CUBIC)
return out return out
out = cv2.resize(img, (224, 224), interpolation=cv2.INTER_CUBIC) out = imgaug.ResizeShortestEdge(224, interp=cv2.INTER_CUBIC).augment(img)
out = imgaug.CenterCrop(224).augment(out)
return out return out
...@@ -73,7 +74,7 @@ def fbresnet_augmentor(isTrain): ...@@ -73,7 +74,7 @@ def fbresnet_augmentor(isTrain):
] ]
else: else:
augmentors = [ augmentors = [
imgaug.ResizeShortestEdge(256), imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC),
imgaug.CenterCrop((224, 224)), imgaug.CenterCrop((224, 224)),
] ]
return augmentors return augmentors
......
...@@ -87,8 +87,6 @@ def layer_register( ...@@ -87,8 +87,6 @@ def layer_register(
log_shape=False, log_shape=False,
use_scope=True): use_scope=True):
""" """
Register a layer.
Args: Args:
log_shape (bool): log input/output shape of this layer log_shape (bool): log input/output shape of this layer
use_scope (bool or None): use_scope (bool or None):
...@@ -97,6 +95,17 @@ def layer_register( ...@@ -97,6 +95,17 @@ def layer_register(
the scope name argument. the scope name argument.
It will try to figure out by checking if the first argument It will try to figure out by checking if the first argument
is string or not. is string or not.
Returns:
A decorator used to register a layer.
Examples:
.. code-block:: python
@layer_register(use_scope=True)
def add10(x):
return x + tf.get_variable('W', shape=[10])
""" """
def wrapper(func): def wrapper(func):
......
...@@ -14,8 +14,22 @@ __all__ = ['auto_reuse_variable_scope', 'cached_name_scope'] ...@@ -14,8 +14,22 @@ __all__ = ['auto_reuse_variable_scope', 'cached_name_scope']
def auto_reuse_variable_scope(func): def auto_reuse_variable_scope(func):
""" """
A decorator which automatically reuse the current variable scope if the A decorator which automatically reuses the current variable scope if the
function has been called with the same variable scope before. function has been called with the same variable scope before.
Examples:
.. code-block:: python
@auto_reuse_variable_scope
def myfunc(x):
return tf.layers.conv2d(x, 128, 3)
myfunc(x1) # will inherit parent scope reuse
myfunc(x2) # will reuse
with tf.variable_scope('newscope'):
myfunc(x3) # will inherit parent scope reuse
myfunc(x4) # will reuse
""" """
used_scope = set() used_scope = set()
...@@ -34,6 +48,35 @@ def auto_reuse_variable_scope(func): ...@@ -34,6 +48,35 @@ def auto_reuse_variable_scope(func):
return wrapper return wrapper
def under_name_scope():
"""
Returns:
A decorator which makes the function happen under a name scope,
which is named by the function itself.
Examples:
.. code-block:: python
@under_name_scope()
def rms(x):
return tf.sqrt( # will be under name scope 'rms'
tf.reduce_mean(tf.square(x)))
Todo:
Add a reuse option.
"""
def _impl(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
name = func.__name__
with tf.name_scope(name):
return func(*args, **kwargs)
return wrapper
return _impl
@graph_memoized @graph_memoized
def _get_cached_ns(name): def _get_cached_ns(name):
with tf.name_scope(None): with tf.name_scope(None):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment