| 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 torchvision.models as models |
| 17 | |
| 18 | def test(): |
| 19 | net = models.squeezenet1_1() |
| 20 | net.eval() |
| 21 | |
| 22 | torch.manual_seed(0) |
| 23 | x = torch.rand(1, 3, 224, 224) |
| 24 | |
| 25 | a = net(x) |
| 26 | |
| 27 | # export torchscript |
| 28 | mod = torch.jit.trace(net, x) |
| 29 | mod.save("test_squeezenet1_1.pt") |
| 30 | |
| 31 | # torchscript to pnnx |
| 32 | import os |
| 33 | os.system("../src/pnnx test_squeezenet1_1.pt inputshape=[1,3,224,224]") |
| 34 | |
| 35 | # pnnx inference |
| 36 | import test_squeezenet1_1_pnnx |
| 37 | b = test_squeezenet1_1_pnnx.test_inference() |
| 38 | |
| 39 | return torch.equal(a, b) |
| 40 | |
| 41 | if __name__ == "__main__": |
| 42 | if test(): |
| 43 | exit(0) |
| 44 | else: |
| 45 | exit(1) |