Elffuss BraidHub

kikocis/ncnn public

master · 1.8 KiBRawPermalink
1# Tencent is pleased to support the open source community by making ncnn available.
2#
3# Copyright (C) 2024 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
17from packaging import version
18
19def test():
20 net = models.squeezenet1_1()
21 net.eval()
22
23 torch.manual_seed(0)
24 x = torch.rand(1, 3, 224, 224)
25
26 a = net(x)
27
28 # export onnx
29 torch.onnx.export(net, (x,), "test_squeezenet1_1.onnx")
30
31 # onnx to pnnx
32 import os
33 os.system("../../src/pnnx test_squeezenet1_1.onnx 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 if not torch.allclose(a, b, 1e-4, 1e-4):
40 return False
41
42 if version.parse(torch.__version__) < version.parse('2.5'):
43 return True
44
45 # export dynamo onnx
46 torch.onnx.dynamo_export(net, x).save("test_squeezenet1_1_dynamo.onnx")
47
48 # onnx to pnnx
49 os.system("../../src/pnnx test_squeezenet1_1_dynamo.onnx inputshape=[1,3,224,224]")
50
51 # pnnx inference
52 import test_squeezenet1_1_dynamo_pnnx
53 b = test_squeezenet1_1_dynamo_pnnx.test_inference()
54
55 return torch.allclose(a, b, 1e-4, 1e-4)
56
57if __name__ == "__main__":
58 if test():
59 exit(0)
60 else:
61 exit(1)