Elffuss BraidHub

kikocis/ncnn public

master · 1.3 KiBRawPermalink
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
15import torch
16import torchvision.models as models
17
18def test():
19 net = models.resnet18()
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_resnet18.pt")
30
31 # torchscript to pnnx
32 import os
33 os.system("../src/pnnx test_resnet18.pt inputshape=[1,3,224,224]")
34
35 # pnnx inference
36 import test_resnet18_pnnx
37 b = test_resnet18_pnnx.test_inference()
38
39 return torch.allclose(a, b, 1e-4, 1e-4)
40
41if __name__ == "__main__":
42 if test():
43 exit(0)
44 else:
45 exit(1)