| 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 | def forward(self, x, xg1, xg2, y, yg1, yg2): |
| 24 | # norm to -1 ~ 1 |
| 25 | xg1 = xg1 * 2 - 1 |
| 26 | xg2 = xg2 * 2 - 1 |
| 27 | yg1 = yg1 * 2 - 1 |
| 28 | yg2 = yg2 * 2 - 1 |
| 29 | |
| 30 | x = F.grid_sample(x, xg1, mode='bilinear', padding_mode='zeros', align_corners=False) |
| 31 | x = F.grid_sample(x, xg2, mode='bilinear', padding_mode='border', align_corners=False) |
| 32 | x = F.grid_sample(x, xg1, mode='bilinear', padding_mode='reflection', align_corners=False) |
| 33 | x = F.grid_sample(x, xg2, mode='nearest', padding_mode='zeros', align_corners=False) |
| 34 | x = F.grid_sample(x, xg1, mode='nearest', padding_mode='border', align_corners=False) |
| 35 | x = F.grid_sample(x, xg2, mode='nearest', padding_mode='reflection', align_corners=False) |
| 36 | x = F.grid_sample(x, xg1, mode='bicubic', padding_mode='zeros', align_corners=False) |
| 37 | x = F.grid_sample(x, xg2, mode='bicubic', padding_mode='border', align_corners=False) |
| 38 | x = F.grid_sample(x, xg1, mode='bicubic', padding_mode='reflection', align_corners=False) |
| 39 | x = F.grid_sample(x, xg2, mode='bilinear', padding_mode='zeros', align_corners=True) |
| 40 | x = F.grid_sample(x, xg1, mode='bilinear', padding_mode='border', align_corners=True) |
| 41 | x = F.grid_sample(x, xg2, mode='bilinear', padding_mode='reflection', align_corners=True) |
| 42 | x = F.grid_sample(x, xg1, mode='nearest', padding_mode='zeros', align_corners=True) |
| 43 | x = F.grid_sample(x, xg2, mode='nearest', padding_mode='border', align_corners=True) |
| 44 | x = F.grid_sample(x, xg1, mode='nearest', padding_mode='reflection', align_corners=True) |
| 45 | x = F.grid_sample(x, xg2, mode='bicubic', padding_mode='zeros', align_corners=True) |
| 46 | x = F.grid_sample(x, xg1, mode='bicubic', padding_mode='border', align_corners=True) |
| 47 | x = F.grid_sample(x, xg2, mode='bicubic', padding_mode='reflection', align_corners=True) |
| 48 | |
| 49 | y = F.grid_sample(y, yg1, mode='bilinear', padding_mode='zeros', align_corners=False) |
| 50 | y = F.grid_sample(y, yg2, mode='bilinear', padding_mode='border', align_corners=False) |
| 51 | y = F.grid_sample(y, yg1, mode='bilinear', padding_mode='reflection', align_corners=False) |
| 52 | y = F.grid_sample(y, yg2, mode='nearest', padding_mode='zeros', align_corners=False) |
| 53 | y = F.grid_sample(y, yg1, mode='nearest', padding_mode='border', align_corners=False) |
| 54 | y = F.grid_sample(y, yg2, mode='nearest', padding_mode='reflection', align_corners=False) |
| 55 | y = F.grid_sample(y, yg1, mode='bilinear', padding_mode='zeros', align_corners=True) |
| 56 | y = F.grid_sample(y, yg2, mode='bilinear', padding_mode='border', align_corners=True) |
| 57 | y = F.grid_sample(y, yg1, mode='bilinear', padding_mode='reflection', align_corners=True) |
| 58 | y = F.grid_sample(y, yg2, mode='nearest', padding_mode='zeros', align_corners=True) |
| 59 | y = F.grid_sample(y, yg1, mode='nearest', padding_mode='border', align_corners=True) |
| 60 | y = F.grid_sample(y, yg2, mode='nearest', padding_mode='reflection', align_corners=True) |
| 61 | |
| 62 | return x, y |
| 63 | |
| 64 | def test(): |
| 65 | net = Model() |
| 66 | net.eval() |
| 67 | |
| 68 | torch.manual_seed(0) |
| 69 | x = torch.rand(1, 3, 12, 16) |
| 70 | xg1 = torch.rand(1, 21, 27, 2) |
| 71 | xg2 = torch.rand(1, 12, 16, 2) |
| 72 | y = torch.rand(1, 5, 10, 12, 16) |
| 73 | yg1 = torch.rand(1, 10, 21, 27, 3) |
| 74 | yg2 = torch.rand(1, 10, 12, 16, 3) |
| 75 | |
| 76 | a0, a1 = net(x, xg1, xg2, y, yg1, yg2) |
| 77 | |
| 78 | # export torchscript |
| 79 | mod = torch.jit.trace(net, (x, xg1, xg2, y, yg1, yg2)) |
| 80 | mod.save("test_F_grid_sample.pt") |
| 81 | |
| 82 | # torchscript to pnnx |
| 83 | import os |
| 84 | os.system("../src/pnnx test_F_grid_sample.pt inputshape=[1,3,12,16],[1,21,27,2],[1,12,16,2],[1,5,10,12,16],[1,10,21,27,3],[1,10,12,16,3]") |
| 85 | |
| 86 | # pnnx inference |
| 87 | import test_F_grid_sample_pnnx |
| 88 | b0, b1 = test_F_grid_sample_pnnx.test_inference() |
| 89 | |
| 90 | return torch.equal(a0, b0) and torch.equal(a1, b1) |
| 91 | |
| 92 | if __name__ == "__main__": |
| 93 | if test(): |
| 94 | exit(0) |
| 95 | else: |
| 96 | exit(1) |