From 94a586c4a57027e46d5849a2c13304bfb2ad8c32 Mon Sep 17 00:00:00 2001 From: baiyfbupt Date: Fri, 25 May 2018 11:21:33 +0800 Subject: [PATCH 1/4] unify UpsamplingBilinear2d interface specification --- doc/fluid/api/layers.rst | 4 +-- python/paddle/fluid/layers/nn.py | 36 ++++++++++++++++--- .../fluid/tests/unittests/test_layers.py | 5 +-- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/doc/fluid/api/layers.rst b/doc/fluid/api/layers.rst index 91449042fcdfd..a7b1ac0f9ffaa 100644 --- a/doc/fluid/api/layers.rst +++ b/doc/fluid/api/layers.rst @@ -1003,9 +1003,9 @@ dice_loss .. autofunction:: paddle.fluid.layers.dice_loss :noindex: -bilinear_interp +UpsamplingBilinear2d ____ -.. autofunction:: paddle.fluid.layers.bilinear_interp +.. autofunction:: paddle.fluid.layers.UpsamplingBilinear2d :noindex: diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 04ee8ac9aee92..ec8ebd637e158 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -81,7 +81,7 @@ 'label_smooth', 'roi_pool', 'dice_loss', - 'bilinear_interp', + 'UpsamplingBilinear2d', ] @@ -3917,8 +3917,10 @@ def dice_loss(input, label, epsilon=0.00001): return reduce_mean(dice_score) -def bilinear_interp(input, out_h, out_w, name=None): +def UpsamplingBilinear2d(input, out_shape=None, scale=None, name=None): """ + The mathematical meaning of UpsamplingBilinear2d is alse called + Bilinear interpolation. Bilinear interpolation is an extension of linear interpolation for interpolating functions of two variables (e.g. H-direction and W-direction in this layer) on a rectilinear 2D grid. @@ -3930,8 +3932,13 @@ def bilinear_interp(input, out_h, out_w, name=None): input (Variable): The input tensor of bilinear interpolation, This is a 4-D tensor of the shape (num_batches, channels, in_h, in_w). - out_h (int): output height of bilinear interpolation layer. - out_w (int): output width of bilinear interpolation layer. + out_shape(list|tuple|None): Output shape of bilinear interpolation + layer, the shape is (out_h, out_w). + Default: None + scale(int|None): The multiplier for the input height or width. + At least one of out_shape or scale must be set. + And out_shape has a higher priority than scale. + Default: None name(str|None): A name for this layer(optional). If set None, the layer will be named automatically. @@ -3942,10 +3949,29 @@ def bilinear_interp(input, out_h, out_w, name=None): Examples: .. code-block:: python - out = fluid.layers.bilinear_interp(input, out_h=12, out_w=12) + out = fluid.layers.bilinear_interp(input, out_shape=[12, 12]) """ + if out_shape is None and scale is None: + raise ValueError("One of out_shape and scale must not be None") helper = LayerHelper('bilinear_interp', **locals()) dtype = helper.input_dtype() + + def _is_list_or_turple_(data): + return (isinstance(data, list) or isinstance(data, tuple)) + + if out_shape is not None: + if not (_is_list_or_turple_(out_shape) and len(out_shape) == 2): + raise ValueError('out_shape should be a list or tuple ', + 'with length 2, (out_h, out_w).') + out_shape = list(map(int, out_shape)) + out_h = out_shape[0] + out_w = out_shape[1] + else: + if not isinstance(scale, int): + scale = int(scale) + out_h = input.shape[2] * scale + out_w = input.shape[3] * scale + out = helper.create_tmp_variable(dtype) helper.append_op( type="bilinear_interp", diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index c44ac59ccdb7f..1f18f94cfe713 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -369,12 +369,13 @@ def test_roi_pool(self): self.assertIsNotNone(output) print(str(program)) - def test_bilinear_interp(self): + def test_UpsamplingBilinear2d(self): program = Program() with program_guard(program): x = layers.data(name='x', shape=[3, 9, 6], dtype="float32") - output = layers.bilinear_interp(x, 12, 12) + output = layers.UpsamplingBilinear2d(x, out_shape=[12, 12]) self.assertIsNotNone(output) + output = layers.UpsamplingBilinear2d(x, scale=3) print(str(program)) From 8addfe208899302a6bf8f23fba6b9454c24329e0 Mon Sep 17 00:00:00 2001 From: baiyfbupt Date: Fri, 25 May 2018 11:25:51 +0800 Subject: [PATCH 2/4] unify UpsamplingBilinear2d interface specification --- python/paddle/fluid/tests/unittests/test_layers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 1f18f94cfe713..1ae8aca211227 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -376,6 +376,7 @@ def test_UpsamplingBilinear2d(self): output = layers.UpsamplingBilinear2d(x, out_shape=[12, 12]) self.assertIsNotNone(output) output = layers.UpsamplingBilinear2d(x, scale=3) + self.assertIsNotNone(output) print(str(program)) From fe5cd7e514b59a563e5bf5f0c1d3b3f4b0406dd4 Mon Sep 17 00:00:00 2001 From: baiyfbupt Date: Fri, 25 May 2018 11:41:24 +0800 Subject: [PATCH 3/4] fix name conventions --- doc/fluid/api/layers.rst | 4 ++-- python/paddle/fluid/layers/nn.py | 6 +++--- python/paddle/fluid/tests/unittests/test_layers.py | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/fluid/api/layers.rst b/doc/fluid/api/layers.rst index a7b1ac0f9ffaa..f53da4d194f8d 100644 --- a/doc/fluid/api/layers.rst +++ b/doc/fluid/api/layers.rst @@ -1003,9 +1003,9 @@ dice_loss .. autofunction:: paddle.fluid.layers.dice_loss :noindex: -UpsamplingBilinear2d +upsampling_bilinear2d ____ -.. autofunction:: paddle.fluid.layers.UpsamplingBilinear2d +.. autofunction:: paddle.fluid.layers.upsampling_bilinear2d :noindex: diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index ec8ebd637e158..2172e720bdfd8 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -81,7 +81,7 @@ 'label_smooth', 'roi_pool', 'dice_loss', - 'UpsamplingBilinear2d', + 'upsampling_bilinear2d', ] @@ -3917,9 +3917,9 @@ def dice_loss(input, label, epsilon=0.00001): return reduce_mean(dice_score) -def UpsamplingBilinear2d(input, out_shape=None, scale=None, name=None): +def upsampling_bilinear2d(input, out_shape=None, scale=None, name=None): """ - The mathematical meaning of UpsamplingBilinear2d is alse called + The mathematical meaning of upsampling_bilinear2d is also called Bilinear interpolation. Bilinear interpolation is an extension of linear interpolation for interpolating functions of two variables (e.g. H-direction and diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 1ae8aca211227..60dc1f83fc32e 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -369,13 +369,13 @@ def test_roi_pool(self): self.assertIsNotNone(output) print(str(program)) - def test_UpsamplingBilinear2d(self): + def test_upsampling_bilinear2d(self): program = Program() with program_guard(program): x = layers.data(name='x', shape=[3, 9, 6], dtype="float32") - output = layers.UpsamplingBilinear2d(x, out_shape=[12, 12]) + output = layers.upsampling_bilinear2d(x, out_shape=[12, 12]) self.assertIsNotNone(output) - output = layers.UpsamplingBilinear2d(x, scale=3) + output = layers.upsampling_bilinear2d(x, scale=3) self.assertIsNotNone(output) print(str(program)) From 42297563bb6d2624638f68176f4c6b3b961c70b9 Mon Sep 17 00:00:00 2001 From: baiyfbupt Date: Fri, 25 May 2018 16:20:57 +0800 Subject: [PATCH 4/4] small fix about computation order --- python/paddle/fluid/layers/nn.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 2172e720bdfd8..b6c47aa9a65b9 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -3967,10 +3967,8 @@ def _is_list_or_turple_(data): out_h = out_shape[0] out_w = out_shape[1] else: - if not isinstance(scale, int): - scale = int(scale) - out_h = input.shape[2] * scale - out_w = input.shape[3] * scale + out_h = int(input.shape[2] * scale) + out_w = int(input.shape[3] * scale) out = helper.create_tmp_variable(dtype) helper.append_op(