| 1 | # Tencent is pleased to support the open source community by making ncnn available. |
| 2 | # |
| 3 | # Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | # in compliance with the License. You may obtain a copy of the License at |
| 7 | # |
| 8 | # https://opensource.org/licenses/BSD-3-Clause |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software distributed |
| 11 | # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | # CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | # specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | import torch |
| 16 | import torch.nn as nn |
| 17 | import torch.nn.functional as F |
| 18 | |
| 19 | class Model(nn.Module): |
| 20 | def __init__(self): |
| 21 | super(Model, self).__init__() |
| 22 | |
| 23 | def forward(self, x, y, z, w): |
| 24 | x = F.upsample(x, size=16) |
| 25 | x = F.upsample(x, scale_factor=2, mode='nearest') |
| 26 | x = F.upsample(x, size=(20), mode='nearest') |
| 27 | x = F.upsample(x, scale_factor=(4), mode='nearest') |
| 28 | x = F.upsample(x, size=16, mode='linear') |
| 29 | x = F.upsample(x, scale_factor=2, mode='linear') |
| 30 | x = F.upsample(x, size=(24), mode='linear', align_corners=True) |
| 31 | x = F.upsample(x, scale_factor=(3), mode='linear', align_corners=True) |
| 32 | |
| 33 | y = F.upsample(y, size=16) |
| 34 | y = F.upsample(y, scale_factor=2, mode='nearest') |
| 35 | y = F.upsample(y, size=(20,20), mode='nearest') |
| 36 | y = F.upsample(y, scale_factor=(4,4), mode='nearest') |
| 37 | y = F.upsample(y, size=(16,24), mode='nearest') |
| 38 | y = F.upsample(y, scale_factor=(2,3), mode='nearest') |
| 39 | y = F.upsample(y, size=16, mode='bilinear') |
| 40 | y = F.upsample(y, scale_factor=2, mode='bilinear') |
| 41 | y = F.upsample(y, size=(20,20), mode='bilinear', align_corners=False) |
| 42 | y = F.upsample(y, scale_factor=(4,4), mode='bilinear', align_corners=False) |
| 43 | y = F.upsample(y, size=(16,24), mode='bilinear', align_corners=True) |
| 44 | y = F.upsample(y, scale_factor=(2,3), mode='bilinear', align_corners=True) |
| 45 | y = F.upsample(y, size=16, mode='bicubic') |
| 46 | y = F.upsample(y, scale_factor=2, mode='bicubic') |
| 47 | y = F.upsample(y, size=(20,20), mode='bicubic', align_corners=False) |
| 48 | y = F.upsample(y, scale_factor=(4,4), mode='bicubic', align_corners=False) |
| 49 | y = F.upsample(y, size=(16,24), mode='bicubic', align_corners=True) |
| 50 | y = F.upsample(y, scale_factor=(2,3), mode='bicubic', align_corners=True) |
| 51 | |
| 52 | z = F.upsample(z, size=16) |
| 53 | z = F.upsample(z, scale_factor=2, mode='nearest') |
| 54 | z = F.upsample(z, size=(20,20,20), mode='nearest') |
| 55 | z = F.upsample(z, scale_factor=(4,4,4), mode='nearest') |
| 56 | z = F.upsample(z, size=(16,24,20), mode='nearest') |
| 57 | z = F.upsample(z, scale_factor=(2,3,4), mode='nearest') |
| 58 | z = F.upsample(z, size=16, mode='trilinear') |
| 59 | z = F.upsample(z, scale_factor=2, mode='trilinear') |
| 60 | z = F.upsample(z, size=(20,20,20), mode='trilinear', align_corners=False) |
| 61 | z = F.upsample(z, scale_factor=(4,4,4), mode='trilinear', align_corners=False) |
| 62 | z = F.upsample(z, size=(16,24,20), mode='trilinear', align_corners=True) |
| 63 | z = F.upsample(z, scale_factor=(2,3,4), mode='trilinear', align_corners=True) |
| 64 | |
| 65 | w = F.upsample(w, scale_factor=(1.499,1.499), mode='nearest') |
| 66 | |
| 67 | return x, y, z, w |
| 68 | |
| 69 | def test(): |
| 70 | net = Model() |
| 71 | net.eval() |
| 72 | |
| 73 | torch.manual_seed(0) |
| 74 | x = torch.rand(1, 3, 32) |
| 75 | y = torch.rand(1, 3, 32, 32) |
| 76 | z = torch.rand(1, 3, 32, 32, 32) |
| 77 | w = torch.rand(1, 8, 12, 12) |
| 78 | |
| 79 | a = net(x, y, z, w) |
| 80 | |
| 81 | # export torchscript |
| 82 | mod = torch.jit.trace(net, (x, y, z, w)) |
| 83 | mod.save("test_F_upsample.pt") |
| 84 | |
| 85 | # torchscript to pnnx |
| 86 | import os |
| 87 | os.system("../src/pnnx test_F_upsample.pt inputshape=[1,3,32],[1,3,32,32],[1,3,32,32,32],[1,8,12,12]") |
| 88 | |
| 89 | # pnnx inference |
| 90 | import test_F_upsample_pnnx |
| 91 | b = test_F_upsample_pnnx.test_inference() |
| 92 | |
| 93 | for a0, b0 in zip(a, b): |
| 94 | if not torch.equal(a0, b0): |
| 95 | return False |
| 96 | return True |
| 97 | |
| 98 | if __name__ == "__main__": |
| 99 | if test(): |
| 100 | exit(0) |
| 101 | else: |
| 102 | exit(1) |