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