Skip to content

Commit 1ba2581

Browse files
baiyfqingqing01
authored andcommitted
Unified bilinear_interp op Python interface specification (#10925)
* unify UpsamplingBilinear2d interface specification * unify UpsamplingBilinear2d interface specification * fix name conventions * small fix about computation order
1 parent 391c274 commit 1ba2581

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

doc/fluid/api/layers.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,9 +1003,9 @@ dice_loss
10031003
.. autofunction:: paddle.fluid.layers.dice_loss
10041004
:noindex:
10051005

1006-
bilinear_interp
1006+
upsampling_bilinear2d
10071007
____
10081008

1009-
.. autofunction:: paddle.fluid.layers.bilinear_interp
1009+
.. autofunction:: paddle.fluid.layers.upsampling_bilinear2d
10101010
:noindex:
10111011

python/paddle/fluid/layers/nn.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
'label_smooth',
8282
'roi_pool',
8383
'dice_loss',
84-
'bilinear_interp',
84+
'upsampling_bilinear2d',
8585
]
8686

8787

@@ -3917,8 +3917,10 @@ def dice_loss(input, label, epsilon=0.00001):
39173917
return reduce_mean(dice_score)
39183918

39193919

3920-
def bilinear_interp(input, out_h, out_w, name=None):
3920+
def upsampling_bilinear2d(input, out_shape=None, scale=None, name=None):
39213921
"""
3922+
The mathematical meaning of upsampling_bilinear2d is also called
3923+
Bilinear interpolation.
39223924
Bilinear interpolation is an extension of linear interpolation for
39233925
interpolating functions of two variables (e.g. H-direction and
39243926
W-direction in this layer) on a rectilinear 2D grid.
@@ -3930,8 +3932,13 @@ def bilinear_interp(input, out_h, out_w, name=None):
39303932
input (Variable): The input tensor of bilinear interpolation,
39313933
This is a 4-D tensor of the shape
39323934
(num_batches, channels, in_h, in_w).
3933-
out_h (int): output height of bilinear interpolation layer.
3934-
out_w (int): output width of bilinear interpolation layer.
3935+
out_shape(list|tuple|None): Output shape of bilinear interpolation
3936+
layer, the shape is (out_h, out_w).
3937+
Default: None
3938+
scale(int|None): The multiplier for the input height or width.
3939+
At least one of out_shape or scale must be set.
3940+
And out_shape has a higher priority than scale.
3941+
Default: None
39353942
name(str|None): A name for this layer(optional). If set None, the layer
39363943
will be named automatically.
39373944
@@ -3942,10 +3949,27 @@ def bilinear_interp(input, out_h, out_w, name=None):
39423949
Examples:
39433950
.. code-block:: python
39443951
3945-
out = fluid.layers.bilinear_interp(input, out_h=12, out_w=12)
3952+
out = fluid.layers.bilinear_interp(input, out_shape=[12, 12])
39463953
"""
3954+
if out_shape is None and scale is None:
3955+
raise ValueError("One of out_shape and scale must not be None")
39473956
helper = LayerHelper('bilinear_interp', **locals())
39483957
dtype = helper.input_dtype()
3958+
3959+
def _is_list_or_turple_(data):
3960+
return (isinstance(data, list) or isinstance(data, tuple))
3961+
3962+
if out_shape is not None:
3963+
if not (_is_list_or_turple_(out_shape) and len(out_shape) == 2):
3964+
raise ValueError('out_shape should be a list or tuple ',
3965+
'with length 2, (out_h, out_w).')
3966+
out_shape = list(map(int, out_shape))
3967+
out_h = out_shape[0]
3968+
out_w = out_shape[1]
3969+
else:
3970+
out_h = int(input.shape[2] * scale)
3971+
out_w = int(input.shape[3] * scale)
3972+
39493973
out = helper.create_tmp_variable(dtype)
39503974
helper.append_op(
39513975
type="bilinear_interp",

python/paddle/fluid/tests/unittests/test_layers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,13 @@ def test_roi_pool(self):
369369
self.assertIsNotNone(output)
370370
print(str(program))
371371

372-
def test_bilinear_interp(self):
372+
def test_upsampling_bilinear2d(self):
373373
program = Program()
374374
with program_guard(program):
375375
x = layers.data(name='x', shape=[3, 9, 6], dtype="float32")
376-
output = layers.bilinear_interp(x, 12, 12)
376+
output = layers.upsampling_bilinear2d(x, out_shape=[12, 12])
377+
self.assertIsNotNone(output)
378+
output = layers.upsampling_bilinear2d(x, scale=3)
377379
self.assertIsNotNone(output)
378380
print(str(program))
379381

0 commit comments

Comments
 (0)