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_groupnorm.cpp 4.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 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. #include "testutil.h"
  15. static int test_groupnorm(const ncnn::Mat& a, int group, float eps, int affine)
  16. {
  17. int channels = a.c;
  18. if (a.dims == 1)
  19. {
  20. channels = a.w;
  21. }
  22. else if (a.dims == 2)
  23. {
  24. channels = a.h;
  25. }
  26. ncnn::ParamDict pd;
  27. pd.set(0, group);
  28. pd.set(1, channels);
  29. pd.set(2, eps);
  30. pd.set(3, affine);
  31. std::vector<ncnn::Mat> weights(2);
  32. weights[0] = RandomMat(channels);
  33. weights[1] = RandomMat(channels);
  34. int ret = test_layer("GroupNorm", pd, weights, a);
  35. if (ret != 0)
  36. {
  37. fprintf(stderr, "test_groupnorm failed a.dims=%d a=(%d %d %d) group=%d eps=%f\n", a.dims, a.w, a.h, a.c, group, eps);
  38. }
  39. return ret;
  40. }
  41. static int test_groupnorm_0()
  42. {
  43. return 0
  44. || test_groupnorm(RandomMat(3, 6, 4, 2), 1, 0.01f, 0)
  45. || test_groupnorm(RandomMat(2, 3, 3, 8), 2, 0.002f, 0)
  46. || test_groupnorm(RandomMat(3, 4, 5, 6), 3, 0.01f, 0)
  47. || test_groupnorm(RandomMat(4, 5, 6, 12), 4, 0.02f, 0)
  48. || test_groupnorm(RandomMat(5, 6, 7, 24), 2, 0.001f, 0)
  49. || test_groupnorm(RandomMat(2, 8, 9, 24), 3, 0.0001f, 0)
  50. || test_groupnorm(RandomMat(3, 6, 4, 2), 1, 0.01f, 1)
  51. || test_groupnorm(RandomMat(2, 3, 3, 8), 2, 0.002f, 1)
  52. || test_groupnorm(RandomMat(3, 4, 5, 6), 3, 0.01f, 1)
  53. || test_groupnorm(RandomMat(4, 5, 6, 12), 4, 0.02f, 1)
  54. || test_groupnorm(RandomMat(5, 6, 7, 24), 2, 0.001f, 1)
  55. || test_groupnorm(RandomMat(2, 8, 9, 24), 3, 0.0001f, 1);
  56. }
  57. static int test_groupnorm_1()
  58. {
  59. return 0
  60. || test_groupnorm(RandomMat(6, 4, 2), 1, 0.01f, 0)
  61. || test_groupnorm(RandomMat(3, 3, 8), 2, 0.002f, 0)
  62. || test_groupnorm(RandomMat(4, 5, 6), 3, 0.01f, 0)
  63. || test_groupnorm(RandomMat(5, 6, 12), 4, 0.02f, 0)
  64. || test_groupnorm(RandomMat(6, 7, 24), 2, 0.001f, 0)
  65. || test_groupnorm(RandomMat(8, 9, 24), 3, 0.0001f, 0)
  66. || test_groupnorm(RandomMat(6, 4, 2), 1, 0.01f, 1)
  67. || test_groupnorm(RandomMat(3, 3, 8), 2, 0.002f, 1)
  68. || test_groupnorm(RandomMat(4, 5, 6), 3, 0.01f, 1)
  69. || test_groupnorm(RandomMat(5, 6, 12), 4, 0.02f, 1)
  70. || test_groupnorm(RandomMat(6, 7, 24), 2, 0.001f, 1)
  71. || test_groupnorm(RandomMat(8, 9, 24), 3, 0.0001f, 1);
  72. }
  73. static int test_groupnorm_2()
  74. {
  75. return 0
  76. || test_groupnorm(RandomMat(24, 2), 1, 0.01f, 0)
  77. || test_groupnorm(RandomMat(23, 8), 2, 0.002f, 0)
  78. || test_groupnorm(RandomMat(25, 6), 3, 0.01f, 0)
  79. || test_groupnorm(RandomMat(26, 12), 4, 0.02f, 0)
  80. || test_groupnorm(RandomMat(27, 24), 2, 0.001f, 0)
  81. || test_groupnorm(RandomMat(29, 24), 3, 0.0001f, 0)
  82. || test_groupnorm(RandomMat(24, 2), 1, 0.01f, 1)
  83. || test_groupnorm(RandomMat(23, 8), 2, 0.002f, 1)
  84. || test_groupnorm(RandomMat(25, 6), 3, 0.01f, 1)
  85. || test_groupnorm(RandomMat(26, 12), 4, 0.02f, 1)
  86. || test_groupnorm(RandomMat(27, 24), 2, 0.001f, 1)
  87. || test_groupnorm(RandomMat(29, 24), 3, 0.0001f, 1);
  88. }
  89. static int test_groupnorm_3()
  90. {
  91. return 0
  92. || test_groupnorm(RandomMat(12), 1, 0.01f, 0)
  93. || test_groupnorm(RandomMat(18), 2, 0.002f, 0)
  94. || test_groupnorm(RandomMat(36), 3, 0.01f, 0)
  95. || test_groupnorm(RandomMat(212), 4, 0.02f, 0)
  96. || test_groupnorm(RandomMat(124), 2, 0.001f, 0)
  97. || test_groupnorm(RandomMat(324), 3, 0.0001f, 0)
  98. || test_groupnorm(RandomMat(12), 1, 0.01f, 1)
  99. || test_groupnorm(RandomMat(18), 2, 0.002f, 1)
  100. || test_groupnorm(RandomMat(36), 3, 0.01f, 1)
  101. || test_groupnorm(RandomMat(212), 4, 0.02f, 1)
  102. || test_groupnorm(RandomMat(124), 2, 0.001f, 1)
  103. || test_groupnorm(RandomMat(324), 3, 0.0001f, 1);
  104. }
  105. int main()
  106. {
  107. SRAND(7767517);
  108. return 0
  109. || test_groupnorm_0()
  110. || test_groupnorm_1()
  111. || test_groupnorm_2()
  112. || test_groupnorm_3();
  113. }