| 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 | def forward(self, x, y, z, w): |
| 25 | |
| 26 | if version.parse(torch.__version__) < version.parse('1.12'): |
| 27 | x0 = F.interpolate(x, size=60) |
| 28 | x0 = F.interpolate(x0, scale_factor=2, mode='nearest') |
| 29 | x1 = F.interpolate(x, size=(40), mode='nearest') |
| 30 | x1 = F.interpolate(x1, scale_factor=(4), mode='nearest') |
| 31 | x2 = F.interpolate(x, size=60, mode='linear') |
| 32 | x2 = F.interpolate(x2, scale_factor=2, mode='linear') |
| 33 | |
| 34 | y0 = F.interpolate(y, size=60) |
| 35 | y0 = F.interpolate(y0, scale_factor=2, mode='nearest') |
| 36 | y1 = F.interpolate(y, size=(40,40), mode='nearest') |
| 37 | y1 = F.interpolate(y1, scale_factor=(4,4), mode='nearest') |
| 38 | y2 = F.interpolate(y, size=(60,50), mode='nearest') |
| 39 | y2 = F.interpolate(y2, scale_factor=(2,3), mode='nearest') |
| 40 | y3 = F.interpolate(y, size=60, mode='bilinear') |
| 41 | y3 = F.interpolate(y3, scale_factor=2, mode='bilinear') |
| 42 | |
| 43 | z0 = F.interpolate(z, size=60) |
| 44 | z0 = F.interpolate(z0, scale_factor=2, mode='nearest') |
| 45 | z1 = F.interpolate(z, size=(40,40,40), mode='nearest') |
| 46 | z1 = F.interpolate(z1, scale_factor=(4,4,4), mode='nearest') |
| 47 | z2 = F.interpolate(z, size=(60,50,40), mode='nearest') |
| 48 | z2 = F.interpolate(z2, scale_factor=(2,3,4), mode='nearest') |
| 49 | z3 = F.interpolate(z, size=60, mode='trilinear') |
| 50 | z3 = F.interpolate(z3, scale_factor=2, mode='trilinear') |
| 51 | |
| 52 | w = F.interpolate(w, scale_factor=(2.976744,2.976744), mode='nearest', recompute_scale_factor=False) |
| 53 | return x0, x1, x2, y0, y1, y2, y3, z0, z1, z2, z3, w |
| 54 | else: |
| 55 | x = F.interpolate(x, size=16) |
| 56 | x = F.interpolate(x, scale_factor=2, mode='nearest') |
| 57 | x = F.interpolate(x, size=(20), mode='nearest') |
| 58 | x = F.interpolate(x, scale_factor=(4), mode='nearest') |
| 59 | x = F.interpolate(x, size=16, mode='linear') |
| 60 | x = F.interpolate(x, scale_factor=2, mode='linear') |
| 61 | x = F.interpolate(x, size=(24), mode='linear', align_corners=True) |
| 62 | x = F.interpolate(x, scale_factor=(3), mode='linear', align_corners=True) |
| 63 | |
| 64 | x = F.interpolate(x, scale_factor=1.5, mode='nearest', recompute_scale_factor=True) |
| 65 | x = F.interpolate(x, scale_factor=1.2, mode='linear', align_corners=False, recompute_scale_factor=True) |
| 66 | x = F.interpolate(x, scale_factor=0.8, mode='linear', align_corners=True, recompute_scale_factor=True) |
| 67 | |
| 68 | y = F.interpolate(y, size=16) |
| 69 | y = F.interpolate(y, scale_factor=2, mode='nearest') |
| 70 | y = F.interpolate(y, size=(20,20), mode='nearest') |
| 71 | y = F.interpolate(y, scale_factor=(4,4), mode='nearest') |
| 72 | y = F.interpolate(y, size=(16,24), mode='nearest') |
| 73 | y = F.interpolate(y, scale_factor=(2,3), mode='nearest') |
| 74 | y = F.interpolate(y, size=16, mode='bilinear') |
| 75 | y = F.interpolate(y, scale_factor=2, mode='bilinear') |
| 76 | y = F.interpolate(y, size=(20,20), mode='bilinear', align_corners=False) |
| 77 | y = F.interpolate(y, scale_factor=(4,4), mode='bilinear', align_corners=False) |
| 78 | y = F.interpolate(y, size=(16,24), mode='bilinear', align_corners=True) |
| 79 | y = F.interpolate(y, scale_factor=(2,3), mode='bilinear', align_corners=True) |
| 80 | |
| 81 | y = F.interpolate(y, size=16, mode='bicubic') |
| 82 | y = F.interpolate(y, scale_factor=2, mode='bicubic') |
| 83 | y = F.interpolate(y, size=(20,20), mode='bicubic', align_corners=False) |
| 84 | y = F.interpolate(y, scale_factor=(4,4), mode='bicubic', align_corners=False) |
| 85 | y = F.interpolate(y, size=(16,24), mode='bicubic', align_corners=True) |
| 86 | y = F.interpolate(y, scale_factor=(2,3), mode='bicubic', align_corners=True) |
| 87 | |
| 88 | y = F.interpolate(y, scale_factor=(1.6,2), mode='nearest', recompute_scale_factor=True) |
| 89 | y = F.interpolate(y, scale_factor=(2,1.2), mode='bilinear', align_corners=False, recompute_scale_factor=True) |
| 90 | y = F.interpolate(y, scale_factor=(0.5,0.4), mode='bilinear', align_corners=True, recompute_scale_factor=True) |
| 91 | y = F.interpolate(y, scale_factor=(0.8,0.9), mode='bicubic', align_corners=False, recompute_scale_factor=True) |
| 92 | y = F.interpolate(y, scale_factor=(1.1,0.5), mode='bicubic', align_corners=True, recompute_scale_factor=True) |
| 93 | |
| 94 | z = F.interpolate(z, size=16) |
| 95 | z = F.interpolate(z, scale_factor=2, mode='nearest') |
| 96 | z = F.interpolate(z, size=(20,20,20), mode='nearest') |
| 97 | z = F.interpolate(z, scale_factor=(4,4,4), mode='nearest') |
| 98 | z = F.interpolate(z, size=(16,24,20), mode='nearest') |
| 99 | z = F.interpolate(z, scale_factor=(2,3,4), mode='nearest') |
| 100 | z = F.interpolate(z, size=16, mode='trilinear') |
| 101 | z = F.interpolate(z, scale_factor=2, mode='trilinear') |
| 102 | z = F.interpolate(z, size=(20,20,20), mode='trilinear', align_corners=False) |
| 103 | z = F.interpolate(z, scale_factor=(4,4,4), mode='trilinear', align_corners=False) |
| 104 | z = F.interpolate(z, size=(16,24,20), mode='trilinear', align_corners=True) |
| 105 | z = F.interpolate(z, scale_factor=(2,3,4), mode='trilinear', align_corners=True) |
| 106 | |
| 107 | z = F.interpolate(z, scale_factor=(1.5,2.5,2), mode='nearest', recompute_scale_factor=True) |
| 108 | z = F.interpolate(z, scale_factor=(0.7,0.5,1), mode='trilinear', align_corners=False, recompute_scale_factor=True) |
| 109 | z = F.interpolate(z, scale_factor=(0.9,0.8,1.2), mode='trilinear', align_corners=True, recompute_scale_factor=True) |
| 110 | |
| 111 | w = F.interpolate(w, scale_factor=(2.976744,2.976744), mode='nearest', recompute_scale_factor=False) |
| 112 | return x, y, z, w |
| 113 | |
| 114 | def test(): |
| 115 | net = Model() |
| 116 | net.eval() |
| 117 | |
| 118 | torch.manual_seed(0) |
| 119 | x = torch.rand(1, 3, 32) |
| 120 | y = torch.rand(1, 3, 32, 32) |
| 121 | z = torch.rand(1, 3, 32, 32, 32) |
| 122 | w = torch.rand(1, 8, 86, 86) |
| 123 | |
| 124 | a = net(x, y, z, w) |
| 125 | |
| 126 | # export onnx |
| 127 | torch.onnx.export(net, (x, y, z, w), "test_F_interpolate.onnx") |
| 128 | |
| 129 | # onnx to pnnx |
| 130 | import os |
| 131 | os.system("../../src/pnnx test_F_interpolate.onnx inputshape=[1,3,32],[1,3,32,32],[1,3,32,32,32],[1,8,86,86]") |
| 132 | |
| 133 | # pnnx inference |
| 134 | import test_F_interpolate_pnnx |
| 135 | b = test_F_interpolate_pnnx.test_inference() |
| 136 | |
| 137 | for a0, b0 in zip(a, b): |
| 138 | if not torch.equal(a0, b0): |
| 139 | return False |
| 140 | return True |
| 141 | |
| 142 | if __name__ == "__main__": |
| 143 | if test(): |
| 144 | exit(0) |
| 145 | else: |
| 146 | exit(1) |