最近寫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 (Tensor) – the input tensor
-
size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]) – output spatial size.
-
scale_factor (float or Tuple[float]) – multiplier for spatial size. Has to match input size if it is a tuple.
-
mode (str) – algorithm used for upsampling:
'nearest'
|'linear'
|'bilinear'
|'bicubic'
|'trilinear'
|'area'
. Default:'nearest'
-
align_corners (bool, 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 toFalse
, 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 whenscale_factor
is kept the same. This only has an effect whenmode
is'linear'
,'bilinear'
,'bicubic'
or'trilinear'
. Default:False
舉例語法:
x = nn.functional.interpolate(x, scale_factor=8, mode='bilinear', align_corners=False)