interpolate
torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None)
根據給定的size或scale_factor參數來對輸入進行下/上采樣
使用的插值算法取決於參數mode的設置
支持目前的temporal(1D, 如向量數據), spatial(2D, 如jpg、png等圖像數據)和volumetric(3D, 如點雲數據)類型的采樣數據作為輸入,輸入數據的格式為minibatch x channels x [optional depth] x [optional height] x width,具體為:
- 對於一個temporal輸入,期待着3D張量的輸入,即minibatch x channels x width
- 對於一個空間spatial輸入,期待着4D張量的輸入,即minibatch x channels x height x width
- 對於體積volumetric輸入,則期待着5D張量的輸入,即minibatch x channels x depth x height x width
可用於重置大小的mode有:最近鄰、線性(3D-only),、雙線性, 雙三次(bicubic,4D-only)和三線性(trilinear,5D-only)插值算法和area算法
參數:
-
input (Tensor) – 輸入張量
-
size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]) –輸出大小.
-
scale_factor (float or Tuple[float]) – 指定輸出為輸入的多少倍數。如果輸入為tuple,其也要制定為tuple類型
-
mode (str) –
可使用的上采樣算法,有
'nearest'
,'linear'
,'bilinear'
,'bicubic'
,'trilinear'和'area'
.默認使用
'nearest'
-
align_corners (bool, optional) –
幾何上,我們認為輸入和輸出的像素是正方形,而不是點。如果設置為True,則輸入和輸出張量由其角像素的中心點對齊,從而保留角像素處的值。如果設置為False,則輸入和輸出張量由它們的角像素的角點對齊,插值使用邊界外值的邊值填充;
當scale_factor保持不變時
,使該操作獨立於輸入大小。僅當使用的算法為'linear'
,'bilinear', 'bilinear'
or'trilinear'時可以使用。
默認設置為
False
注意:
使用mode='bicubic'時,可能會導致overshoot問題,即它可以為圖像生成負值或大於255的值。如果你想在顯示圖像時減少overshoot問題,可以顯式地調用result.clamp(min=0,max=255)。
When using the CUDA backend, this operation may induce nondeterministic behaviour in be backward that is not easily switched off. Please see the notes on Reproducibility for background.
警告:
當align_corners = True時,線性插值模式(線性、雙線性、雙三線性和三線性)不按比例對齊輸出和輸入像素,因此輸出值可以依賴於輸入的大小。這是0.3.1版本之前這些模式的默認行為。從那時起,默認行為是align_corners = False,如下圖:
上面的圖是source pixel為4*4上采樣為target pixel為8*8的兩種情況,這就是對齊和不對齊的差別,會對齊左上角元素,即設置為align_corners = True時輸入的左上角元素是一定等於輸出的左上角元素。但是有時align_corners = False時左上角元素也會相等,官網上給的例子就不太能說明兩者的不同(也沒有試出不同的例子,大家理解這個概念就行了)
舉例:
import torch from torch import nn import torch.nn.functional as F input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2) input
返回:
tensor([[[[1., 2.], [3., 4.]]]])
x = F.interpolate(input, scale_factor=2, mode='nearest') x
返回:
tensor([[[[1., 1., 2., 2.], [1., 1., 2., 2.], [3., 3., 4., 4.], [3., 3., 4., 4.]]]])
x = F.interpolate(input, scale_factor=2, mode='bilinear', align_corners=True) x
返回:
tensor([[[[1.0000, 1.3333, 1.6667, 2.0000], [1.6667, 2.0000, 2.3333, 2.6667], [2.3333, 2.6667, 3.0000, 3.3333], [3.0000, 3.3333, 3.6667, 4.0000]]]])
也提供了一些Upsample的方法:
upsample
torch.nn.functional.upsample(input, size=None, scale_factor=None, mode='nearest', align_corners=None)
torch.nn.functional.upsample_nearest(input, size=None, scale_factor=None)
torch.nn.functional.upsample_bilinear(input, size=None, scale_factor=None)
因為這些現在都建議使用上面的interpolate方法實現,所以就不解釋了
更加復雜的例子可見:pytorch 不使用轉置卷積來實現上采樣