resize_nearest¶
- paddle.fluid.layers.nn. resize_nearest ( input, out_shape=None, scale=None, name=None, actual_shape=None, align_corners=True, data_format='NCHW' ) [source]
- 
         This op resizes the input by performing nearest neighbor interpolation in both the height direction and the width direction based on given output shape which is specified by actual_shape, out_shape and scale in priority order. Warning: the parameter actual_shapewill be deprecated in the future and only useout_shapeinstead.Example: For scale: 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) Nearest neighbor interpolation: if: 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}) else: align_corners = True input : (N,C,H_in,W_in) output: (N,C,H_out,W_out) where: H_out = round(H_{in} * scale_{factor}) W_out = round(W_{in} * scale_{factor})For details of nearest neighbor interpolation, please refer to Wikipedia: https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation - Parameters
- 
           - input (Variable) – 4-D Tensor, its data type is float32, float64, or uint8, its data format is specified by - data_format.
- out_shape (list|tuple|Variable|None) – The output shape of resized tensor, the shape is (out_h, out_w). Default: None. Every element should be an integer or a tensor Variable with shape: [1] if it is a list. If it is a tensor Variable, its dimension size should be 1. 
- scale (float|Variable|None) – The multiplier for the input height or width. At least one of - out_shapeor- scalemust be set. And- out_shapehas a higher priority than- scale. Default: None.
- 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 
- actual_shape (Variable) – An optional input to specify output shape dynamically. If provided, image resize according to this given shape rather than - out_shapeand- scalespecifying shape. That is to say actual_shape has the highest priority. It is recommended to use- out_shapeif you want to specify output shape dynamically, because- actual_shapewill be deprecated. When using actual_shape to specify output shape, one of- out_shapeand- scaleshould also be set, otherwise errors would be occurred in graph constructing stage. Default: None
- align_corners (bool) – an optional bool. Defaults to True. If True, the centers of 4 corner pixels of the input and output tensors are aligned, preserving the values at the corner pixels, If False, are not aligned 
- 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: “NCHW”, “NHWC”. The default is “NCHW”. When it is “NCHW”, the data is stored in the order of: [batch_size, input_channels, input_height, input_width]. 
 
- Returns
- 
           4-D tensor(NCHW or NHWC). 
- Return type
- 
           Variable 
 Examples #declarative mode import paddle.fluid as fluid import numpy as np import paddle paddle.enable_static() input = fluid.data(name="input", shape=[None,3,6,10]) #1 output = fluid.layers.resize_nearest(input=input,out_shape=[12,12]) #2 #x = np.array([2]).astype("int32") #dim1 = fluid.data(name="dim1", shape=[1], dtype="int32") #fluid.layers.assign(input=x, output=dim1) #output = fluid.layers.resize_nearest(input=input,out_shape=[12,dim1]) #3 #x = np.array([3,12]).astype("int32") #shape_tensor = fluid.data(name="shape_tensor", shape=[2], dtype="int32") #fluid.layers.assign(input=x, output=shape_tensor) #output = fluid.layers.resize_nearest(input=input,out_shape=shape_tensor) #4 #x = np.array([0.5]).astype("float32") #scale_tensor = fluid.data(name="scale", shape=[1], dtype="float32") #fluid.layers.assign(x,scale_tensor) #output = fluid.layers.resize_nearest(input=input,scale=scale_tensor) place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) input_data = np.random.rand(2,3,6,10).astype("float32") output_data = exe.run(fluid.default_main_program(), feed={"input":input_data}, fetch_list=[output], return_numpy=True) print(output_data[0].shape) #1 # (2, 3, 12, 12) #2 # (2, 3, 12, 2) #3 # (2, 3, 3, 12) #4 # (2, 3, 3, 5) #imperative mode import paddle.fluid.dygraph as dg with dg.guard(place) as g: input = dg.to_variable(input_data) output = fluid.layers.resize_nearest(input=input, out_shape=[12,12]) print(output.shape) # [2L, 3L, 12L, 12L] 
