評價此頁

Upsample#

class torch.nn.modules.upsampling.Upsample(size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None)[原始碼]#

對給定的多通道一維(時間)、二維(空間)或三維(體積)資料進行上取樣。

假定輸入資料格式為 minibatch x channels x [可選深度] x [可選高度] x 寬度。因此,對於空間輸入,我們期望一個4D張量;對於體積輸入,我們期望一個5D張量。

可用的上取樣演算法包括:最近鄰插值,以及適用於3D、4D和5D輸入張量的線性、雙線性、雙三次和三線性插值。

您可以提供 scale_factor 或目標輸出 size 來計算輸出大小。(您不能同時提供兩者,因為這會產生歧義)

引數
  • size (intTuple[int] 或 Tuple[int, int] 或 Tuple[int, int, int], optional) – 輸出空間尺寸

  • scale_factor (floatTuple[float] 或 Tuple[float, float] 或 Tuple[float, float, float], optional) – 空間尺寸的乘數。如果為元組,必須與輸入尺寸匹配。

  • mode (str, optional) – 上取樣演算法:可選值為 'nearest', 'linear', 'bilinear', 'bicubic''trilinear'。 預設值:'nearest'

  • align_corners (bool, optional) – 如果為 True,則輸入和輸出張量的角畫素將對齊,從而保留這些畫素的值。這僅在 mode'linear', 'bilinear', 'bicubic''trilinear' 時有效。 預設值:False

  • recompute_scale_factor (bool, optional) – 重新計算用於插值計算的 scale_factor。如果 recompute_scale_factorTrue,則必須傳入 scale_factor,並且 scale_factor 用於計算輸出 size。計算出的輸出 size 將用於推斷新的插值比例。請注意,當 scale_factor 為浮點數時,由於舍入和精度問題,它可能與重新計算的 scale_factor 不同。如果 recompute_scale_factorFalse,則 sizescale_factor` 將直接用於插值。

形狀
  • 輸入:(N,C,Win)(N, C, W_{in}), (N,C,Hin,Win)(N, C, H_{in}, W_{in})(N,C,Din,Hin,Win)(N, C, D_{in}, H_{in}, W_{in})

  • 輸出:(N,C,Wout)(N, C, W_{out}), (N,C,Hout,Wout)(N, C, H_{out}, W_{out})(N,C,Dout,Hout,Wout)(N, C, D_{out}, H_{out}, W_{out}),其中

Dout=Din×scale_factorD_{out} = \left\lfloor D_{in} \times \text{scale\_factor} \right\rfloor
Hout=Hin×scale_factorH_{out} = \left\lfloor H_{in} \times \text{scale\_factor} \right\rfloor
Wout=Win×scale_factorW_{out} = \left\lfloor W_{in} \times \text{scale\_factor} \right\rfloor

警告

align_corners = True 時,線性插值模式(linear, bilinear, bicubic, 和 trilinear)不會按比例對齊輸出和輸入畫素,因此輸出值可能取決於輸入大小。這是版本 0.3.1 及之前這些模式的預設行為。從那時起,預設行為是 align_corners = False。有關此行為如何影響輸出的具體示例,請參見下文。

注意

如果您需要降取樣/通用重取樣,應該使用 interpolate()

示例

>>> input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
>>> input
tensor([[[[1., 2.],
          [3., 4.]]]])

>>> m = nn.Upsample(scale_factor=2, mode='nearest')
>>> m(input)
tensor([[[[1., 1., 2., 2.],
          [1., 1., 2., 2.],
          [3., 3., 4., 4.],
          [3., 3., 4., 4.]]]])

>>> m = nn.Upsample(scale_factor=2, mode='bilinear')  # align_corners=False
>>> m(input)
tensor([[[[1.0000, 1.2500, 1.7500, 2.0000],
          [1.5000, 1.7500, 2.2500, 2.5000],
          [2.5000, 2.7500, 3.2500, 3.5000],
          [3.0000, 3.2500, 3.7500, 4.0000]]]])

>>> m = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
>>> m(input)
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]]]])

>>> # Try scaling the same data in a larger tensor
>>> input_3x3 = torch.zeros(3, 3).view(1, 1, 3, 3)
>>> input_3x3[:, :, :2, :2].copy_(input)
tensor([[[[1., 2.],
          [3., 4.]]]])
>>> input_3x3
tensor([[[[1., 2., 0.],
          [3., 4., 0.],
          [0., 0., 0.]]]])

>>> m = nn.Upsample(scale_factor=2, mode='bilinear')  # align_corners=False
>>> # Notice that values in top left corner are the same with the small input (except at boundary)
>>> m(input_3x3)
tensor([[[[1.0000, 1.2500, 1.7500, 1.5000, 0.5000, 0.0000],
          [1.5000, 1.7500, 2.2500, 1.8750, 0.6250, 0.0000],
          [2.5000, 2.7500, 3.2500, 2.6250, 0.8750, 0.0000],
          [2.2500, 2.4375, 2.8125, 2.2500, 0.7500, 0.0000],
          [0.7500, 0.8125, 0.9375, 0.7500, 0.2500, 0.0000],
          [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])

>>> m = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
>>> # Notice that values in top left corner are now changed
>>> m(input_3x3)
tensor([[[[1.0000, 1.4000, 1.8000, 1.6000, 0.8000, 0.0000],
          [1.8000, 2.2000, 2.6000, 2.2400, 1.1200, 0.0000],
          [2.6000, 3.0000, 3.4000, 2.8800, 1.4400, 0.0000],
          [2.4000, 2.7200, 3.0400, 2.5600, 1.2800, 0.0000],
          [1.2000, 1.3600, 1.5200, 1.2800, 0.6400, 0.0000],
          [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])
extra_repr()[原始碼]#

返回模組的額外表示。

返回型別

str

forward(input)[原始碼]#

執行前向傳播。

返回型別

張量