日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

nn.Upsample

發(fā)布時(shí)間:2024/3/12 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nn.Upsample 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

寫(xiě)在前面:在PyTorch中有兩種上采樣/下采樣的方法,一種是Upsample,另一種是interpolate
這兩個(gè)函數(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(這個(gè)參數(shù)下面會(huì)單獨(dú)講)

此函數(shù)可以上采樣或者下采樣(但是在PyTorch中下采樣推薦interpolate,不過(guò)我發(fā)現(xiàn)沒(méi)有區(qū)別?)
在上采樣/下采樣的過(guò)程中,只需要給定size或者scale_factor一個(gè)值即可,不需要給定所有的值,不然會(huì)沖突

一、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)

上采樣

舉個(gè)例子,假設(shè)放大倍數(shù)為2,那么src=des×0.5src=des\times 0.5src=des×0.5,對(duì)于下圖,坐標(biāo)(3,2)處的顏色應(yīng)為原來(lái)坐標(biāo)系(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)對(duì)應(yīng)橙色

下采樣

下采樣和上采樣的處理過(guò)程完全相同

代碼示例

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

雙線(xiàn)性插值

代碼示例

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論壇的討論鏈接。下面是相應(yīng)的圖解

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.

注意事項(xiàng)

在最近的使用中,程序報(bào)了如下錯(cuò)誤

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.

這個(gè)錯(cuò)誤來(lái)源于,當(dāng)我的Upsample中scale_factor為整數(shù)時(shí)不會(huì)報(bào)錯(cuò),為分?jǐn)?shù)時(shí)就會(huì)報(bào)錯(cuò),這個(gè)解決方案是不對(duì)scale_factor進(jìn)行賦值,只對(duì)size進(jìn)行賦值,錯(cuò)誤就消失了

代碼示例

input = torch.arange(1, 13, dtype=torch.float32).view(1, 3, 2, 2) # sample = nn.Upsample(size=(5), mode='nearest') # 使用這行,錯(cuò)誤消失 sample = nn.Upsample(scale_factor=2.5, mode='nearest') # 報(bào)錯(cuò):如下 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ù)報(bào)錯(cuò)內(nèi)容,我們找到Upsample的文檔介紹,其中有一句為,If you want downsampling/general resizing, you should use interpolate().,也就是說(shuō)當(dāng)我們采用下采樣的時(shí)候,scale factor為0.5這時(shí)候要用interpolate函數(shù),由此我們進(jìn)入到interpolate函數(shù)的文檔中,我們?cè)俅握业揭粋€(gè)提示信息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.,此時(shí)如果我們下采樣的話(huà)需要將scale factor設(shè)為0.5,同時(shí)加上recompute_scale_factor=True即可

總結(jié)

以上是生活随笔為你收集整理的nn.Upsample的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。