Elffuss BraidHub

kikocis/ncnn public

master · 2.2 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 torch.nn as nn
17import torch.nn.functional as F
18
19class Model(nn.Module):
20 def __init__(self):
21 super(Model, self).__init__()
22
23 self.pool_0 = nn.LPPool2d(norm_type=2, kernel_size=3)
24 self.pool_1 = nn.LPPool2d(norm_type=2, kernel_size=4, stride=2)
25 self.pool_2 = nn.LPPool2d(norm_type=1, kernel_size=(1,3), stride=1, ceil_mode=False)
26 self.pool_3 = nn.LPPool2d(norm_type=1, kernel_size=(4,5), stride=(1,2), ceil_mode=True)
27 self.pool_4 = nn.LPPool2d(norm_type=1.2, kernel_size=(5,3), stride=(2,1), ceil_mode=False)
28 self.pool_5 = nn.LPPool2d(norm_type=0.5, kernel_size=2, stride=1, ceil_mode=True)
29 self.pool_6 = nn.LPPool2d(norm_type=0.1, kernel_size=(5,4), stride=1, ceil_mode=False)
30
31 def forward(self, x):
32 x = self.pool_0(x)
33 x = self.pool_1(x)
34 x = self.pool_2(x)
35 x = self.pool_3(x)
36 x = self.pool_4(x)
37 x = self.pool_5(x)
38 x = self.pool_6(x)
39 return x
40
41def test():
42 net = Model()
43 net.eval()
44
45 torch.manual_seed(0)
46 x = torch.rand(1, 12, 128, 128)
47
48 a = net(x)
49
50 # export torchscript
51 mod = torch.jit.trace(net, x)
52 mod.save("test_nn_LPPool2d.pt")
53
54 # torchscript to pnnx
55 import os
56 os.system("../src/pnnx test_nn_LPPool2d.pt inputshape=[1,12,128,128]")
57
58 # pnnx inference
59 import test_nn_LPPool2d_pnnx
60 b = test_nn_LPPool2d_pnnx.test_inference()
61
62 return torch.equal(a, b)
63
64if __name__ == "__main__":
65 if test():
66 exit(0)
67 else:
68 exit(1)