interpolate¶
- paddle.nn.functional. interpolate ( x, size=None, scale_factor=None, mode='nearest', align_corners=False, align_mode=0, data_format=None, name=None ) [source]
-
This API resizes a batch of images.
The input must be be a 3-D Tensor of the shape (num_batches, channels, in_w) or (num_batches, in_w, channels), or 4-D (num_batches, channels, in_h, in_w) or (num_batches, in_h, in_w, channels), or a 5-D Tensor of the shape (num_batches, channels, in_d, in_h, in_w) or (num_batches, in_d, in_h, in_w, channels), Where in_w is width of the input tensor, in_h is the height of the input tensor, in_d is the depth of the input tensor. and the resizing only applies on the three dimensions(depth, height and width).
Supporting resample methods:
‘linear’ : Linear interpolation
‘bilinear’ : Bilinear interpolation
‘trilinear’ : Trilinear interpolation
‘nearest’ : Nearest neighbor interpolation
‘bicubic’ : Bicubic interpolation
‘area’: Area interpolation
Linear interpolation is the method of using a line connecting two known quantities to determine the value of an unknown quantity between the two known quantities.
Nearest neighbor interpolation is to perform nearest neighbor interpolation in both the 3rd dimension(in height direction) and the 4th dimension(in width direction) on input tensor.
Bilinear interpolation is an extension of linear interpolation for interpolating functions of two variables (e.g. H-direction and W-direction in this op) on a rectilinear 2D grid. The key idea is to perform linear interpolation first in one direction, and then again in the other direction.
Trilinear interpolation is an extension of linear interpolation for interpolating functions of three variables (e.g. D-direction, H-direction and W-direction in this op) on a rectilinear 3D grid. The linear interpolation is performed on three directions. align_corners and align_mode are optional parameters,the calculation method of interpolation can be selected by them.
Bicubic interpolation is an extension of cubic interpolation for interpolating data points on a two-dimensional regular grid. The interpolated surface is smoother than corresponding surfaces obtained by bilinear interpolation or nearest-neighbor interpolation.
Area interpolation is to perform area interpolation in both the 3rd dimension(in height direction) , the 4th dimension(in width direction) and the 5th dimension(in depth direction) on input tensor. Set to area will directly call paddle.nn.functional.adaptive_avg_pool1d or paddle.nn.functional.adaptive_avg_pool2d or paddle.nn.functional.adaptive_avg_pool3d.
Example:
# For scale_factor: if align_corners = True && out_size > 1 : scale_factor = (in_size-1.0)/(out_size-1.0) else: scale_factor = float(in_size/out_size) # Linear interpolation: if: align_corners = False , align_mode = 0 input : (N,C,W_in) output: (N,C,W_out) where: W_out = (W_{in}+0.5) * scale_{factor} - 0.5 else: input : (N,C,W_in) output: (N,C,W_out) where: W_out = W_{in} * scale_{factor} # Nearest neighbor interpolation: align_corners = False input : (N,C,H_in,W_in) output: (N,C,H_out,W_out) where: H_out = floor (H_{in} * scale_{factor}) W_out = floor (W_{in} * scale_{factor}) # Bilinear interpolation: if: align_corners = False , align_mode = 0 input : (N,C,H_in,W_in) output: (N,C,H_out,W_out) where: H_out = (H_{in}+0.5) * scale_{factor} - 0.5 W_out = (W_{in}+0.5) * scale_{factor} - 0.5 else: input : (N,C,H_in,W_in) output: (N,C,H_out,W_out) where: H_out = H_{in} * scale_{factor} W_out = W_{in} * scale_{factor} # Bicubic interpolation: if: align_corners = False input : (N,C,H_in,W_in) output: (N,C,H_out,W_out) where: H_out = (H_{in}+0.5) * scale_{factor} - 0.5 W_out = (W_{in}+0.5) * scale_{factor} - 0.5 else: input : (N,C,H_in,W_in) output: (N,C,H_out,W_out) where: H_out = H_{in} * scale_{factor} W_out = W_{in} * scale_{factor} # Trilinear interpolation: if: align_corners = False , align_mode = 0 input : (N,C,D_in,H_in,W_in) output: (N,C,D_out,H_out,W_out) where: D_out = (D_{in}+0.5) * scale_{factor} - 0.5 H_out = (H_{in}+0.5) * scale_{factor} - 0.5 W_out = (W_{in}+0.5) * scale_{factor} - 0.5 else: input : (N,C,D_in,H_in,W_in) output: (N,C,D_out,H_out,W_out) where: D_out = D_{in} * scale_{factor} H_out = H_{in} * scale_{factor} W_out = W_{in} * scale_{factor}
For details of linear interpolation, please refer to Wikipedia: https://en.wikipedia.org/wiki/Linear_interpolation.
For details of nearest neighbor interpolation, please refer to Wikipedia: https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation.
For details of bilinear interpolation, please refer to Wikipedia: https://en.wikipedia.org/wiki/Bilinear_interpolation.
For details of trilinear interpolation, please refer to Wikipedia: https://en.wikipedia.org/wiki/Trilinear_interpolation.
For details of bicubic interpolation, please refer to Wikipedia: https://en.wikipedia.org/wiki/Bicubic_interpolation
- Parameters
-
x (Tensor) – 3-D, 4-D or 5-D Tensor, its data type is float32, float64, or uint8, its data format is specified by
data_format
. Ifdata_format
is not provided, the data format will be presumed according to its dimension. See details indata_format
.size (list|tuple|Tensor|None) – Output shape of image resize layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w) when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor. Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1] or []. If a Tensor, its dimensions size should be a 1.
scale_factor (float|Tensor|list|tuple|None) – The multiplier for the input height or width. At least one of
size
orscale_factor
must be set. Andsize
has a higher priority thanscale_factor
.Has to match input size if it is either a list or a tuple or a Tensor.If a list/tuple, each element can be an integer or a Tensor of shape: [1] or []. Default: None.mode (str) – The resample method. It supports ‘linear’, ‘area’, ‘nearest’, ‘bilinear’, ‘bicubic’ and ‘trilinear’ currently. Default: ‘nearest’
align_corners (bool) – An optional bool, If True, the centers of the 4 corner pixels of the input and output tensors are aligned, preserving the values at the corner pixels.This only has an effect when ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: False
align_mode (int) – An optional for linear/bilinear/trilinear interpolation. Refer to the formula in the example above, it can be ‘0’ for src_idx = scale_factor*(dst_index+0.5)-0.5 , can be ‘1’ for src_idx = scale_factor*dst_index.
data_format (str, optional) – Specify the data format of the input, and the data format of the output will be consistent with that of the input. An optional string from:”NCW”, “NWC”, “NCHW”, “NHWC”, “NCDHW”, “NDHWC”. The default value is None. When
data_format
is not specified, it will be automatically inferred from the input dimension ofx
. Whenx
is a 3-D Tensor,data_format
will be set to “NCW”; Whenx
is a 4-D Tensor,data_format
will be set to “NCHW”; Whenx
is a 5-D Tensor,data_format
will be set to “NCDHW”. When it is “NCHW”, the data should be stored in the order of: [batch_size, input_channels, input_height, input_width]. When it is “NCDHW”, the data should be stored in the order of: [batch_size, input_channels, input_depth, input_height, input_width].name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name
- Returns
-
A 3-D, 4-D or 5-D Tensor, with the same data format of the input
x
.
Examples
>>> import paddle >>> import paddle.nn.functional as F >>> input_data = paddle.randn(shape=(2,3,6,10)).astype(paddle.float32) >>> output_1 = F.interpolate(x=input_data, size=[12,12]) >>> print(output_1.shape) [2, 3, 12, 12] >>> # given scale >>> output_2 = F.interpolate(x=input_data, scale_factor=[2,1]) >>> print(output_2.shape) [2, 3, 12, 10] >>> # bilinear interp >>> output_3 = F.interpolate(x=input_data, scale_factor=[2,1], mode="bilinear") >>> print(output_2.shape) [2, 3, 12, 10]