pytorch torch.nn.functional實現插值和上採樣

 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[intint] or Tuple[intintint]) –輸出大小.

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

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

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

     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章