| 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 | |
| 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.pool_0 = nn.MaxPool3d(kernel_size=3) |
| 25 | self.pool_1 = nn.MaxPool3d(kernel_size=4, stride=2, padding=2, dilation=1) |
| 26 | self.pool_2 = nn.MaxPool3d(kernel_size=(1,2,3), stride=1, padding=(0,0,1), dilation=1, return_indices=False, ceil_mode=False) |
| 27 | self.pool_3 = nn.MaxPool3d(kernel_size=(3,4,5), stride=(1,2,2), padding=(1,2,2), dilation=1, return_indices=False, ceil_mode=True) |
| 28 | if version.parse(torch.__version__) < version.parse('1.12'): |
| 29 | self.pool_4 = nn.MaxPool3d(kernel_size=(2,3,3), stride=1, padding=1, dilation=1, return_indices=False, ceil_mode=False) |
| 30 | else: |
| 31 | self.pool_4 = nn.MaxPool3d(kernel_size=(2,3,3), stride=1, padding=1, dilation=(1,2,2), return_indices=False, ceil_mode=False) |
| 32 | self.pool_5 = nn.MaxPool3d(kernel_size=2, stride=1, padding=0, dilation=1, return_indices=True, ceil_mode=True) |
| 33 | self.pool_6 = nn.MaxPool3d(kernel_size=(5,4,4), stride=1, padding=2, dilation=1, return_indices=True, ceil_mode=False) |
| 34 | |
| 35 | def forward(self, x): |
| 36 | x = self.pool_0(x) |
| 37 | x = self.pool_1(x) |
| 38 | x = self.pool_2(x) |
| 39 | x = self.pool_3(x) |
| 40 | x = self.pool_4(x) |
| 41 | x, tx = self.pool_5(x) |
| 42 | x, indices = self.pool_6(x) |
| 43 | return x, indices |
| 44 | |
| 45 | def test(): |
| 46 | net = Model() |
| 47 | net.eval() |
| 48 | |
| 49 | torch.manual_seed(0) |
| 50 | x = torch.rand(1, 12, 64, 64, 64) |
| 51 | |
| 52 | a = net(x) |
| 53 | |
| 54 | # export onnx |
| 55 | torch.onnx.export(net, (x,), "test_nn_MaxPool3d.onnx") |
| 56 | |
| 57 | # onnx to pnnx |
| 58 | import os |
| 59 | os.system("../../src/pnnx test_nn_MaxPool3d.onnx inputshape=[1,12,64,64,64]") |
| 60 | |
| 61 | # pnnx inference |
| 62 | import test_nn_MaxPool3d_pnnx |
| 63 | b = test_nn_MaxPool3d_pnnx.test_inference() |
| 64 | |
| 65 | for a0, b0 in zip(a, b): |
| 66 | if not torch.equal(a0, b0): |
| 67 | return False |
| 68 | return True |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | if test(): |
| 72 | exit(0) |
| 73 | else: |
| 74 | exit(1) |