上采样

classtorch.nn.Upsample(size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None)[源代码]

对给定的多通道一维(时间)、二维(空间)或三维(体积)数据进行上采样处理。

输入数据的形式被假设为minibatch x channels x [可选 depth] x [可选 height] x width。因此,对于空间输入,我们期待的是4D张量,而对于体积输入,则是5D张量。

可用的上采样算法包括最近邻和线性插值。对于3D输入张量使用双线性插值,对于4D输入张量使用双立方插值,对于5D输入张量使用三线性插值。

可以提供一个scale_factor或目标输出size来确定输出大小。(但不能同时提供两者,这样会引发歧义)

参数
  • size (intTuple[int]Tuple[int, int]Tuple[int, int, int], 可选) – 输出空间尺寸

  • scale_factor (floatTuple[float] 或 Tuple[float, float] 或 Tuple[float, float, float], 可选) – 空间大小的乘数。如果它是元组形式,则必须与输入大小相匹配。

  • mode (str, optional) – 上采样算法,可选值包括:'nearest''linear''bilinear''bicubic''trilinear'。默认值为:'nearest'

  • align_corners (bool, optional) – 如果为 True,输入和输出张量的角像素将对齐,从而保留这些像素上的值。这仅在 mode'linear''bilinear''bicubic''trilinear' 时有效。默认值: False

  • recompute_scale_factor (bool, 可选) – 重新计算用于插值的 scale_factor。如果 recompute_scale_factorTrue,则需要传递 scale_factor 参数,并使用它来计算输出大小。然后根据这个输出大小推断新的插值比例。需要注意的是,当 scale_factor 是浮点数时,由于舍入和精度问题,实际的 scale_factor 可能与重新计算的结果不同。如果 recompute_scale_factorFalse,则直接使用 sizescale_factor 进行插值。

形状:
  • 输入: $(N, C, W_{in})$, $(N, C, H_{in}, W_{in})$$(N, C, D_{in}, H_{in}, W_{in})$

  • 输出为:$(N, C, W_{out})$$(N, C, H_{out}, W_{out})$$(N, C, D_{out}, H_{out}, W_{out})$,其中

$D_{out} = \left\lfloor D_{in} \times \text{scale\_factor} \right\rfloor$
$H_{out} = \left\lfloor H_{in} \times \text{scale\_factor} \right\rfloor$
$W_{out} = \left\lfloor W_{in} \times \text{scale\_factor} \right\rfloor$

警告

当设置为align_corners = True时,线性插值模式(如linearbilinearbicubictrilinear)不会按比例对齐输出和输入像素,因此输出值会依赖于输入大小。在版本 0.3.1 及之前,默认行为是align_corners = True。从那时起,默认行为改为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]]]])
本页目录