@@ -3929,22 +3929,25 @@ def dice_loss(input, label, epsilon=0.00001):
3929
3929
return reduce_mean (dice_score )
3930
3930
3931
3931
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' ):
3933
3937
"""
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.
3939
3939
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
3942
3945
3943
3946
Args:
3944
- input (Variable): The input tensor of resize bilinear layer,
3947
+ input (Variable): The input tensor of image resize layer,
3945
3948
This is a 4-D tensor of the shape
3946
3949
(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
3948
3951
layer, the shape is (out_h, out_w).
3949
3952
Default: None
3950
3953
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):
3953
3956
Default: None
3954
3957
name(str|None): A name for this layer(optional). If set None, the layer
3955
3958
will be named automatically.
3959
+ resample(str): The resample method. It can only be 'BILINEAR' currently.
3960
+ Default: 'BILINEAR'
3956
3961
3957
3962
Returns:
3958
3963
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):
3961
3966
Examples:
3962
3967
.. code-block:: python
3963
3968
3964
- out = fluid.layers.resize_bilinear (input, out_shape=[12, 12])
3969
+ out = fluid.layers.image_resize (input, out_shape=[12, 12])
3965
3970
"""
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." )
3966
3975
if out_shape is None and scale is None :
3967
3976
raise ValueError ("One of out_shape and scale must not be None" )
3968
3977
helper = LayerHelper ('bilinear_interp' , ** locals ())
@@ -3990,31 +3999,80 @@ def _is_list_or_turple_(data):
3990
3999
3991
4000
out = helper .create_tmp_variable (dtype )
3992
4001
helper .append_op (
3993
- type = "bilinear_interp" ,
4002
+ type = resample_methods [ resample ] ,
3994
4003
inputs = inputs ,
3995
4004
outputs = {"Out" : out },
3996
4005
attrs = {"out_h" : out_h ,
3997
4006
"out_w" : out_w })
3998
4007
return out
3999
4008
4000
4009
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
+
4001
4059
def gather (input , index ):
4002
4060
"""
4003
4061
Output is obtained by gathering entries of the outer-most dimension
4004
4062
of X indexed by `index` and concatenate them together.
4005
4063
4006
4064
.. math::
4007
4065
4008
- Out = X[Index]
4066
+ Out = X[Index]
4009
4067
4010
4068
4011
4069
.. code-block:: text
4012
4070
4013
4071
4014
4072
Given:
4015
4073
4016
- X = [[1, 2],
4017
- [3, 4],
4074
+ X = [[1, 2],
4075
+ [3, 4],
4018
4076
[5, 6]]
4019
4077
4020
4078
Index = [1, 2]
0 commit comments