alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

【581】PyTorch 实现上采样 —— nn.Upsampling

参考:pytorch torch.nn 实现上采样——nn.Upsample

参考:PyTorch 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.00001.25001.75002.0000],
          [ 1.50001.75002.25002.5000],
          [ 2.50002.75003.25003.5000],
          [ 3.00003.25003.75004.0000]]]])
 
>>> m = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
>>> m(input)
tensor([[[[ 1.00001.33331.66672.0000],
          [ 1.66672.00002.33332.6667],
          [ 2.33332.66673.00003.3333],
          [ 3.00003.33333.66674.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.00001.25001.75001.50000.50000.0000],
          [ 1.50001.75002.25001.87500.62500.0000],
          [ 2.50002.75003.25002.62500.87500.0000],
          [ 2.25002.43752.81252.25000.75000.0000],
          [ 0.75000.81250.93750.75000.25000.0000],
          [ 0.00000.00000.00000.00000.00000.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.00001.40001.80001.60000.80000.0000],
          [ 1.80002.20002.60002.24001.12000.0000],
          [ 2.60003.00003.40002.88001.44000.0000],
          [ 2.40002.72003.04002.56001.28000.0000],
          [ 1.20001.36001.52001.28000.64000.0000],
          [ 0.00000.00000.00000.00000.00000.0000]]]])

 

posted on   McDelfino  阅读(555)  评论(0编辑  收藏  举报

编辑推荐:
· 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 语法参考
点击右上角即可分享
微信分享提示