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_instance_norm.py 3.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. self.m3 = torch.rand(12)
  21. self.v3 = torch.rand(12)
  22. self.w3 = nn.Parameter(torch.rand(12))
  23. self.b3 = nn.Parameter(torch.rand(12))
  24. self.m4 = torch.rand(3)
  25. self.v4 = torch.rand(3)
  26. self.w4 = nn.Parameter(torch.rand(3))
  27. self.b4 = nn.Parameter(torch.rand(3))
  28. self.m5 = torch.rand(10)
  29. self.v5 = torch.rand(10)
  30. self.w5 = nn.Parameter(torch.rand(10))
  31. self.b5 = nn.Parameter(torch.rand(10))
  32. def forward(self, x, y, z, m0, v0, w0, b0, m1, v1, w1, b1, m2, v2, w2, b2):
  33. x = F.instance_norm(x, m0, v0, w0, b0)
  34. x = F.instance_norm(x, m0, v0, None, None)
  35. x = F.instance_norm(x, self.m3, self.v3, self.w3, self.b3)
  36. y = F.instance_norm(y, m1, v1, w1, b1, eps=1e-3)
  37. y = F.instance_norm(y, m1, v1, None, None)
  38. y = F.instance_norm(y, self.m4, self.v4, self.w4, self.b4)
  39. z = F.instance_norm(z, m2, v2, w2, b2)
  40. z = F.instance_norm(z, m2, v2, None, None, eps=1e-2)
  41. z = F.instance_norm(z, self.m5, self.v5, self.w5, self.b5)
  42. return x, y, z
  43. def test():
  44. net = Model()
  45. net.eval()
  46. torch.manual_seed(0)
  47. x = torch.rand(1, 12, 24)
  48. y = torch.rand(2, 3, 12, 16)
  49. z = torch.rand(1, 10, 12, 16, 24)
  50. m0 = torch.rand(12)
  51. v0 = torch.rand(12)
  52. w0 = torch.rand(12)
  53. b0 = torch.rand(12)
  54. m1 = torch.rand(3)
  55. v1 = torch.rand(3)
  56. w1 = torch.rand(3)
  57. b1 = torch.rand(3)
  58. m2 = torch.rand(10)
  59. v2 = torch.rand(10)
  60. w2 = torch.rand(10)
  61. b2 = torch.rand(10)
  62. a0, a1, a2 = net(x, y, z, m0, v0, w0, b0, m1, v1, w1, b1, m2, v2, w2, b2)
  63. # export torchscript
  64. mod = torch.jit.trace(net, (x, y, z, m0, v0, w0, b0, m1, v1, w1, b1, m2, v2, w2, b2))
  65. mod.save("test_F_instance_norm.pt")
  66. # torchscript to pnnx
  67. import os
  68. os.system("../src/pnnx test_F_instance_norm.pt inputshape=[1,12,24],[2,3,12,16],[1,10,12,16,24],[12],[12],[12],[12],[3],[3],[3],[3],[10],[10],[10],[10]")
  69. # pnnx inference
  70. import test_F_instance_norm_pnnx
  71. b0, b1, b2 = test_F_instance_norm_pnnx.test_inference()
  72. return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2)
  73. if __name__ == "__main__":
  74. if test():
  75. exit(0)
  76. else:
  77. exit(1)