Elffuss BraidHub

kikocis/ncnn public

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