【581】PyTorch 实现上采样 —— nn.Upsampling
参考:pytorch torch.nn 实现上采样——nn.Upsample
参考:Official - Docs > torch.nn > Upsample
举例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | >>> 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 ]]]]) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2019-06-28 【418】C语言ADT实现Quack(stack+queue)
2019-06-28 【417】一条语句编译并执行C语言
2016-06-28 【207】WinForm Chart类
2012-06-28 【054】◀▶ JavaScript 语法参考