| 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.ConvTranspose2d(in_channels=12, out_channels=16, kernel_size=3) |
| 24 | self.bn_0 = nn.BatchNorm2d(num_features=16) |
| 25 | self.deconv_1 = nn.ConvTranspose2d(in_channels=16, out_channels=20, kernel_size=(2,4), stride=(2,1), padding=2, output_padding=0) |
| 26 | self.bn_1 = nn.BatchNorm2d(num_features=20) |
| 27 | 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) |
| 28 | self.bn_2 = nn.BatchNorm2d(num_features=24, eps=1e-1, affine=False) |
| 29 | 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) |
| 30 | self.bn_3 = nn.BatchNorm2d(num_features=28, eps=1e-1, affine=False) |
| 31 | 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) |
| 32 | self.bn_4 = nn.BatchNorm2d(num_features=32) |
| 33 | 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) |
| 34 | self.bn_5 = nn.BatchNorm2d(num_features=32) |
| 35 | 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) |
| 36 | self.bn_6 = nn.BatchNorm2d(num_features=28, affine=True) |
| 37 | 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) |
| 38 | self.bn_7 = nn.BatchNorm2d(num_features=24, affine=True) |
| 39 | |
| 40 | def forward(self, x): |
| 41 | x = self.deconv_0(x) |
| 42 | x = self.bn_0(x) |
| 43 | x = self.deconv_1(x) |
| 44 | x = self.bn_1(x) |
| 45 | x = self.deconv_2(x) |
| 46 | x = self.bn_2(x) |
| 47 | x = self.deconv_3(x) |
| 48 | x = self.bn_3(x) |
| 49 | x = self.deconv_4(x) |
| 50 | x = self.bn_4(x) |
| 51 | x = self.deconv_5(x) |
| 52 | x = self.bn_5(x) |
| 53 | x = self.deconv_6(x) |
| 54 | x = self.bn_6(x) |
| 55 | x = self.deconv_7(x) |
| 56 | x = self.bn_7(x) |
| 57 | |
| 58 | return x |
| 59 | |
| 60 | def test(): |
| 61 | net = Model() |
| 62 | net.eval() |
| 63 | |
| 64 | torch.manual_seed(0) |
| 65 | x = torch.rand(1, 12, 10, 10) |
| 66 | |
| 67 | a = net(x) |
| 68 | |
| 69 | # export torchscript |
| 70 | mod = torch.jit.trace(net, x) |
| 71 | mod.save("test_pnnx_fuse_convtranspose2d_batchnorm2d.pt") |
| 72 | |
| 73 | # torchscript to pnnx |
| 74 | import os |
| 75 | os.system("../src/pnnx test_pnnx_fuse_convtranspose2d_batchnorm2d.pt inputshape=[1,12,10,10]") |
| 76 | |
| 77 | # pnnx inference |
| 78 | import test_pnnx_fuse_convtranspose2d_batchnorm2d_pnnx |
| 79 | b = test_pnnx_fuse_convtranspose2d_batchnorm2d_pnnx.test_inference() |
| 80 | |
| 81 | return torch.allclose(a, b, 1e-4, 1e-4) |
| 82 | |
| 83 | if __name__ == "__main__": |
| 84 | if test(): |
| 85 | exit(0) |
| 86 | else: |
| 87 | exit(1) |