| 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 | |
| 15 | import torch |
| 16 | import torch.nn as nn |
| 17 | import torch.nn.functional as F |
| 18 | |
| 19 | class Model(nn.Module): |
| 20 | def __init__(self): |
| 21 | super(Model, self).__init__() |
| 22 | |
| 23 | self.deconv_0 = nn.ConvTranspose1d(in_channels=12, out_channels=16, kernel_size=3) |
| 24 | self.deconv_1 = nn.ConvTranspose1d(in_channels=16, out_channels=20, kernel_size=2, stride=2, padding=2, output_padding=0) |
| 25 | self.deconv_2 = nn.ConvTranspose1d(in_channels=20, out_channels=24, kernel_size=3, stride=1, padding=(2), output_padding=(0), dilation=1, groups=1, bias=False) |
| 26 | self.deconv_3 = nn.ConvTranspose1d(in_channels=24, out_channels=28, kernel_size=5, stride=2, padding=0, output_padding=(1), dilation=1, groups=4, bias=True) |
| 27 | self.deconv_4 = nn.ConvTranspose1d(in_channels=28, out_channels=32, kernel_size=3, stride=1, padding=1, output_padding=0, dilation=2, groups=2, bias=False) |
| 28 | self.deconv_5 = nn.ConvTranspose1d(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.ConvTranspose1d(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.ConvTranspose1d(in_channels=28, out_channels=24, kernel_size=3, stride=2, padding=(6), output_padding=(1), dilation=2, groups=1, bias=True) |
| 31 | |
| 32 | self.downsample = nn.Conv1d(24, 16, 3, stride=2, padding=1) |
| 33 | self.upsample = nn.ConvTranspose1d(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 | |
| 50 | def test(): |
| 51 | net = Model() |
| 52 | net.eval() |
| 53 | |
| 54 | torch.manual_seed(0) |
| 55 | x = torch.rand(1, 12, 10) |
| 56 | |
| 57 | a = net(x) |
| 58 | |
| 59 | # export torchscript |
| 60 | mod = torch.jit.trace(net, x) |
| 61 | mod.save("test_nn_ConvTranspose1d.pt") |
| 62 | |
| 63 | # torchscript to pnnx |
| 64 | import os |
| 65 | os.system("../src/pnnx test_nn_ConvTranspose1d.pt inputshape=[1,12,10]") |
| 66 | |
| 67 | # pnnx inference |
| 68 | import test_nn_ConvTranspose1d_pnnx |
| 69 | b = test_nn_ConvTranspose1d_pnnx.test_inference() |
| 70 | |
| 71 | return torch.equal(a, b) |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | if test(): |
| 75 | exit(0) |
| 76 | else: |
| 77 | exit(1) |