81
81
'label_smooth' ,
82
82
'roi_pool' ,
83
83
'dice_loss' ,
84
+ 'image_resize' ,
85
+ 'image_resize_short' ,
84
86
'resize_bilinear' ,
85
87
'gather' ,
86
88
'random_crop' ,
@@ -3929,22 +3931,25 @@ def dice_loss(input, label, epsilon=0.00001):
3929
3931
return reduce_mean (dice_score )
3930
3932
3931
3933
3932
- def resize_bilinear (input , out_shape = None , scale = None , name = None ):
3934
+ def image_resize (input ,
3935
+ out_shape = None ,
3936
+ scale = None ,
3937
+ name = None ,
3938
+ resample = 'BILINEAR' ):
3933
3939
"""
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.
3940
+ Resize a batch of images.
3939
3941
3940
- For details, please refer to Wikipedia:
3941
- https://en.wikipedia.org/wiki/Bilinear_interpolation
3942
+ The input must be a tensor of the shape (num_batches, channels, in_h, in_w),
3943
+ and the resizing only applies on the last two dimensions(hight and width).
3944
+
3945
+ Supporting resample methods:
3946
+ 'BILINEAR' : Bilinear interpolation
3942
3947
3943
3948
Args:
3944
- input (Variable): The input tensor of resize bilinear layer,
3949
+ input (Variable): The input tensor of image resize layer,
3945
3950
This is a 4-D tensor of the shape
3946
3951
(num_batches, channels, in_h, in_w).
3947
- out_shape(list|tuple|Variable|None): Output shape of resize bilinear
3952
+ out_shape(list|tuple|Variable|None): Output shape of image resize
3948
3953
layer, the shape is (out_h, out_w).
3949
3954
Default: None
3950
3955
scale(float|None): The multiplier for the input height or width.
@@ -3953,6 +3958,8 @@ def resize_bilinear(input, out_shape=None, scale=None, name=None):
3953
3958
Default: None
3954
3959
name(str|None): A name for this layer(optional). If set None, the layer
3955
3960
will be named automatically.
3961
+ resample(str): The resample method. It can only be 'BILINEAR' currently.
3962
+ Default: 'BILINEAR'
3956
3963
3957
3964
Returns:
3958
3965
out (Variable): The output is a 4-D tensor of the shape
@@ -3961,8 +3968,12 @@ def resize_bilinear(input, out_shape=None, scale=None, name=None):
3961
3968
Examples:
3962
3969
.. code-block:: python
3963
3970
3964
- out = fluid.layers.resize_bilinear (input, out_shape=[12, 12])
3971
+ out = fluid.layers.image_resize (input, out_shape=[12, 12])
3965
3972
"""
3973
+ resample_methods = {'BILINEAR' : 'bilinear_interp' }
3974
+ if resample not in resample_methods :
3975
+ raise ValueError (
3976
+ "The 'resample' of image_resize can only be 'BILINEAR' currently." )
3966
3977
if out_shape is None and scale is None :
3967
3978
raise ValueError ("One of out_shape and scale must not be None" )
3968
3979
helper = LayerHelper ('bilinear_interp' , ** locals ())
@@ -3990,31 +4001,80 @@ def _is_list_or_turple_(data):
3990
4001
3991
4002
out = helper .create_tmp_variable (dtype )
3992
4003
helper .append_op (
3993
- type = "bilinear_interp" ,
4004
+ type = resample_methods [ resample ] ,
3994
4005
inputs = inputs ,
3995
4006
outputs = {"Out" : out },
3996
4007
attrs = {"out_h" : out_h ,
3997
4008
"out_w" : out_w })
3998
4009
return out
3999
4010
4000
4011
4012
+ def resize_bilinear (input , out_shape = None , scale = None , name = None ):
4013
+ """
4014
+ This is an alias of layer 'image_resize' with bilinear interpolation.
4015
+
4016
+ The mathematical meaning of resize bilinear layer is
4017
+ Bilinear interpolation.
4018
+ Bilinear interpolation is an extension of linear interpolation for
4019
+ interpolating functions of two variables (e.g. H-direction and
4020
+ W-direction in this layer) on a rectilinear 2D grid.
4021
+
4022
+ For details, please refer to Wikipedia:
4023
+ https://en.wikipedia.org/wiki/Bilinear_interpolation
4024
+ """
4025
+
4026
+ return image_resize (input , out_shape , scale , name , 'BILINEAR' )
4027
+
4028
+
4029
+ def image_resize_short (input , out_short_len , resample = 'BILINEAR' ):
4030
+ """
4031
+ Resize a batch of images. The short edge of input images will be
4032
+ resized to the given 'out_short_len'. The long edge of input images
4033
+ will be resized proportionately to make images' length-width ratio
4034
+ constant.
4035
+
4036
+ Args:
4037
+ input (Variable): The input tensor of image resize layer,
4038
+ This is a 4-D tensor of the shape
4039
+ (num_batches, channels, in_h, in_w).
4040
+ out_short_len(int): The length of output images' short edge.
4041
+
4042
+ Returns:
4043
+ out (Variable): The output is a 4-D tensor of the shape
4044
+ (num_batches, channls, out_h, out_w).
4045
+ """
4046
+ in_shape = input .shape
4047
+ if len (in_shape ) != 4 :
4048
+ raise ValueError (
4049
+ "The rank of input must be 4 (num_batches, channels, in_h, in_w)." )
4050
+ hw = in_shape [2 :4 ]
4051
+ short_idx = hw .index (min (hw ))
4052
+ long_idx = 1 - short_idx
4053
+ out_shape = list (hw )
4054
+ out_shape [short_idx ] = out_short_len
4055
+ out_shape [long_idx ] = int (
4056
+ float (out_shape [long_idx ]) * (float (out_short_len ) / float (hw [
4057
+ short_idx ])) + 0.5 )
4058
+ return image_resize (input = input , out_shape = out_shape , resample = resample )
4059
+
4060
+
4001
4061
def gather (input , index ):
4002
4062
"""
4003
4063
Output is obtained by gathering entries of the outer-most dimension
4004
4064
of X indexed by `index` and concatenate them together.
4005
4065
4006
4066
.. math::
4007
4067
4008
- Out = X[Index]
4068
+ Out = X[Index]
4009
4069
4010
4070
4011
4071
.. code-block:: text
4012
4072
4013
4073
4014
4074
Given:
4015
4075
4016
- X = [[1, 2],
4017
- [3, 4],
4076
+ X = [[1, 2],
4077
+ [3, 4],
4018
4078
[5, 6]]
4019
4079
4020
4080
Index = [1, 2]
0 commit comments