参考链接: class torch.nn.UpsamplingBilinear2d(size=None, scale_factor=None)
实验举例:
(base) PS C:\Users\chenxuqi> conda activate pytorch_1.7.1_cu102
(pytorch_1.7.1_cu102) PS C:\Users\chenxuqi> python
Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x00000226C1777890>
>>> a = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
>>> a
tensor([[[[1., 2.],
[3., 4.]]]])
>>> model = torch.nn.UpsamplingBilinear2d(scale_factor=2)
>>> b = model(a)
>>> b.shape
torch.Size([1, 1, 4, 4])
>>> a.shape
torch.Size([1, 1, 2, 2])
>>> a
tensor([[[[1., 2.],
[3., 4.]]]])
>>> b
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]]]])
>>>
>>>
>>> model = torch.nn.UpsamplingBilinear2d(size=(3,5))
>>> a
tensor([[[[1., 2.],
[3., 4.]]]])
>>> a.shape
torch.Size([1, 1, 2, 2])
>>> b = model(a)
>>> b.shape
torch.Size([1, 1, 3, 5])
>>> b
tensor([[[[1.0000, 1.2500, 1.5000, 1.7500, 2.0000],
[2.0000, 2.2500, 2.5000, 2.7500, 3.0000],
[3.0000, 3.2500, 3.5000, 3.7500, 4.0000]]]])
>>>
>>>
>>>
实验举例:
Microsoft Windows [版本 10.0.18363.1440]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>conda activate pytorch_1.7.1_cu102
(pytorch_1.7.1_cu102) C:\Users\chenxuqi>python
Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000002E60F3E7870>
>>> a = torch.randn(3,4,1024,512,requires_grad=True)
>>> a.shape
torch.Size([3, 4, 1024, 512])
>>> model = torch.nn.UpsamplingBilinear2d(scale_factor=(2,8))
>>> b = model(a)
>>> a.shape,b.shape
(torch.Size([3, 4, 1024, 512]), torch.Size([3, 4, 2048, 4096]))
>>>
>>>
>>> model = torch.nn.UpsamplingBilinear2d(size=(200,300))
>>> a = torch.randn(3,4,1024,512,requires_grad=True)
>>> a.shape
torch.Size([3, 4, 1024, 512])
>>> b = model(a)
>>> a.shape,b.shape
(torch.Size([3, 4, 1024, 512]), torch.Size([3, 4, 200, 300]))
>>>
>>>
>>>