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