提交 1ba2581a 编写于 作者: B baiyf 提交者: qingqing01

Unified bilinear_interp op Python interface specification (#10925)

* unify UpsamplingBilinear2d interface specification

* unify UpsamplingBilinear2d interface specification

* fix name conventions

* small fix about computation order
上级 391c2749
...@@ -1003,9 +1003,9 @@ dice_loss ...@@ -1003,9 +1003,9 @@ dice_loss
.. autofunction:: paddle.fluid.layers.dice_loss .. autofunction:: paddle.fluid.layers.dice_loss
:noindex: :noindex:
bilinear_interp upsampling_bilinear2d
____ ____
.. autofunction:: paddle.fluid.layers.bilinear_interp .. autofunction:: paddle.fluid.layers.upsampling_bilinear2d
:noindex: :noindex:
...@@ -81,7 +81,7 @@ __all__ = [ ...@@ -81,7 +81,7 @@ __all__ = [
'label_smooth', 'label_smooth',
'roi_pool', 'roi_pool',
'dice_loss', 'dice_loss',
'bilinear_interp', 'upsampling_bilinear2d',
] ]
...@@ -3917,8 +3917,10 @@ def dice_loss(input, label, epsilon=0.00001): ...@@ -3917,8 +3917,10 @@ def dice_loss(input, label, epsilon=0.00001):
return reduce_mean(dice_score) 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 Bilinear interpolation is an extension of linear interpolation for
interpolating functions of two variables (e.g. H-direction and interpolating functions of two variables (e.g. H-direction and
W-direction in this layer) on a rectilinear 2D grid. W-direction in this layer) on a rectilinear 2D grid.
...@@ -3930,8 +3932,13 @@ def bilinear_interp(input, out_h, out_w, name=None): ...@@ -3930,8 +3932,13 @@ def bilinear_interp(input, out_h, out_w, name=None):
input (Variable): The input tensor of bilinear interpolation, input (Variable): The input tensor of bilinear interpolation,
This is a 4-D tensor of the shape This is a 4-D tensor of the shape
(num_batches, channels, in_h, in_w). (num_batches, channels, in_h, in_w).
out_h (int): output height of bilinear interpolation layer. out_shape(list|tuple|None): Output shape of bilinear interpolation
out_w (int): output width of bilinear interpolation layer. 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 name(str|None): A name for this layer(optional). If set None, the layer
will be named automatically. will be named automatically.
...@@ -3942,10 +3949,27 @@ def bilinear_interp(input, out_h, out_w, name=None): ...@@ -3942,10 +3949,27 @@ def bilinear_interp(input, out_h, out_w, name=None):
Examples: Examples:
.. code-block:: python .. 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()) helper = LayerHelper('bilinear_interp', **locals())
dtype = helper.input_dtype() 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) out = helper.create_tmp_variable(dtype)
helper.append_op( helper.append_op(
type="bilinear_interp", type="bilinear_interp",
......
...@@ -369,11 +369,13 @@ class TestBook(unittest.TestCase): ...@@ -369,11 +369,13 @@ class TestBook(unittest.TestCase):
self.assertIsNotNone(output) self.assertIsNotNone(output)
print(str(program)) print(str(program))
def test_bilinear_interp(self): def test_upsampling_bilinear2d(self):
program = Program() program = Program()
with program_guard(program): with program_guard(program):
x = layers.data(name='x', shape=[3, 9, 6], dtype="float32") 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) self.assertIsNotNone(output)
print(str(program)) print(str(program))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册