Elffuss BraidHub

kikocis/ncnn public

master · 2.7 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.MaxPool2d(kernel_size=3)
24 self.pool_1 = nn.MaxPool2d(kernel_size=4, stride=2, padding=2, dilation=1)
25 self.pool_2 = nn.MaxPool2d(kernel_size=(1,3), stride=1, padding=(0,1), dilation=1, return_indices=False, ceil_mode=False)
26 self.pool_3 = nn.MaxPool2d(kernel_size=(4,5), stride=(1,2), padding=(1,2), dilation=1, return_indices=False, ceil_mode=True)
27 self.pool_4 = nn.MaxPool2d(kernel_size=(2,3), stride=1, padding=1, dilation=(1,2), return_indices=False, ceil_mode=False)
28 self.pool_5 = nn.MaxPool2d(kernel_size=2, stride=1, padding=0, dilation=1, return_indices=True, ceil_mode=True)
29 self.pool_6 = nn.MaxPool2d(kernel_size=(5,4), stride=1, padding=2, dilation=1, return_indices=True, ceil_mode=False)
30
31 def forward(self, x, y):
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, tx = self.pool_5(x)
38 x, indices = self.pool_6(x)
39
40 y = self.pool_0(y)
41 y = self.pool_1(y)
42 y = self.pool_2(y)
43 y = self.pool_3(y)
44 y = self.pool_4(y)
45 y, ty = self.pool_5(y)
46 return x, indices, y
47
48def test():
49 net = Model()
50 net.eval()
51
52 torch.manual_seed(0)
53 x = torch.rand(1, 12, 64, 64)
54 y = torch.rand(12, 64, 64)
55
56 a = net(x, y)
57
58 # export torchscript
59 mod = torch.jit.trace(net, (x, y))
60 mod.save("test_nn_MaxPool2d.pt")
61
62 # torchscript to pnnx
63 import os
64 os.system("../src/pnnx test_nn_MaxPool2d.pt inputshape=[1,12,64,64],[12,64,64]")
65
66 # pnnx inference
67 import test_nn_MaxPool2d_pnnx
68 b = test_nn_MaxPool2d_pnnx.test_inference()
69
70 for a0, b0 in zip(a, b):
71 if not torch.equal(a0, b0):
72 return False
73 return True
74
75if __name__ == "__main__":
76 if test():
77 exit(0)
78 else:
79 exit(1)