小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

pytorch torch.nn.functional實(shí)現(xiàn)插值和上采樣

 LibraryPKU 2021-04-15

interpolate

torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None)

根據(jù)給定的size或scale_factor參數(shù)來(lái)對(duì)輸入進(jìn)行下/上采樣

使用的插值算法取決于參數(shù)mode的設(shè)置

支持目前的temporal(1D, 如向量數(shù)據(jù)), spatial(2D, 如jpg、png等圖像數(shù)據(jù))和volumetric(3D, 如點(diǎn)云數(shù)據(jù))類型的采樣數(shù)據(jù)作為輸入,輸入數(shù)據(jù)的格式為minibatch x channels x [optional depth] x [optional height] x width,具體為:

  • 對(duì)于一個(gè)temporal輸入,期待著3D張量的輸入,即minibatch x channels x width
  • 對(duì)于一個(gè)空間spatial輸入,期待著4D張量的輸入,即minibatch x channels x height x width
  • 對(duì)于體積volumetric輸入,則期待著5D張量的輸入,即minibatch x channels x depth x height x width

可用于重置大小的mode有:最近鄰、線性(3D-only),、雙線性, 雙三次(bicubic,4D-only)和三線性(trilinear,5D-only)插值算法和area算法

參數(shù):

  • input (Tensor) – 輸入張量

  • size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]) –輸出大小.

  • scale_factor (float or Tuple[float]) – 指定輸出為輸入的多少倍數(shù)。如果輸入為tuple,其也要制定為tuple類型

  • mode (str) – 可使用的上采樣算法,有'nearest', 'linear', 'bilinear', 'bicubic' , 'trilinear'和'area'. 默認(rèn)使用'nearest'

  • align_corners (bool, optional) –幾何上,我們認(rèn)為輸入和輸出的像素是正方形,而不是點(diǎn)。如果設(shè)置為True,則輸入和輸出張量由其角像素的中心點(diǎn)對(duì)齊,從而保留角像素處的值。如果設(shè)置為False,則輸入和輸出張量由它們的角像素的角點(diǎn)對(duì)齊,插值使用邊界外值的邊值填充;當(dāng)scale_factor保持不變時(shí),使該操作獨(dú)立于輸入大小。僅當(dāng)使用的算法為'linear', 'bilinear', 'bilinear'or 'trilinear'時(shí)可以使用。默認(rèn)設(shè)置為False

 

注意:

使用mode='bicubic'時(shí),可能會(huì)導(dǎo)致overshoot問(wèn)題,即它可以為圖像生成負(fù)值或大于255的值。如果你想在顯示圖像時(shí)減少overshoot問(wèn)題,可以顯式地調(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.

 

警告:

當(dāng)align_corners = True時(shí),線性插值模式(線性、雙線性、雙三線性和三線性)不按比例對(duì)齊輸出和輸入像素,因此輸出值可以依賴于輸入的大小。這是0.3.1版本之前這些模式的默認(rèn)行為。從那時(shí)起,默認(rèn)行為是align_corners = False,如下圖:

上面的圖是source pixel為4*4上采樣為target pixel為8*8的兩種情況,這就是對(duì)齊和不對(duì)齊的差別,會(huì)對(duì)齊左上角元素,即設(shè)置為align_corners = True時(shí)輸入的左上角元素是一定等于輸出的左上角元素。但是有時(shí)align_corners = False時(shí)左上角元素也會(huì)相等,官網(wǎng)上給的例子就不太能說(shuō)明兩者的不同(也沒(méi)有試出不同的例子,大家理解這個(gè)概念就行了)

 

舉例:

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)

因?yàn)檫@些現(xiàn)在都建議使用上面的interpolate方法實(shí)現(xiàn),所以就不解釋了

 

更加復(fù)雜的例子可見:pytorch 不使用轉(zhuǎn)置卷積來(lái)實(shí)現(xiàn)上采樣

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多