Upsample¶
- class torch.nn.Upsample(size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None)[source][source]¶
對給定的多通道 1D (時間序列), 2D (空間) 或 3D (體積) 資料進行升採樣 (upsample)。
輸入資料的格式應為 minibatch x channels x [optional depth] x [optional height] x width。因此,對於空間輸入,我們預期一個 4D 張量,對於體積輸入,我們預期一個 5D 張量。
升採樣可用的演算法包括最鄰近 (nearest neighbor) 以及用於 3D、4D 和 5D 輸入張量的線性 (linear)、雙線性 (bilinear)、雙立方 (bicubic) 和三線性 (trilinear)。
可以給定
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) – 空間大小的倍數。如果是一個 tuple,則必須與輸入大小匹配。
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]]]])