| 1 | # Tencent is pleased to support the open source community by making ncnn available. |
| 2 | # |
| 3 | # Copyright (C) 2022 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 | self.up_0 = nn.Upsample(scale_factor=1, mode='nearest') |
| 24 | self.up_1 = nn.Upsample(size=(12,52), mode='bicubic', align_corners=True) |
| 25 | self.up_2 = nn.UpsamplingBilinear2d(scale_factor=(1,1)) |
| 26 | self.up_3 = nn.UpsamplingNearest2d(scale_factor=1) |
| 27 | |
| 28 | def forward(self, x): |
| 29 | x = self.up_0(x) |
| 30 | x = self.up_1(x) |
| 31 | x = self.up_2(x) |
| 32 | x = self.up_3(x) |
| 33 | x = F.upsample(x, scale_factor=1, mode='bilinear') |
| 34 | x = F.upsample(x, size=(12,52), mode='bicubic', align_corners=True) |
| 35 | x = F.upsample_bilinear(x, scale_factor=1) |
| 36 | x = F.upsample_nearest(x, size=(12,52)) |
| 37 | x = F.interpolate(x, scale_factor=(1,1), mode='nearest', recompute_scale_factor=True) |
| 38 | x = F.interpolate(x, scale_factor=(1,1), mode='bicubic', align_corners=True, recompute_scale_factor=True) |
| 39 | x = F.interpolate(x, size=(12,52), mode='bicubic', align_corners=False) |
| 40 | x = F.relu(x) |
| 41 | return x |
| 42 | |
| 43 | def test(): |
| 44 | net = Model() |
| 45 | net.eval() |
| 46 | |
| 47 | torch.manual_seed(0) |
| 48 | x = torch.rand(1, 15, 12, 52) |
| 49 | |
| 50 | a = net(x) |
| 51 | |
| 52 | # export torchscript |
| 53 | mod = torch.jit.trace(net, x) |
| 54 | mod.save("test_pnnx_eliminate_noop_upsample.pt") |
| 55 | |
| 56 | # torchscript to pnnx |
| 57 | import os |
| 58 | os.system("../src/pnnx test_pnnx_eliminate_noop_upsample.pt inputshape=[1,15,12,52]") |
| 59 | |
| 60 | # pnnx inference |
| 61 | import test_pnnx_eliminate_noop_upsample_pnnx |
| 62 | b = test_pnnx_eliminate_noop_upsample_pnnx.test_inference() |
| 63 | |
| 64 | return torch.equal(a, b) |
| 65 | |
| 66 | if __name__ == "__main__": |
| 67 | if test(): |
| 68 | exit(0) |
| 69 | else: |
| 70 | exit(1) |