Pytorch——torch.nn.functional.interpolate函数
最近写pytorch的时候用到了这个函数:torch.nn.functional.interpolate
一时没太懂这个函数是干嘛的,所以看了下pytorch的官方文档:
torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode=nearest, align_corners=None):
Down/up samples the input to either the given size or the given scale_factor
The algorithm used for interpolation is determined by mode.
Currently temporal, spatial and volumetric sampling are supported, i.e. expected inputs are 3-D, 4-D or 5-D in shape.
The input dimensions are interpreted in the form: mini-batch x channels x [optional depth] x [optional height] x width.
The modes available for resizing are: nearest, linear (3D-only), bilinear, bicubic (4D-only), trilinear (5D-only), area
大意就是这个函数是用来上采样或下采样,可以给定size或者scale_factor来进行上下采样。同时支持3D、4D、5D的张量输入。
插值算法可选,最近邻、线性、双线性等等。
来看看这个函数的参数:
-
input () – the input tensor size ( or Tuple[] or Tuple[, ] or Tuple[, , ]) – output spatial size. scale_factor ( or Tuple[]) – multiplier for spatial size. Has to match input size if it is a tuple. mode () – algorithm used for upsampling: nearest | linear | bilinear | bicubic |trilinear | area. Default: nearest align_corners (, optional) – Geometrically, we consider the pixels of the input and output as squares rather than points. If set to True, the input and output tensors are aligned by the center points of their corner pixels, preserving the values at the corner pixels. If set to False, the input and output tensors are aligned by the corner points of their corner pixels, and the interpolation uses edge value padding for out-of-boundary values, making this operation independent of input size when scale_factor is kept the same. This only has an effect when mode is linear, bilinear, bicubic or trilinear. Default: False
举例语法:
x = nn.functional.interpolate(x, scale_factor=8, mode=bilinear, align_corners=False)
