From 1ba2581a52438ef1071d5c42d24341898b0ab675 Mon Sep 17 00:00:00 2001 From: baiyf Date: Fri, 25 May 2018 21:10:02 +0800 Subject: [PATCH] Unified bilinear_interp op Python interface specification (#10925) * unify UpsamplingBilinear2d interface specification * unify UpsamplingBilinear2d interface specification * fix name conventions * small fix about computation order --- doc/fluid/api/layers.rst | 4 +-- python/paddle/fluid/layers/nn.py | 34 ++++++++++++++++--- .../fluid/tests/unittests/test_layers.py | 6 ++-- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/doc/fluid/api/layers.rst b/doc/fluid/api/layers.rst index 91449042fc..f53da4d194 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 +upsampling_bilinear2d ____ -.. autofunction:: paddle.fluid.layers.bilinear_interp +.. autofunction:: paddle.fluid.layers.upsampling_bilinear2d :noindex: diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 04ee8ac9ae..b6c47aa9a6 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -81,7 +81,7 @@ __all__ = [ 'label_smooth', 'roi_pool', 'dice_loss', - 'bilinear_interp', + 'upsampling_bilinear2d', ] @@ -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 upsampling_bilinear2d(input, out_shape=None, scale=None, name=None): """ + 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 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,27 @@ 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: + out_h = int(input.shape[2] * scale) + out_w = int(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 c44ac59ccd..60dc1f83fc 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -369,11 +369,13 @@ class TestBook(unittest.TestCase): self.assertIsNotNone(output) print(str(program)) - def test_bilinear_interp(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.bilinear_interp(x, 12, 12) + output = layers.upsampling_bilinear2d(x, out_shape=[12, 12]) + self.assertIsNotNone(output) + output = layers.upsampling_bilinear2d(x, scale=3) self.assertIsNotNone(output) print(str(program)) -- GitLab