diff --git a/doc/fluid/api/layers.rst b/doc/fluid/api/layers.rst index 9ae7ffb2604250aebfd9ecd8966384c3ef05f97b..709ddc64a611ac24d1142ebfd1f2f17333ccda0a 100644 --- a/doc/fluid/api/layers.rst +++ b/doc/fluid/api/layers.rst @@ -834,4 +834,8 @@ dice_loss .. autofunction:: paddle.fluid.layers.dice_loss :noindex: +bilinear_interp +____ +.. autofunction:: paddle.fluid.layers.bilinear_interp + :noindex: diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index dd360c2b98414d1cf2c0da5f7c8d5c6ca461a22a..04ee8ac9aee92a0e161e83bf1bb34d3ce727a0fb 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -81,6 +81,7 @@ __all__ = [ 'label_smooth', 'roi_pool', 'dice_loss', + 'bilinear_interp', ] @@ -3852,6 +3853,8 @@ def roi_pool(input, rois, pooled_height=1, pooled_width=1, spatial_scale=1.0): (num_rois, channels, pooled_h, pooled_w). Examples: + .. code-block:: python + pool_out = fluid.layers.roi_pool(input=x, rois=rois, 7, 7, 1.0) """ helper = LayerHelper('roi_pool', **locals()) @@ -3899,6 +3902,8 @@ def dice_loss(input, label, epsilon=0.00001): dice_loss (Variable): The dice loss with shape [1]. Examples: + .. code-block:: python + predictions = fluid.layers.softmax(x) loss = fluid.layers.dice_loss(input=predictions, label=label, 2) """ @@ -3910,3 +3915,42 @@ def dice_loss(input, label, epsilon=0.00001): label, dim=reduce_dim) dice_score = 1 - inse * 2 / (dice_denominator + epsilon) return reduce_mean(dice_score) + + +def bilinear_interp(input, out_h, out_w, name=None): + """ + 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. + + For details, please refer to Wikipedia: + https://en.wikipedia.org/wiki/Bilinear_interpolation + + Args: + 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. + name(str|None): A name for this layer(optional). If set None, the layer + will be named automatically. + + Returns: + out (Variable): The output is a 4-D tensor of the shape + (num_batches, channls, out_h, out_w). + + Examples: + .. code-block:: python + + out = fluid.layers.bilinear_interp(input, out_h=12, out_w=12) + """ + helper = LayerHelper('bilinear_interp', **locals()) + dtype = helper.input_dtype() + out = helper.create_tmp_variable(dtype) + helper.append_op( + type="bilinear_interp", + inputs={"X": input}, + outputs={"Out": out}, + attrs={"out_h": out_h, + "out_w": out_w}) + return out diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index c5414abf0fee6b686dccf7c97e9c6d5408ecf62a..c44ac59ccdb7fa212ab2a8ab83ee0c70fc498f9f 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -369,6 +369,14 @@ class TestBook(unittest.TestCase): self.assertIsNotNone(output) print(str(program)) + def test_bilinear_interp(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) + self.assertIsNotNone(output) + print(str(program)) + if __name__ == '__main__': unittest.main()