Elffuss BraidHub

kikocis/ncnn public

master · 3.9 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.Conv2d(in_channels=12, out_channels=16, kernel_size=3)
25 self.bn_0 = nn.BatchNorm2d(num_features=16)
26 self.conv_1 = nn.Conv2d(in_channels=16, out_channels=20, kernel_size=(2,4), stride=(2,1), padding=2, dilation=1)
27 self.bn_1 = nn.BatchNorm2d(num_features=20)
28 self.conv_2 = nn.Conv2d(in_channels=20, out_channels=24, kernel_size=(1,3), stride=1, padding=(2,4), dilation=1, groups=1, bias=False)
29 self.bn_2 = nn.BatchNorm2d(num_features=24)
30 if version.parse(torch.__version__) < version.parse('1.9'):
31 self.conv_3 = nn.Conv2d(in_channels=24, out_channels=28, kernel_size=(5,4), stride=1, padding=0, dilation=1, groups=4, bias=True)
32 else:
33 self.conv_3 = nn.Conv2d(in_channels=24, out_channels=28, kernel_size=(5,4), stride=1, padding='valid', dilation=1, groups=4, bias=True)
34 self.bn_3 = nn.BatchNorm2d(num_features=28)
35 if version.parse(torch.__version__) < version.parse('1.9'):
36 self.conv_4 = nn.Conv2d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding=1, dilation=(1,2), groups=2, bias=False, padding_mode='zeros')
37 else:
38 self.conv_4 = nn.Conv2d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding='same', dilation=(1,2), groups=2, bias=False, padding_mode='zeros')
39 self.bn_4 = nn.BatchNorm2d(num_features=32)
40 self.conv_5 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size=2, stride=2, padding=3, dilation=1, groups=32, bias=True, padding_mode='reflect')
41 self.bn_5 = nn.BatchNorm2d(num_features=32)
42 self.conv_6 = nn.Conv2d(in_channels=32, out_channels=28, kernel_size=2, stride=1, padding=2, dilation=1, groups=1, bias=False, padding_mode='replicate')
43 self.bn_6 = nn.BatchNorm2d(num_features=28)
44 #self.conv_7 = nn.Conv2d(in_channels=28, out_channels=24, kernel_size=3, stride=2, padding=(5,6), dilation=2, groups=1, bias=True, padding_mode='circular')
45 #self.bn_7 = nn.BatchNorm2d(num_features=24)
46
47 def forward(self, x):
48 x = self.conv_0(x)
49 x = self.bn_0(x)
50 x = self.conv_1(x)
51 x = self.bn_1(x)
52 x = self.conv_2(x)
53 x = self.bn_2(x)
54 x = self.conv_3(x)
55 x = self.bn_3(x)
56 x = self.conv_4(x)
57 x = self.bn_4(x)
58 x = self.conv_5(x)
59 x = self.bn_5(x)
60 x = self.conv_6(x)
61 x = self.bn_6(x)
62 #x = self.conv_7(x)
63 #x = self.bn_7(x)
64
65 return x
66
67def test():
68 net = Model()
69 net.eval()
70
71 torch.manual_seed(0)
72 x = torch.rand(1, 12, 64, 64)
73
74 a = net(x)
75
76 # export torchscript
77 mod = torch.jit.trace(net, x)
78 mod.save("test_pnnx_fuse_conv2d_batchnorm2d.pt")
79
80 # torchscript to pnnx
81 import os
82 os.system("../src/pnnx test_pnnx_fuse_conv2d_batchnorm2d.pt inputshape=[1,12,64,64]")
83
84 # pnnx inference
85 import test_pnnx_fuse_conv2d_batchnorm2d_pnnx
86 b = test_pnnx_fuse_conv2d_batchnorm2d_pnnx.test_inference()
87
88 return torch.allclose(a, b, 1e-4, 1e-4)
89
90if __name__ == "__main__":
91 if test():
92 exit(0)
93 else:
94 exit(1)