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_pooling.cpp 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/pooling.h"
  15. #include "testutil.h"
  16. static int test_pooling(int w, int h, int c, int pooling_type, int kernel, int stride, int pad, int global_pooling, int pad_mode, int avgpool_count_include_pad)
  17. {
  18. ncnn::Mat a = RandomMat(w, h, c);
  19. ncnn::ParamDict pd;
  20. pd.set(0, pooling_type); // pooling_type
  21. pd.set(1, kernel); // kernel_w
  22. pd.set(2, stride); // stride_w
  23. pd.set(3, pad); // pad_w
  24. pd.set(4, global_pooling); // global_pooling
  25. pd.set(5, pad_mode); // pad_mode
  26. pd.set(6, avgpool_count_include_pad); // avgpool_count_include_pad
  27. std::vector<ncnn::Mat> weights(0);
  28. int ret = test_layer<ncnn::Pooling>("Pooling", pd, weights, a);
  29. if (ret != 0)
  30. {
  31. fprintf(stderr, "test_pooling failed w=%d h=%d c=%d pooling_type=%d kernel=%d stride=%d pad=%d global_pooling=%d pad_mode=%d avgpool_count_include_pad=%d\n", w, h, c, pooling_type, kernel, stride, pad, global_pooling, pad_mode, avgpool_count_include_pad);
  32. }
  33. return ret;
  34. }
  35. static int test_pooling_0()
  36. {
  37. static const int ksp[11][3] = {
  38. {2, 1, 0},
  39. {2, 2, 0},
  40. {3, 1, 0},
  41. {3, 2, 1},
  42. {4, 1, 0},
  43. {4, 2, 1},
  44. {5, 1, 0},
  45. {5, 2, 2},
  46. {7, 1, 0},
  47. {7, 2, 1},
  48. {7, 3, 2},
  49. };
  50. for (int i = 0; i < 11; i++)
  51. {
  52. int ret = 0
  53. || test_pooling(9, 7, 1, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0)
  54. || test_pooling(9, 7, 2, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0)
  55. || test_pooling(9, 7, 3, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 0)
  56. || test_pooling(9, 7, 4, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 0)
  57. || test_pooling(9, 7, 7, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0)
  58. || test_pooling(9, 7, 8, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0)
  59. || test_pooling(9, 7, 15, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 2, 0)
  60. || test_pooling(9, 7, 16, 0, ksp[i][0], ksp[i][1], ksp[i][2], 0, 3, 0);
  61. if (ret != 0)
  62. return -1;
  63. }
  64. return 0;
  65. }
  66. static int test_pooling_1()
  67. {
  68. static const int ksp[11][3] = {
  69. {2, 1, 0},
  70. {2, 2, 0},
  71. {3, 1, 0},
  72. {3, 2, 1},
  73. {4, 1, 0},
  74. {4, 2, 1},
  75. {5, 1, 0},
  76. {5, 2, 2},
  77. {7, 1, 0},
  78. {7, 2, 1},
  79. {7, 3, 2},
  80. };
  81. for (int i = 0; i < 11; i++)
  82. {
  83. int ret = 0
  84. || test_pooling(9, 7, 1, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0)
  85. || test_pooling(9, 7, 2, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0)
  86. || test_pooling(9, 7, 3, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 1)
  87. || test_pooling(9, 7, 4, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0)
  88. || test_pooling(9, 7, 7, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0)
  89. || test_pooling(9, 7, 8, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 1)
  90. || test_pooling(9, 7, 15, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 0, 0)
  91. || test_pooling(9, 7, 16, 1, ksp[i][0], ksp[i][1], ksp[i][2], 0, 1, 0);
  92. if (ret != 0)
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. static int test_pooling_2()
  98. {
  99. return 0
  100. || test_pooling(2, 5, 1, 0, 1, 1, 0, 1, 0, 0)
  101. || test_pooling(5, 2, 1, 1, 1, 1, 0, 1, 0, 0)
  102. || test_pooling(3, 6, 3, 0, 1, 1, 0, 1, 0, 0)
  103. || test_pooling(6, 3, 3, 1, 1, 1, 0, 1, 0, 0)
  104. || test_pooling(4, 4, 4, 0, 1, 1, 0, 1, 0, 0)
  105. || test_pooling(6, 4, 4, 1, 1, 1, 0, 1, 0, 0)
  106. || test_pooling(8, 7, 8, 0, 1, 1, 0, 1, 0, 0)
  107. || test_pooling(7, 8, 8, 1, 1, 1, 0, 1, 0, 0)
  108. || test_pooling(11, 13, 16, 0, 1, 1, 0, 1, 0, 0)
  109. || test_pooling(13, 11, 16, 1, 1, 1, 0, 1, 0, 0)
  110. || test_pooling(48, 48, 4, 0, 2, 2, 0, 0, 0, 0)
  111. || test_pooling(48, 48, 15, 0, 2, 2, 1, 0, 0, 0);
  112. }
  113. int main()
  114. {
  115. SRAND(7767517);
  116. return 0
  117. || test_pooling_0()
  118. || test_pooling_1()
  119. || test_pooling_2();
  120. }