interpolatetorch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None) 根據(jù)給定的size或scale_factor參數(shù)來對輸入進行下/上采樣 使用的插值算法取決于參數(shù)mode的設(shè)置 支持目前的temporal(1D, 如向量數(shù)據(jù)), spatial(2D, 如jpg、png等圖像數(shù)據(jù))和volumetric(3D, 如點云數(shù)據(jù))類型的采樣數(shù)據(jù)作為輸入,輸入數(shù)據(jù)的格式為minibatch x channels x [optional depth] x [optional height] x width,具體為:
可用于重置大小的mode有:最近鄰、線性(3D-only),、雙線性, 雙三次(bicubic,4D-only)和三線性(trilinear,5D-only)插值算法和area算法 參數(shù):
注意: 使用mode='bicubic'時,可能會導致overshoot問題,即它可以為圖像生成負值或大于255的值。如果你想在顯示圖像時減少overshoot問題,可以顯式地調(diào)用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的兩種情況,這就是對齊和不對齊的差別,會對齊左上角元素,即設(shè)置為align_corners = True時輸入的左上角元素是一定等于輸出的左上角元素。但是有時align_corners = False時左上角元素也會相等,官網(wǎng)上給的例子就不太能說明兩者的不同(也沒有試出不同的例子,大家理解這個概念就行了)
舉例: 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的方法: upsampletorch.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) 因為這些現(xiàn)在都建議使用上面的interpolate方法實現(xiàn),所以就不解釋了
更加復(fù)雜的例子可見:pytorch 不使用轉(zhuǎn)置卷積來實現(xiàn)上采樣
|
|
來自: LibraryPKU > 《機器學習》