Elffuss BraidHub

kikocis/ncnn public

master · 3.2 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.conv_0 = nn.Conv3d(in_channels=12, out_channels=16, kernel_size=3)
25 self.conv_1 = nn.Conv3d(in_channels=16, out_channels=20, kernel_size=(2,3,4), stride=(2,2,1), padding=2, dilation=1)
26 self.conv_2 = nn.Conv3d(in_channels=20, out_channels=24, kernel_size=(1,2,3), stride=1, padding=(2,4,1), dilation=1, groups=1, bias=False)
27 self.conv_3 = nn.Conv3d(in_channels=24, out_channels=28, kernel_size=(5,4,3), stride=1, padding=0, dilation=1, groups=4, bias=True)
28 self.conv_4 = nn.Conv3d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding=1, dilation=(1,2,2), groups=2, bias=False, padding_mode='zeros')
29 # self.conv_3 = nn.Conv3d(in_channels=24, out_channels=28, kernel_size=(5,4,3), stride=1, padding='valid', dilation=1, groups=4, bias=True)
30 # self.conv_4 = nn.Conv3d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding='same', dilation=(1,2,2), groups=2, bias=False, padding_mode='zeros')
31 if version.parse(torch.__version__) >= version.parse('1.10'):
32 self.conv_5 = nn.Conv3d(in_channels=32, out_channels=32, kernel_size=2, stride=2, padding=3, dilation=1, groups=32, bias=True, padding_mode='reflect')
33 self.conv_6 = nn.Conv3d(in_channels=32, out_channels=28, kernel_size=2, stride=1, padding=2, dilation=1, groups=1, bias=False, padding_mode='replicate')
34 # self.conv_7 = nn.Conv3d(in_channels=28, out_channels=24, kernel_size=3, stride=2, padding=(5,6), dilation=2, groups=1, bias=True, padding_mode='circular')
35
36 def forward(self, x):
37 x = self.conv_0(x)
38 x = self.conv_1(x)
39 x = self.conv_2(x)
40 x = self.conv_3(x)
41 x = self.conv_4(x)
42 if version.parse(torch.__version__) < version.parse('1.10'):
43 return x
44
45 x = self.conv_5(x)
46 x = self.conv_6(x)
47 #x = self.conv_7(x)
48
49 return x
50
51def test():
52 net = Model()
53 net.eval()
54
55 torch.manual_seed(0)
56 x = torch.rand(1, 12, 48, 48, 64)
57
58 a = net(x)
59
60 # export onnx
61 torch.onnx.export(net, (x, ), "test_nn_Conv3d.onnx")
62
63 # onnx to pnnx
64 import os
65 os.system("../../src/pnnx test_nn_Conv3d.onnx inputshape=[1,12,48,48,64]")
66
67 # pnnx inference
68 import test_nn_Conv3d_pnnx
69 b = test_nn_Conv3d_pnnx.test_inference()
70
71 return torch.equal(a, b)
72
73if __name__ == "__main__":
74 if test():
75 exit(0)
76 else:
77 exit(1)