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_normalize.py 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 = F.normalize(x)
  22. x = F.normalize(x, eps=1e-3)
  23. y = F.normalize(y, p=1, dim=1)
  24. y = F.normalize(y, dim=2)
  25. z = F.normalize(z)
  26. z = F.normalize(z, dim=2, eps=1e-4)
  27. return x, y, z
  28. def test():
  29. net = Model()
  30. net.eval()
  31. torch.manual_seed(0)
  32. x = torch.rand(1, 24, 64)
  33. y = torch.rand(1, 12, 24, 64)
  34. z = torch.rand(1, 12, 16, 24, 64)
  35. a0, a1, a2 = net(x, y, z)
  36. # export torchscript
  37. mod = torch.jit.trace(net, (x, y, z))
  38. mod.save("test_F_normalize.pt")
  39. # torchscript to pnnx
  40. import os
  41. os.system("../src/pnnx test_F_normalize.pt inputshape=[1,24,64],[1,12,24,64],[1,12,16,24,64]")
  42. # pnnx inference
  43. import test_F_normalize_pnnx
  44. b0, b1, b2 = test_F_normalize_pnnx.test_inference()
  45. return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2)
  46. if __name__ == "__main__":
  47. if test():
  48. exit(0)
  49. else:
  50. exit(1)