Elffuss BraidHub

kikocis/ncnn public

master · 3.3 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
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 if version.parse(torch.__version__) < version.parse('1.9'):
28 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)
29 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')
30 else:
31 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)
32 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')
33 if version.parse(torch.__version__) >= version.parse('1.10'):
34 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')
35 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')
36 # 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')
37
38 def forward(self, x):
39 x = self.conv_0(x)
40 x = self.conv_1(x)
41 x = self.conv_2(x)
42 x = self.conv_3(x)
43 x = self.conv_4(x)
44 if version.parse(torch.__version__) < version.parse('1.10'):
45 return x
46
47 x = self.conv_5(x)
48 x = self.conv_6(x)
49 #x = self.conv_7(x)
50
51 return x
52
53def test():
54 net = Model()
55 net.eval()
56
57 torch.manual_seed(0)
58 x = torch.rand(1, 12, 48, 48, 64)
59
60 a = net(x)
61
62 # export torchscript
63 mod = torch.jit.trace(net, x)
64 mod.save("test_nn_Conv3d.pt")
65
66 # torchscript to pnnx
67 import os
68 os.system("../src/pnnx test_nn_Conv3d.pt inputshape=[1,12,48,48,64]")
69
70 # pnnx inference
71 import test_nn_Conv3d_pnnx
72 b = test_nn_Conv3d_pnnx.test_inference()
73
74 return torch.equal(a, b)
75
76if __name__ == "__main__":
77 if test():
78 exit(0)
79 else:
80 exit(1)