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