| 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.pool_0 = nn.AvgPool3d(kernel_size=3) |
| 24 | self.pool_1 = nn.AvgPool3d(kernel_size=4, stride=2, padding=2) |
| 25 | self.pool_2 = nn.AvgPool3d(kernel_size=(1,2,3), stride=1, padding=(0,1,1), ceil_mode=False, count_include_pad=True) |
| 26 | self.pool_3 = nn.AvgPool3d(kernel_size=(3,4,5), stride=(1,2,2), padding=(1,1,2), ceil_mode=True, count_include_pad=False) |
| 27 | self.pool_4 = nn.AvgPool3d(kernel_size=(5,4,3), stride=(2,1,1), padding=1, ceil_mode=False, count_include_pad=True) |
| 28 | self.pool_5 = nn.AvgPool3d(kernel_size=2, stride=1, padding=0, ceil_mode=True, count_include_pad=True) |
| 29 | self.pool_6 = nn.AvgPool3d(kernel_size=(5,4,4), stride=1, padding=2, ceil_mode=False, count_include_pad=False, divisor_override=77) |
| 30 | |
| 31 | def forward(self, x, y): |
| 32 | x = self.pool_0(x) |
| 33 | x = self.pool_1(x) |
| 34 | x = self.pool_2(x) |
| 35 | x = self.pool_3(x) |
| 36 | x = self.pool_4(x) |
| 37 | x = self.pool_5(x) |
| 38 | x = self.pool_6(x) |
| 39 | |
| 40 | y = self.pool_0(y) |
| 41 | y = self.pool_1(y) |
| 42 | y = self.pool_2(y) |
| 43 | y = self.pool_3(y) |
| 44 | y = self.pool_4(y) |
| 45 | y = self.pool_5(y) |
| 46 | y = self.pool_6(y) |
| 47 | return x, y |
| 48 | |
| 49 | def test(): |
| 50 | net = Model() |
| 51 | net.eval() |
| 52 | |
| 53 | torch.manual_seed(0) |
| 54 | x = torch.rand(1, 12, 96, 128, 128) |
| 55 | y = torch.rand(12, 96, 128, 128) |
| 56 | |
| 57 | a = net(x, y) |
| 58 | |
| 59 | # export torchscript |
| 60 | mod = torch.jit.trace(net, (x, y)) |
| 61 | mod.save("test_nn_AvgPool3d.pt") |
| 62 | |
| 63 | # torchscript to pnnx |
| 64 | import os |
| 65 | os.system("../src/pnnx test_nn_AvgPool3d.pt inputshape=[1,12,96,128,128],[12,96,128,128]") |
| 66 | |
| 67 | # pnnx inference |
| 68 | import test_nn_AvgPool3d_pnnx |
| 69 | b = test_nn_AvgPool3d_pnnx.test_inference() |
| 70 | |
| 71 | for a0, b0 in zip(a, b): |
| 72 | if not torch.equal(a0, b0): |
| 73 | return False |
| 74 | return True |
| 75 | |
| 76 | if __name__ == "__main__": |
| 77 | if test(): |
| 78 | exit(0) |
| 79 | else: |
| 80 | exit(1) |