乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      pytorch torch.nn.functional實現(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ù)來對輸入進行下/上采樣

      使用的插值算法取決于參數(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,具體為:

      • 對于一個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算法

      參數(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'. 默認使用'nearest'

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

       

      注意:

      使用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的方法:

      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)

      因為這些現(xiàn)在都建議使用上面的interpolate方法實現(xiàn),所以就不解釋了

       

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

       

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多