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 3.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_batchnorm(const ncnn::Mat& a, float eps)
  16. {
  17. int channels;
  18. if (a.dims == 1) channels = a.w;
  19. if (a.dims == 2) channels = a.h;
  20. if (a.dims == 3 || a.dims == 4) channels = a.c;
  21. ncnn::ParamDict pd;
  22. pd.set(0, channels); // channels
  23. pd.set(1, eps); // eps
  24. std::vector<ncnn::Mat> weights(4);
  25. weights[0] = RandomMat(channels);
  26. weights[1] = RandomMat(channels);
  27. weights[2] = RandomMat(channels);
  28. weights[3] = RandomMat(channels);
  29. // var must be positive
  30. Randomize(weights[2], 0.001f, 2.f);
  31. int ret = test_layer("BatchNorm", pd, weights, a);
  32. if (ret != 0)
  33. {
  34. 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);
  35. }
  36. return ret;
  37. }
  38. static int test_batchnorm_0()
  39. {
  40. return 0
  41. || test_batchnorm(RandomMat(5, 6, 7, 24), 0.f)
  42. || test_batchnorm(RandomMat(5, 6, 7, 24), 0.01f)
  43. || test_batchnorm(RandomMat(7, 8, 9, 12), 0.f)
  44. || test_batchnorm(RandomMat(7, 8, 9, 12), 0.001f)
  45. || test_batchnorm(RandomMat(3, 4, 5, 13), 0.f)
  46. || test_batchnorm(RandomMat(3, 4, 5, 13), 0.f)
  47. || test_batchnorm(RandomMat(3, 4, 6, 32), 0.f)
  48. || test_batchnorm(RandomMat(3, 4, 5, 32), 0.001f);
  49. }
  50. static int test_batchnorm_1()
  51. {
  52. return 0
  53. || test_batchnorm(RandomMat(5, 7, 24), 0.f)
  54. || test_batchnorm(RandomMat(5, 7, 24), 0.01f)
  55. || test_batchnorm(RandomMat(7, 9, 12), 0.f)
  56. || test_batchnorm(RandomMat(7, 9, 12), 0.001f)
  57. || test_batchnorm(RandomMat(3, 5, 13), 0.f)
  58. || test_batchnorm(RandomMat(3, 5, 13), 0.001f)
  59. || test_batchnorm(RandomMat(3, 5, 16), 0.001f)
  60. || test_batchnorm(RandomMat(3, 5, 32), 0.001f);
  61. }
  62. static int test_batchnorm_2()
  63. {
  64. return 0
  65. || test_batchnorm(RandomMat(15, 24), 0.f)
  66. || test_batchnorm(RandomMat(15, 24), 0.01f)
  67. || test_batchnorm(RandomMat(17, 12), 0.f)
  68. || test_batchnorm(RandomMat(17, 12), 0.001f)
  69. || test_batchnorm(RandomMat(19, 15), 0.f)
  70. || test_batchnorm(RandomMat(19, 15), 0.001f)
  71. || test_batchnorm(RandomMat(128, 16), 0.f)
  72. || test_batchnorm(RandomMat(16, 128), 0.001f);
  73. }
  74. static int test_batchnorm_3()
  75. {
  76. return 0
  77. || test_batchnorm(RandomMat(128), 0.f)
  78. || test_batchnorm(RandomMat(128), 0.001f)
  79. || test_batchnorm(RandomMat(124), 0.f)
  80. || test_batchnorm(RandomMat(124), 0.1f)
  81. || test_batchnorm(RandomMat(127), 0.f)
  82. || test_batchnorm(RandomMat(127), 0.1f);
  83. }
  84. int main()
  85. {
  86. SRAND(7767517);
  87. return 0
  88. || test_batchnorm_0()
  89. || test_batchnorm_1()
  90. || test_batchnorm_2()
  91. || test_batchnorm_3();
  92. }