resize_nearest

  • paddle.fluid.layers.resize_nearest(input, out_shape=None, scale=None, name=None, actual_shape=None, align_corners=True, data_format='NCHW')[源代码]

该OP对输入图片进行大小调整,在高度方向宽度方向进行最邻近插值(nearest neighbor interpolation)操作。

输出形状按优先级顺序依据 actual_shape , out_shapescale 而定。

注意: 参数 actual_shape 将被弃用,请使用 out_shape 替代。

  1. Example:
  2.  
  3. For scale:
  4.  
  5. if align_corners = True && out_size > 1 :
  6.  
  7. scale_factor = (in_size-1.0)/(out_size-1.0)
  8.  
  9. else:
  10.  
  11. scale_factor = float(in_size/out_size)
  12.  
  13. Nearest neighbor interpolation:
  14.  
  15. if align_corners = False
  16.  
  17. input : (N,C,H_in,W_in)
  18. output: (N,C,H_out,W_out) where:
  19.  
  20. H_out = \left \lfloor {H_{in} * scale_{}factor}} \right \rfloor
  21. W_out = \left \lfloor {W_{in} * scale_{}factor}} \right \rfloor
  22.  
  23. else:
  24. align_corners = True
  25.  
  26. input : (N,C,H_in,W_in)
  27. output: (N,C,H_out,W_out) where:
  28.  
  29. H_out = round(H_{in} * scale_{factor})
  30. W_out = round(W_{in} * scale_{factor})

最邻近插值的详细介绍请参照: Wiki Nearest-neighbor interpolation

  • 参数:
    • input (Variable) - 4-D Tensor,数据类型为float32、float64或uint8,其数据格式由参数 data_format 指定。
    • out_shape (list|tuple|Variable|None) - 双线性插值法调整后的输出,维度为[out_h, out_w]的2-D Tensor。如果 out_shape 是列表,每一个元素可以是整数或者shape为[1]的变量。如果 out_shape 是变量,则其维度大小为1。默认值为None。
    • scale (float|Variable|None) – 输入高宽的乘数因子。 out_shapescale 二者至少设置其一。 out_shape 具有比 scale 更高的优先级。 默认值为None。
    • name (str|None) - 该参数供开发人员打印调试信息时使用,具体用法请参见 Name 。默认值为None。
    • actual_shape (Variable) - 可选输入,用于动态指定输出形状。如果指定actual_shape,图像将根据给定的形状调整大小,而不是根据指定形状的 out_shapescale 进行调整。也就是说, actual_shape 具有最高的优先级。注意:如果希望动态指定输出形状,建议使用 out_shape , 因为 actual_shape 未来将被弃用。在使用actual_shape指定输出形状时,仍然需要设置out_shape和scale之一,否则在图形构建阶段会出现错误。默认值为None。
    • align_corners (bool)- 一个可选的bool型参数,如果为True,则将输入和输出张量的4个角落像素的中心对齐,并保留角点像素的值。 默认值为True。
    • data_format (str,可选)- 指定输入的数据格式,输出的数据格式将与输入保持一致,可以是"NCHW"和"NHWC"。N是批尺寸,C是通道数,H是特征高度,W是特征宽度。默认值:"NCHW"。

返回:4-D Tensor,形状为 (num_batches, channels, out_h, out_w) 或 (num_batches, out_h, out_w, channels)。

返回类型:Variable

代码示例

  1. import paddle.fluid as fluid
  2. input = fluid.layers.data(name="input", shape=[3,6,9], dtype="float32")
  3. # input.shape = [-1, 3, 6, 9], where -1 indicates batch size, and it will get the exact value in runtime.
  4.  
  5. out0 = fluid.layers.resize_nearest(input, out_shape=[12, 12])
  6. # out0.shape = [-1, 3, 12, 12], it means out0.shape[0] = input.shape[0] in runtime.
  7.  
  8. # out_shape is a list in which each element is a integer or a tensor Variable
  9. dim1 = fluid.layers.data(name="dim1", shape=[1], dtype="int32", append_batch_size=False)
  10. out1 = fluid.layers.resize_nearest(input, out_shape=[12, dim1])
  11. # out1.shape = [-1, 3, 12, -1]
  12.  
  13. # out_shape is a 1-D tensor Variable
  14. shape_tensor = fluid.layers.data(name="resize_shape", shape=[2], dtype="int32", append_batch_size=False)
  15. out2 = fluid.layers.resize_nearest(input, out_shape=shape_tensor)
  16. # out2.shape = [-1, 3, -1, -1]
  17.  
  18. # when use actual_shape
  19. actual_shape_tensor = fluid.layers.data(name="actual_shape_tensor", shape=[2], dtype="int32", append_batch_size=False)
  20. out3 = fluid.layers.resize_nearest(input, out_shape=[4, 4], actual_shape=actual_shape_tensor)
  21. # out3.shape = [-1, 3, 4, 4]
  22.  
  23. # scale is a Variable
  24. scale_tensor = fluid.layers.data(name="scale", shape=[1], dtype="float32", append_batch_size=False)
  25. out4 = fluid.layers.resize_nearest(input, scale=scale_tensor)
  26. # out4.shape = [-1, 3, -1, -1]