Upsample#
- class torch.nn.Upsample(size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None)[source]#
将给定多通道一维(时间)、二维(空间)或三维(体积)数据进行上采样。
输入数据假定为 minibatch x channels x [optional depth] x [optional height] x width 的形式。因此,对于空间输入,我们期望一个四维张量,对于体积输入,我们期望一个五维张量。
可用的上采样算法包括最近邻插值,以及适用于三维、四维和五维输入张量的线性、双线性、双三次和三线性插值。
您可以提供
scale_factor
或目标输出size
来计算输出尺寸。(不能同时提供两者,因为会引起歧义)- 参数
size (int 或 Tuple[int] 或 Tuple[int, int] 或 Tuple[int, int, int], optional) – 输出的空间尺寸
scale_factor (float 或 Tuple[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_factor 为True
,则必须传入 scale_factor,并且 scale_factor 用于计算输出 size。计算出的输出 size 将用于推断新的插值尺度。请注意,当 scale_factor 是浮点数时,由于舍入和精度问题,它可能与重新计算的 scale_factor 不同。如果 recompute_scale_factor 为False
,则 size 或 scale_factor 将直接用于插值。
- 形状
输入: , 或
输出: , 或 , 其中
警告
当
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]]]])