Elffuss BraidHub

kikocis/ncnn public

master · 2.0 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 torch.nn as nn
17import torch.nn.functional as F
18from packaging import version
19
20class Model(nn.Module):
21 def __init__(self):
22 super(Model, self).__init__()
23
24 self.up_0 = nn.UpsamplingBilinear2d(size=16)
25 self.up_1 = nn.UpsamplingBilinear2d(scale_factor=2)
26 self.up_2 = nn.UpsamplingBilinear2d(size=(20,20))
27 self.up_3 = nn.UpsamplingBilinear2d(scale_factor=(4,4))
28 self.up_4 = nn.UpsamplingBilinear2d(size=(16,24))
29 self.up_5 = nn.UpsamplingBilinear2d(scale_factor=(2,3))
30
31 def forward(self, x):
32 x = self.up_0(x)
33 x = self.up_1(x)
34 x = self.up_2(x)
35 x = self.up_3(x)
36 x = self.up_4(x)
37 x = self.up_5(x)
38 return x
39
40def test():
41 if version.parse(torch.__version__) < version.parse('1.12'):
42 return True
43
44 net = Model()
45 net.eval()
46
47 torch.manual_seed(0)
48 x = torch.rand(1, 3, 32, 32)
49
50 a = net(x)
51
52 # export onnx
53 torch.onnx.export(net, (x, ), "test_nn_UpsamplingBilinear2d.onnx")
54
55 # onnx to pnnx
56 import os
57 os.system("../../src/pnnx test_nn_UpsamplingBilinear2d.onnx inputshape=[1,3,32,32]")
58
59 # pnnx inference
60 import test_nn_UpsamplingBilinear2d_pnnx
61 b = test_nn_UpsamplingBilinear2d_pnnx.test_inference()
62
63 return torch.equal(a, b)
64
65if __name__ == "__main__":
66 if test():
67 exit(0)
68 else:
69 exit(1)