From 315fe040c4dd485fac007fc8625a445b262e5148 Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Fri, 20 Apr 2018 15:31:43 +0100 Subject: [PATCH 1/3] update super resolution from function to class --- docs/modules/layers.rst | 4 +- tensorlayer/layers/super_resolution.py | 109 ++++++++++--------------- 2 files changed, 45 insertions(+), 68 deletions(-) diff --git a/docs/modules/layers.rst b/docs/modules/layers.rst index 4718bc670..3f9b8f870 100644 --- a/docs/modules/layers.rst +++ b/docs/modules/layers.rst @@ -519,11 +519,11 @@ Super-Resolution layer 1D Subpixel Convolution ^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. autofunction:: SubpixelConv1d +.. autoclass:: SubpixelConv1d 2D Subpixel Convolution ^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. autofunction:: SubpixelConv2d +.. autoclass:: SubpixelConv2d Spatial Transformer diff --git a/tensorlayer/layers/super_resolution.py b/tensorlayer/layers/super_resolution.py index f00a6836b..dceb4166b 100644 --- a/tensorlayer/layers/super_resolution.py +++ b/tensorlayer/layers/super_resolution.py @@ -13,8 +13,7 @@ ] -@deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release -def subpixel_conv2d(prev_layer, scale=2, n_out_channel=None, act=tf.identity, name='subpixel_conv2d'): +class SubpixelConv2d(Layer): """It is a 2D sub-pixel up-sampling layer, usually be used for Super-Resolution applications, see `SRGAN `__ for example. @@ -33,11 +32,6 @@ def subpixel_conv2d(prev_layer, scale=2, n_out_channel=None, act=tf.identity, na name : str A unique layer name. - Returns - ------- - :class:`Layer` - A 2D sub-pixel up-sampling layer - Examples --------- >>> # examples here just want to tell you how to set the n_out_channel. @@ -71,51 +65,41 @@ def subpixel_conv2d(prev_layer, scale=2, n_out_channel=None, act=tf.identity, na """ # github/Tetrachrome/subpixel https://github.com/Tetrachrome/subpixel/blob/master/subpixel.py + @deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release + def __init__(self, prev_layer, scale=2, n_out_channel=None, act=tf.identity, name='subpixel_conv2d'): + _err_log = "SubpixelConv2d: The number of input channels == (scale x scale) x The number of output channels" - _err_log = "SubpixelConv2d: The number of input channels == (scale x scale) x The number of output channels" - - # scope_name = tf.get_variable_scope().name - # if scope_name: - # whole_name = scope_name + '/' + name - # else: - # whole_name = name - - def _PS(X, r, n_out_channels): - if n_out_channels >= 1: - assert int(X.get_shape()[-1]) == (r**2) * n_out_channels, _err_log + super(SubpixelConv2d, self).__init__(prev_layer=prev_layer, name=name) + logging.info("SubpixelConv2d %s: scale: %d n_out_channel: %s act: %s" % (name, scale, n_out_channel, act.__name__)) - # bsize, a, b, c = X.get_shape().as_list() - # bsize = tf.shape(X)[0] # Handling Dimension(None) type for undefined batch dim - # Xs=tf.split(X,r,3) #b*h*w*r*r - # Xr=tf.concat(Xs,2) #b*h*(r*w)*r - # X=tf.reshape(Xr,(bsize,r*a,r*b,n_out_channel)) # b*(r*h)*(r*w)*c + def _PS(X, r, n_out_channels): + if n_out_channels >= 1: + if int(X.get_shape()[-1]) != (r**2) * n_out_channels: + raise Exception(_err_log) + # bsize, a, b, c = X.get_shape().as_list() + # bsize = tf.shape(X)[0] # Handling Dimension(None) type for undefined batch dim + # Xs=tf.split(X,r,3) #b*h*w*r*r + # Xr=tf.concat(Xs,2) #b*h*(r*w)*r + # X=tf.reshape(Xr,(bsize,r*a,r*b,n_out_channel)) # b*(r*h)*(r*w)*c - X = tf.depth_to_space(X, r) - else: - logging.info(_err_log) - return X + X = tf.depth_to_space(X, r) + else: + logging.info(_err_log) + return X - inputs = prev_layer.outputs - if n_out_channel is None: - assert int(inputs.get_shape()[-1]) / (scale**2) % 1 == 0, _err_log - n_out_channel = int(int(inputs.get_shape()[-1]) / (scale**2)) + self.inputs = prev_layer.outputs + if n_out_channel is None: + if int(self.inputs.get_shape()[-1]) / (scale**2) % 1 != 0: + raise Exception(_err_log) + n_out_channel = int(int(self.inputs.get_shape()[-1]) / (scale**2)) - logging.info("SubpixelConv2d %s: scale: %d n_out_channel: %s act: %s" % (name, scale, n_out_channel, act.__name__)) + with tf.variable_scope(name): + self.outputs = act(_PS(self.inputs, r=scale, n_out_channels=n_out_channel)) - net_new = Layer(prev_layer=prev_layer, name=name) - # with tf.name_scope(name): - with tf.variable_scope(name): - net_new.outputs = act(_PS(inputs, r=scale, n_out_channels=n_out_channel)) + self.all_layers.append(self.outputs) - # net_new.all_layers = list(prev_layer.all_layers) - # net_new.all_params = list(prev_layer.all_params) - # net_new.all_drop = dict(prev_layer.all_drop) - net_new.all_layers.append(net_new.outputs) - return net_new - -@deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release -def subpixel_conv1d(prev_layer, scale=2, act=tf.identity, name='subpixel_conv1d'): +class SubpixelConv1d(Layer): """It is a 1D sub-pixel up-sampling layer. Calls a TensorFlow function that directly implements this functionality. @@ -132,11 +116,6 @@ def subpixel_conv1d(prev_layer, scale=2, act=tf.identity, name='subpixel_conv1d' name : str A unique layer name. - Returns - ------- - :class:`Layer` - A 1D sub-pixel up-sampling layer - Examples ---------- >>> t_signal = tf.placeholder('float32', [10, 100, 4], name='x') @@ -151,26 +130,24 @@ def subpixel_conv1d(prev_layer, scale=2, act=tf.identity, name='subpixel_conv1d' """ - def _PS(I, r): - X = tf.transpose(I, [2, 1, 0]) # (r, w, b) - X = tf.batch_to_space_nd(X, [r], [[0, 0]]) # (1, r*w, b) - X = tf.transpose(X, [2, 1, 0]) - return X + @deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release + def __init__(self, prev_layer, scale=2, act=tf.identity, name='subpixel_conv1d'): + def _PS(I, r): + X = tf.transpose(I, [2, 1, 0]) # (r, w, b) + X = tf.batch_to_space_nd(X, [r], [[0, 0]]) # (1, r*w, b) + X = tf.transpose(X, [2, 1, 0]) + return X - logging.info("SubpixelConv1d %s: scale: %d act: %s" % (name, scale, act.__name__)) + super(SubpixelConv1d, self).__init__(prev_layer=prev_layer, name=name) + logging.info("SubpixelConv1d %s: scale: %d act: %s" % (name, scale, act.__name__)) - inputs = prev_layer.outputs - net_new = Layer(prev_layer=prev_layer, name=name) - with tf.name_scope(name): - net_new.outputs = act(_PS(inputs, r=scale)) + self.inputs = prev_layer.outputs + with tf.name_scope(name): + self.outputs = act(_PS(self.inputs, r=scale)) - # net_new.all_layers = list(prev_layer.all_layers) - # net_new.all_params = list(prev_layer.all_params) - # net_new.all_drop = dict(prev_layer.all_drop) - net_new.all_layers.append(net_new.outputs) - return net_new + self.all_layers.append(self.outputs) # Alias -SubpixelConv2d = subpixel_conv2d -SubpixelConv1d = subpixel_conv1d +# SubpixelConv2d = subpixel_conv2d +# SubpixelConv1d = subpixel_conv1d From 0416dde565e2233cac2a6380ad4f33da9aa0a2ed Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Fri, 20 Apr 2018 16:39:29 +0100 Subject: [PATCH 2/3] fix docs --- tensorlayer/layers/core.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index 73b7f1919..b8642a51a 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -348,13 +348,16 @@ class Layer(object): Examples --------- + Define model + >>> x = tf.placeholder("float32", [None, 100]) >>> n = tl.layers.InputLayer(x, name='in') >>> n = tl.layers.DenseLayer(n, 80, name='d1') >>> n = tl.layers.DenseLayer(n, 80, name='d2') Get information + >>> print(n) ... Last layer is: DenseLayer (d2) [None, 80] >>> n.print_layers() @@ -370,11 +373,13 @@ class Layer(object): ... 14560 Slicing the outputs + >>> n2 = n[:, :30] >>> print(n2) ... Last layer is: Layer (d2) [None, 30] Iterating the outputs + >>> for l in n: >>> print(l) ... Tensor("d1/Identity:0", shape=(?, 80), dtype=float32) From 824c125e9a4ea91d8381cb4acd37cea846dd7aba Mon Sep 17 00:00:00 2001 From: zsdonghao Date: Fri, 20 Apr 2018 19:27:10 +0100 Subject: [PATCH 3/3] fix docs --- tensorlayer/layers/core.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index b8642a51a..ef9947e17 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -349,14 +349,14 @@ class Layer(object): Examples --------- - Define model + - Define model >>> x = tf.placeholder("float32", [None, 100]) >>> n = tl.layers.InputLayer(x, name='in') >>> n = tl.layers.DenseLayer(n, 80, name='d1') >>> n = tl.layers.DenseLayer(n, 80, name='d2') - Get information + - Get information >>> print(n) ... Last layer is: DenseLayer (d2) [None, 80] @@ -372,14 +372,14 @@ class Layer(object): >>> n.count_params() ... 14560 - Slicing the outputs + - Slicing the outputs >>> n2 = n[:, :30] >>> print(n2) ... Last layer is: Layer (d2) [None, 30] - Iterating the outputs - + - Iterating the outputs + >>> for l in n: >>> print(l) ... Tensor("d1/Identity:0", shape=(?, 80), dtype=float32)