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_batchnorm.cpp 2.9 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "testutil.h"
  4. static int test_batchnorm(const ncnn::Mat& a, float eps)
  5. {
  6. int channels;
  7. if (a.dims == 1) channels = a.w;
  8. if (a.dims == 2) channels = a.h;
  9. if (a.dims == 3 || a.dims == 4) channels = a.c;
  10. ncnn::ParamDict pd;
  11. pd.set(0, channels); // channels
  12. pd.set(1, eps); // eps
  13. std::vector<ncnn::Mat> weights(4);
  14. weights[0] = RandomMat(channels);
  15. weights[1] = RandomMat(channels);
  16. weights[2] = RandomMat(channels);
  17. weights[3] = RandomMat(channels);
  18. // var must be positive
  19. Randomize(weights[2], 0.001f, 2.f);
  20. int ret = test_layer("BatchNorm", pd, weights, a);
  21. if (ret != 0)
  22. {
  23. fprintf(stderr, "test_batchnorm failed a.dims=%d a=(%d %d %d %d) eps=%f\n", a.dims, a.w, a.h, a.d, a.c, eps);
  24. }
  25. return ret;
  26. }
  27. static int test_batchnorm_0()
  28. {
  29. return 0
  30. || test_batchnorm(RandomMat(5, 6, 7, 24), 0.f)
  31. || test_batchnorm(RandomMat(5, 6, 7, 24), 0.01f)
  32. || test_batchnorm(RandomMat(7, 8, 9, 12), 0.f)
  33. || test_batchnorm(RandomMat(7, 8, 9, 12), 0.001f)
  34. || test_batchnorm(RandomMat(3, 4, 5, 13), 0.f)
  35. || test_batchnorm(RandomMat(3, 4, 5, 13), 0.f)
  36. || test_batchnorm(RandomMat(3, 4, 6, 32), 0.f)
  37. || test_batchnorm(RandomMat(3, 4, 5, 32), 0.001f);
  38. }
  39. static int test_batchnorm_1()
  40. {
  41. return 0
  42. || test_batchnorm(RandomMat(5, 7, 24), 0.f)
  43. || test_batchnorm(RandomMat(5, 7, 24), 0.01f)
  44. || test_batchnorm(RandomMat(7, 9, 12), 0.f)
  45. || test_batchnorm(RandomMat(7, 9, 12), 0.001f)
  46. || test_batchnorm(RandomMat(3, 5, 13), 0.f)
  47. || test_batchnorm(RandomMat(3, 5, 13), 0.001f)
  48. || test_batchnorm(RandomMat(3, 5, 16), 0.001f)
  49. || test_batchnorm(RandomMat(3, 5, 32), 0.001f);
  50. }
  51. static int test_batchnorm_2()
  52. {
  53. return 0
  54. || test_batchnorm(RandomMat(15, 24), 0.f)
  55. || test_batchnorm(RandomMat(15, 24), 0.01f)
  56. || test_batchnorm(RandomMat(17, 12), 0.f)
  57. || test_batchnorm(RandomMat(17, 12), 0.001f)
  58. || test_batchnorm(RandomMat(19, 15), 0.f)
  59. || test_batchnorm(RandomMat(19, 15), 0.001f)
  60. || test_batchnorm(RandomMat(128, 16), 0.f)
  61. || test_batchnorm(RandomMat(16, 128), 0.001f);
  62. }
  63. static int test_batchnorm_3()
  64. {
  65. return 0
  66. || test_batchnorm(RandomMat(128), 0.f)
  67. || test_batchnorm(RandomMat(128), 0.001f)
  68. || test_batchnorm(RandomMat(124), 0.f)
  69. || test_batchnorm(RandomMat(124), 0.1f)
  70. || test_batchnorm(RandomMat(127), 0.f)
  71. || test_batchnorm(RandomMat(127), 0.1f);
  72. }
  73. int main()
  74. {
  75. SRAND(7767517);
  76. return 0
  77. || test_batchnorm_0()
  78. || test_batchnorm_1()
  79. || test_batchnorm_2()
  80. || test_batchnorm_3();
  81. }