nn.Upsample
寫在前面:在PyTorch中有兩種上采樣/下采樣的方法,一種是Upsample,另一種是interpolate
這兩個函數(shù)的使用方法略有差異,這里僅介紹Upsample
Upsample
torch.nn.Upsample(size=None, scale_factor=None, mode='nearest', align_corners=None)
- size - output spatial sizes(int or tuple(int, int))
- scale_factor - multiplier for spatial size(float or tuple(float, float))
- mode - upsampling algorithm: ‘linear’, ‘bilinear’, ‘bicubic’, ‘trilinear’. default:‘nearest’
- align_corners - if True, the corner pixels of the input and output tensors are aligned, and thus preserving the values at those pixels. This only has effect when mode is ‘linear’, ‘bilinear’, or ‘trilinear’. Default: False(這個參數(shù)下面會單獨講)
此函數(shù)可以上采樣或者下采樣(但是在PyTorch中下采樣推薦interpolate,不過我發(fā)現(xiàn)沒有區(qū)別?)
在上采樣/下采樣的過程中,只需要給定size或者scale_factor一個值即可,不需要給定所有的值,不然會沖突
一、nearest
最近鄰插值的公式為
srcX=desX(srcW/desW)srcY=desY(srcH/desH)srcX = desX(srcW/desW) \\ srcY = desY(srcH/desH) srcX=desX(srcW/desW)srcY=desY(srcH/desH)
上采樣
舉個例子,假設(shè)放大倍數(shù)為2,那么src=des×0.5src=des\times 0.5src=des×0.5,對于下圖,坐標(3,2)處的顏色應為原來坐標系(3×0.5,2×0.5)=(1.5,1)(3\times 0.5, 2\times 0.5)=(1.5, 1)(3×0.5,2×0.5)=(1.5,1)也就是(2, 1)對應橙色
下采樣
下采樣和上采樣的處理過程完全相同
代碼示例
input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2) sample = nn.Upsample(scale_factor=2, mode='nearest') print(input) print(sample(input)) ''' tensor([[[[1., 2.],[3., 4.]]]]) tensor([[[[1., 1., 2., 2.],[1., 1., 2., 2.],[3., 3., 4., 4.],[3., 3., 4., 4.]]]]) '''二、bilinear
雙線性插值
代碼示例
input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2) sample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False) sample2 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True) print(input) print(sample(input)) print(sample2(input)) ''' tensor([[[[1., 2.],[3., 4.]]]]) 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]]]]) 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]]]]) '''關(guān)于align_corners參數(shù)的用法:給出PyTorch論壇的討論鏈接。下面是相應的圖解
Here is a simple illustration I made showing how a 4x4 image is upsampled to 8x8.
- When align_corners=True, pixels are regarded as a grid of points. Points at the corners are aligned.
- When align_corners=False, pixels are regarded as 1x1 areas. Area boundaries, rather than their centers, are aligned.
注意事項
在最近的使用中,程序報了如下錯誤
UserWarning: The default behavior for interpolate/upsample with float scale_factor changed in 1.6.0 to align with other frameworks/libraries, and now uses scale_factor directly, instead of relying on the computed output size. If you wish to restore the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details.
這個錯誤來源于,當我的Upsample中scale_factor為整數(shù)時不會報錯,為分數(shù)時就會報錯,這個解決方案是不對scale_factor進行賦值,只對size進行賦值,錯誤就消失了
代碼示例
input = torch.arange(1, 13, dtype=torch.float32).view(1, 3, 2, 2) # sample = nn.Upsample(size=(5), mode='nearest') # 使用這行,錯誤消失 sample = nn.Upsample(scale_factor=2.5, mode='nearest') # 報錯:如下 print(input) print(sample(input)) ''' tensor([[[[ 1., 2.],[ 3., 4.]],[[ 5., 6.],[ 7., 8.]],[[ 9., 10.],[11., 12.]]]]) /home/wangyh/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/functional.py:3103: UserWarning: The default behavior for interpolate/upsample with float scale_factor changed in 1.6.0 to align with other frameworks/libraries, and now uses scale_factor directly, instead of relying on the computed output size. If you wish to restore the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. warnings.warn("The default behavior for interpolate/upsample with float scale_factor changed " tensor([[[[ 1., 1., 1., 2., 2.],[ 1., 1., 1., 2., 2.],[ 1., 1., 1., 2., 2.],[ 3., 3., 3., 4., 4.],[ 3., 3., 3., 4., 4.]],[[ 5., 5., 5., 6., 6.],[ 5., 5., 5., 6., 6.],[ 5., 5., 5., 6., 6.],[ 7., 7., 7., 8., 8.],[ 7., 7., 7., 8., 8.]],[[ 9., 9., 9., 10., 10.],[ 9., 9., 9., 10., 10.],[ 9., 9., 9., 10., 10.],[11., 11., 11., 12., 12.],[11., 11., 11., 12., 12.]]]]) '''還有另一種方法,根據(jù)報錯內(nèi)容,我們找到Upsample的文檔介紹,其中有一句為,If you want downsampling/general resizing, you should use interpolate().,也就是說當我們采用下采樣的時候,scale factor為0.5這時候要用interpolate函數(shù),由此我們進入到interpolate函數(shù)的文檔中,我們再次找到一個提示信息When scale_factor is specified, if recompute_scale_factor=True, scale_factor is used to compute the output_size which will then be used to infer new scales for the interpolation. The default behavior for recompute_scale_factor changed to False in 1.6.0, and scale_factor is used in the interpolation calculation.,此時如果我們下采樣的話需要將scale factor設(shè)為0.5,同時加上recompute_scale_factor=True即可
總結(jié)
以上是生活随笔為你收集整理的nn.Upsample的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022飞鱼科技--鱼苗夏令营实习--游
- 下一篇: tomcat搭建简易网站