Skip to content

Commit e1599eb

Browse files
committed
Add image_resize_short and refine API
1 parent 2381249 commit e1599eb

File tree

1 file changed

+73
-15
lines changed
  • python/paddle/fluid/layers

1 file changed

+73
-15
lines changed

python/paddle/fluid/layers/nn.py

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3929,22 +3929,25 @@ def dice_loss(input, label, epsilon=0.00001):
39293929
return reduce_mean(dice_score)
39303930

39313931

3932-
def resize_bilinear(input, out_shape=None, scale=None, name=None):
3932+
def image_resize(input,
3933+
out_shape=None,
3934+
scale=None,
3935+
name=None,
3936+
resample='BILINEAR'):
39333937
"""
3934-
The mathematical meaning of resize bilinear layer is
3935-
Bilinear interpolation.
3936-
Bilinear interpolation is an extension of linear interpolation for
3937-
interpolating functions of two variables (e.g. H-direction and
3938-
W-direction in this layer) on a rectilinear 2D grid.
3938+
Resize a batch of images.
39393939
3940-
For details, please refer to Wikipedia:
3941-
https://en.wikipedia.org/wiki/Bilinear_interpolation
3940+
The input must be a tensor of the shape (num_batches, channels, in_h, in_w),
3941+
and the resizing only applies on the last two dimensions(hight and width).
3942+
3943+
Supporting resample methods:
3944+
'BILINEAR' : Bilinear interpolation
39423945
39433946
Args:
3944-
input (Variable): The input tensor of resize bilinear layer,
3947+
input (Variable): The input tensor of image resize layer,
39453948
This is a 4-D tensor of the shape
39463949
(num_batches, channels, in_h, in_w).
3947-
out_shape(list|tuple|Variable|None): Output shape of resize bilinear
3950+
out_shape(list|tuple|Variable|None): Output shape of image resize
39483951
layer, the shape is (out_h, out_w).
39493952
Default: None
39503953
scale(float|None): The multiplier for the input height or width.
@@ -3953,6 +3956,8 @@ def resize_bilinear(input, out_shape=None, scale=None, name=None):
39533956
Default: None
39543957
name(str|None): A name for this layer(optional). If set None, the layer
39553958
will be named automatically.
3959+
resample(str): The resample method. It can only be 'BILINEAR' currently.
3960+
Default: 'BILINEAR'
39563961
39573962
Returns:
39583963
out (Variable): The output is a 4-D tensor of the shape
@@ -3961,8 +3966,12 @@ def resize_bilinear(input, out_shape=None, scale=None, name=None):
39613966
Examples:
39623967
.. code-block:: python
39633968
3964-
out = fluid.layers.resize_bilinear(input, out_shape=[12, 12])
3969+
out = fluid.layers.image_resize(input, out_shape=[12, 12])
39653970
"""
3971+
resample_methods = {'BILINEAR': 'bilinear_interp'}
3972+
if resample not in resample_methods:
3973+
raise ValueError(
3974+
"The 'resample' of image_resize can only be 'BILINEAR' currently.")
39663975
if out_shape is None and scale is None:
39673976
raise ValueError("One of out_shape and scale must not be None")
39683977
helper = LayerHelper('bilinear_interp', **locals())
@@ -3990,31 +3999,80 @@ def _is_list_or_turple_(data):
39903999

39914000
out = helper.create_tmp_variable(dtype)
39924001
helper.append_op(
3993-
type="bilinear_interp",
4002+
type=resample_methods[resample],
39944003
inputs=inputs,
39954004
outputs={"Out": out},
39964005
attrs={"out_h": out_h,
39974006
"out_w": out_w})
39984007
return out
39994008

40004009

4010+
def resize_bilinear(input, out_shape=None, scale=None, name=None):
4011+
"""
4012+
This is an alias of layer 'image_resize' with bilinear interpolation.
4013+
4014+
The mathematical meaning of resize bilinear layer is
4015+
Bilinear interpolation.
4016+
Bilinear interpolation is an extension of linear interpolation for
4017+
interpolating functions of two variables (e.g. H-direction and
4018+
W-direction in this layer) on a rectilinear 2D grid.
4019+
4020+
For details, please refer to Wikipedia:
4021+
https://en.wikipedia.org/wiki/Bilinear_interpolation
4022+
"""
4023+
4024+
return image_resize(input, out_shape, scale, name, 'BILINEAR')
4025+
4026+
4027+
def image_resize_short(input, out_short_len, resample='BILINEAR'):
4028+
"""
4029+
Resize a batch of images. The short edge of input images will be
4030+
resized to the given 'out_short_len'. The long edge of input images
4031+
will be resized proportionately to make images' length-width ratio
4032+
constant.
4033+
4034+
Args:
4035+
input (Variable): The input tensor of image resize layer,
4036+
This is a 4-D tensor of the shape
4037+
(num_batches, channels, in_h, in_w).
4038+
out_short_len(int): The length of output images' short edge.
4039+
4040+
Returns:
4041+
out (Variable): The output is a 4-D tensor of the shape
4042+
(num_batches, channls, out_h, out_w).
4043+
"""
4044+
in_shape = input.shape
4045+
if len(in_shape) != 4:
4046+
raise ValueError(
4047+
"The rank of input must be 4 (num_batches, channels, in_h, in_w).")
4048+
hw = in_shape[2:4]
4049+
short_idx = hw.index(min(hw))
4050+
long_idx = 1 - short_idx
4051+
out_shape = list(hw)
4052+
out_shape[short_idx] = out_short_len
4053+
out_shape[long_idx] = round(
4054+
float(out_shape[long_idx]) *
4055+
(float(out_short_len) / float(hw[short_idx])))
4056+
return image_resize(input=input, out_shape=out_shape, resample=resample)
4057+
4058+
40014059
def gather(input, index):
40024060
"""
40034061
Output is obtained by gathering entries of the outer-most dimension
40044062
of X indexed by `index` and concatenate them together.
40054063
40064064
.. math::
40074065
4008-
Out = X[Index]
4066+
Out = X[Index]
40094067
40104068
40114069
.. code-block:: text
40124070
40134071
40144072
Given:
40154073
4016-
X = [[1, 2],
4017-
[3, 4],
4074+
X = [[1, 2],
4075+
[3, 4],
40184076
[5, 6]]
40194077
40204078
Index = [1, 2]

0 commit comments

Comments
 (0)