You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_F_grid_sample.py 4.8 kB

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