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_Tensor_slice.py 1.9 kB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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, y, z):
  21. x = x[:,:12,1:14:2]
  22. x = x[...,1:]
  23. x = x[:,:,:x.size(2)-1]
  24. y = y[0:,1:,5:,3:]
  25. y = y[:,:,1:13:2,:14]
  26. y = y[:1,:y.size(1):,:,:]
  27. z = z[4:]
  28. z = z[:2,:,:,:,2:-2:3]
  29. z = z[:,:,:,z.size(3)-3:,:]
  30. return x, y, z
  31. def test():
  32. net = Model()
  33. net.eval()
  34. torch.manual_seed(0)
  35. x = torch.rand(1, 13, 26)
  36. y = torch.rand(1, 15, 19, 21)
  37. z = torch.rand(14, 18, 15, 19, 20)
  38. a = net(x, y, z)
  39. # export torchscript
  40. mod = torch.jit.trace(net, (x, y, z))
  41. mod.save("test_Tensor_slice.pt")
  42. # torchscript to pnnx
  43. import os
  44. os.system("../src/pnnx test_Tensor_slice.pt inputshape=[1,13,26],[1,15,19,21],[14,18,15,19,20]")
  45. # pnnx inference
  46. import test_Tensor_slice_pnnx
  47. b = test_Tensor_slice_pnnx.test_inference()
  48. for a0, b0 in zip(a, b):
  49. if not torch.equal(a0, b0):
  50. return False
  51. return True
  52. if __name__ == "__main__":
  53. if test():
  54. exit(0)
  55. else:
  56. exit(1)