Elffuss BraidHub

kikocis/ncnn public

master · 2.3 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
18
19class Model(nn.Module):
20 def __init__(self):
21 super(Model, self).__init__()
22
23 self.pool_0 = nn.AvgPool3d(kernel_size=3)
24 self.pool_1 = nn.AvgPool3d(kernel_size=4, stride=2, padding=2)
25 self.pool_2 = nn.AvgPool3d(kernel_size=(1,2,3), stride=1, padding=(0,1,1), ceil_mode=False, count_include_pad=True)
26 self.pool_3 = nn.AvgPool3d(kernel_size=(3,4,5), stride=(1,2,2), padding=(1,1,2), ceil_mode=True, count_include_pad=False)
27 self.pool_4 = nn.AvgPool3d(kernel_size=(5,4,3), stride=(2,1,1), padding=1, ceil_mode=False, count_include_pad=True)
28 self.pool_5 = nn.AvgPool3d(kernel_size=2, stride=1, padding=0, ceil_mode=True, count_include_pad=True)
29 # self.pool_6 = nn.AvgPool3d(kernel_size=(5,4,4), stride=1, padding=2, ceil_mode=False, count_include_pad=False, divisor_override=77)
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, 96, 128, 128)
47
48 a = net(x)
49
50 # export onnx
51 torch.onnx.export(net, (x,), "test_nn_AvgPool3d.onnx")
52
53 # onnx to pnnx
54 import os
55 os.system("../../src/pnnx test_nn_AvgPool3d.onnx inputshape=[1,12,96,128,128]")
56
57 # pnnx inference
58 import test_nn_AvgPool3d_pnnx
59 b = test_nn_AvgPool3d_pnnx.test_inference()
60
61 return torch.equal(a, b)
62
63if __name__ == "__main__":
64 if test():
65 exit(0)
66 else:
67 exit(1)