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) 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.deconv_0 = nn.ConvTranspose2d(in_channels=12, out_channels=16, kernel_size=3)
24 self.deconv_1 = nn.ConvTranspose2d(in_channels=16, out_channels=20, kernel_size=(2,4), stride=(2,1), padding=2, output_padding=0)
25 self.deconv_2 = nn.ConvTranspose2d(in_channels=20, out_channels=24, kernel_size=(1,3), stride=1, padding=(2,4), output_padding=(0,0), dilation=1, groups=1, bias=False)
26 self.deconv_3 = nn.ConvTranspose2d(in_channels=24, out_channels=28, kernel_size=(5,4), stride=2, padding=0, output_padding=(0,1), dilation=1, groups=4, bias=True)
27 self.deconv_4 = nn.ConvTranspose2d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding=1, output_padding=0, dilation=(1,2), groups=2, bias=False)
28 self.deconv_5 = nn.ConvTranspose2d(in_channels=32, out_channels=32, kernel_size=2, stride=2, padding=3, output_padding=1, dilation=1, groups=32, bias=True)
29 self.deconv_6 = nn.ConvTranspose2d(in_channels=32, out_channels=28, kernel_size=2, stride=1, padding=2, output_padding=0, dilation=1, groups=1, bias=False)
30 self.deconv_7 = nn.ConvTranspose2d(in_channels=28, out_channels=24, kernel_size=3, stride=2, padding=(5,6), output_padding=(1,0), dilation=2, groups=1, bias=True)
31
32 self.downsample = nn.Conv2d(24, 16, 3, stride=2, padding=1)
33 self.upsample = nn.ConvTranspose2d(16, 24, 3, stride=2, padding=1)
34
35 def forward(self, x):
36 x = self.deconv_0(x)
37 x = self.deconv_1(x)
38 x = self.deconv_2(x)
39 x = self.deconv_3(x)
40 x = self.deconv_4(x)
41 x = self.deconv_5(x)
42 x = self.deconv_6(x)
43 x = self.deconv_7(x)
44
45 y = self.downsample(x)
46 x = self.upsample(y, output_size=x.size())
47
48 return x
49
50def test():
51 net = Model()
52 net.eval()
53
54 torch.manual_seed(0)
55 x = torch.rand(1, 12, 10, 10)
56
57 a = net(x)
58
59 # export onnx
60 torch.onnx.export(net, (x, ), "test_nn_ConvTranspose2d.onnx")
61
62 # onnx to pnnx
63 import os
64 os.system("../../src/pnnx test_nn_ConvTranspose2d.onnx inputshape=[1,12,10,10]")
65
66 # pnnx inference
67 import test_nn_ConvTranspose2d_pnnx
68 b = test_nn_ConvTranspose2d_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)