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.8 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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) 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) eps=%f\n", a.dims, a.w, a.h, a.c, eps);
  36. }
  37. return ret;
  38. }
  39. static int test_batchnorm_0()
  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. }
  49. static int test_batchnorm_1()
  50. {
  51. return 0
  52. || test_batchnorm(RandomMat(15, 24), 0.f)
  53. || test_batchnorm(RandomMat(15, 24), 0.01f)
  54. || test_batchnorm(RandomMat(17, 12), 0.f)
  55. || test_batchnorm(RandomMat(17, 12), 0.001f)
  56. || test_batchnorm(RandomMat(19, 15), 0.f)
  57. || test_batchnorm(RandomMat(19, 15), 0.001f);
  58. }
  59. static int test_batchnorm_2()
  60. {
  61. return 0
  62. || test_batchnorm(RandomMat(128), 0.f)
  63. || test_batchnorm(RandomMat(128), 0.001f)
  64. || test_batchnorm(RandomMat(124), 0.f)
  65. || test_batchnorm(RandomMat(124), 0.1f)
  66. || test_batchnorm(RandomMat(127), 0.f)
  67. || test_batchnorm(RandomMat(127), 0.1f);
  68. }
  69. int main()
  70. {
  71. SRAND(7767517);
  72. return 0
  73. || test_batchnorm_0()
  74. || test_batchnorm_1()
  75. || test_batchnorm_2();
  76. }