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_squeeze.cpp 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2021 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/squeeze.h"
  15. #include "testutil.h"
  16. static int test_squeeze(const ncnn::Mat& a, int squeeze_w, int squeeze_h, int squeeze_d, int squeeze_c)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, squeeze_w);
  20. pd.set(1, squeeze_h);
  21. pd.set(11, squeeze_d);
  22. pd.set(2, squeeze_c);
  23. std::vector<ncnn::Mat> weights(0);
  24. int ret = test_layer<ncnn::Squeeze>("Squeeze", pd, weights, a);
  25. if (ret != 0)
  26. {
  27. fprintf(stderr, "test_squeeze failed a.dims=%d a=(%d %d %d %d) squeeze_w=%d squeeze_h=%d squeeze_d=%d squeeze_c=%d\n", a.dims, a.w, a.h, a.d, a.c, squeeze_w, squeeze_h, squeeze_d, squeeze_c);
  28. }
  29. return ret;
  30. }
  31. static ncnn::Mat IntArrayMat(int a0)
  32. {
  33. ncnn::Mat m(1);
  34. int* p = m;
  35. p[0] = a0;
  36. return m;
  37. }
  38. static ncnn::Mat IntArrayMat(int a0, int a1)
  39. {
  40. ncnn::Mat m(2);
  41. int* p = m;
  42. p[0] = a0;
  43. p[1] = a1;
  44. return m;
  45. }
  46. static ncnn::Mat IntArrayMat(int a0, int a1, int a2)
  47. {
  48. ncnn::Mat m(3);
  49. int* p = m;
  50. p[0] = a0;
  51. p[1] = a1;
  52. p[2] = a2;
  53. return m;
  54. }
  55. static ncnn::Mat IntArrayMat(int a0, int a1, int a2, int a3)
  56. {
  57. ncnn::Mat m(4);
  58. int* p = m;
  59. p[0] = a0;
  60. p[1] = a1;
  61. p[2] = a2;
  62. p[3] = a3;
  63. return m;
  64. }
  65. static void print_int_array(const ncnn::Mat& a)
  66. {
  67. const int* pa = a;
  68. fprintf(stderr, "[");
  69. for (int i = 0; i < a.w; i++)
  70. {
  71. fprintf(stderr, " %d", pa[i]);
  72. }
  73. fprintf(stderr, " ]");
  74. }
  75. static int test_squeeze_axes(const ncnn::Mat& a, const ncnn::Mat& axes)
  76. {
  77. ncnn::ParamDict pd;
  78. pd.set(3, axes);
  79. std::vector<ncnn::Mat> weights(0);
  80. int ret = test_layer<ncnn::Squeeze>("Squeeze", pd, weights, a);
  81. if (ret != 0)
  82. {
  83. fprintf(stderr, "test_squeeze_axes failed a.dims=%d a=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c);
  84. fprintf(stderr, " axes=");
  85. print_int_array(axes);
  86. fprintf(stderr, "\n");
  87. }
  88. return ret;
  89. }
  90. static int test_squeeze_all_params(const ncnn::Mat& a)
  91. {
  92. return 0
  93. || test_squeeze(a, 0, 0, 0, 0)
  94. || test_squeeze(a, 0, 0, 0, 1)
  95. || test_squeeze(a, 0, 0, 1, 0)
  96. || test_squeeze(a, 0, 0, 1, 1)
  97. || test_squeeze(a, 0, 1, 0, 0)
  98. || test_squeeze(a, 0, 1, 0, 1)
  99. || test_squeeze(a, 0, 1, 1, 0)
  100. || test_squeeze(a, 0, 1, 1, 1)
  101. || test_squeeze(a, 1, 0, 0, 0)
  102. || test_squeeze(a, 1, 0, 0, 1)
  103. || test_squeeze(a, 1, 0, 1, 0)
  104. || test_squeeze(a, 1, 0, 1, 1)
  105. || test_squeeze(a, 1, 1, 0, 0)
  106. || test_squeeze(a, 1, 1, 0, 1)
  107. || test_squeeze(a, 1, 1, 1, 0)
  108. || test_squeeze(a, 1, 1, 1, 1)
  109. || test_squeeze_axes(a, IntArrayMat(0))
  110. || test_squeeze_axes(a, IntArrayMat(1))
  111. || test_squeeze_axes(a, IntArrayMat(2))
  112. || test_squeeze_axes(a, IntArrayMat(3))
  113. || test_squeeze_axes(a, IntArrayMat(0, 1))
  114. || test_squeeze_axes(a, IntArrayMat(0, 2))
  115. || test_squeeze_axes(a, IntArrayMat(0, 3))
  116. || test_squeeze_axes(a, IntArrayMat(1, 2))
  117. || test_squeeze_axes(a, IntArrayMat(1, 3))
  118. || test_squeeze_axes(a, IntArrayMat(2, 3))
  119. || test_squeeze_axes(a, IntArrayMat(0, 1, 2))
  120. || test_squeeze_axes(a, IntArrayMat(0, 1, 3))
  121. || test_squeeze_axes(a, IntArrayMat(0, 2, 3))
  122. || test_squeeze_axes(a, IntArrayMat(1, 2, 3))
  123. || test_squeeze_axes(a, IntArrayMat(0, 1, 2, 3));
  124. }
  125. static int test_squeeze_0()
  126. {
  127. return 0
  128. || test_squeeze_all_params(RandomMat(4, 5, 7, 16))
  129. || test_squeeze_all_params(RandomMat(4, 5, 1, 15))
  130. || test_squeeze_all_params(RandomMat(4, 1, 7, 12))
  131. || test_squeeze_all_params(RandomMat(1, 5, 7, 16))
  132. || test_squeeze_all_params(RandomMat(1, 5, 1, 15))
  133. || test_squeeze_all_params(RandomMat(1, 1, 7, 12))
  134. || test_squeeze_all_params(RandomMat(6, 1, 1, 16))
  135. || test_squeeze_all_params(RandomMat(1, 1, 1, 15))
  136. || test_squeeze_all_params(RandomMat(4, 5, 7, 1))
  137. || test_squeeze_all_params(RandomMat(4, 5, 1, 1))
  138. || test_squeeze_all_params(RandomMat(4, 1, 7, 1))
  139. || test_squeeze_all_params(RandomMat(1, 5, 7, 1))
  140. || test_squeeze_all_params(RandomMat(1, 5, 1, 1))
  141. || test_squeeze_all_params(RandomMat(1, 1, 7, 1))
  142. || test_squeeze_all_params(RandomMat(1, 1, 1, 1));
  143. }
  144. static int test_squeeze_1()
  145. {
  146. return 0
  147. || test_squeeze_all_params(RandomMat(3, 12, 16))
  148. || test_squeeze_all_params(RandomMat(3, 1, 16))
  149. || test_squeeze_all_params(RandomMat(1, 33, 15))
  150. || test_squeeze_all_params(RandomMat(1, 14, 1))
  151. || test_squeeze_all_params(RandomMat(12, 13, 1))
  152. || test_squeeze_all_params(RandomMat(1, 1, 1));
  153. }
  154. static int test_squeeze_2()
  155. {
  156. return 0
  157. || test_squeeze_all_params(RandomMat(14, 16))
  158. || test_squeeze_all_params(RandomMat(1, 14))
  159. || test_squeeze_all_params(RandomMat(11, 1))
  160. || test_squeeze_all_params(RandomMat(1, 1));
  161. }
  162. static int test_squeeze_3()
  163. {
  164. return 0
  165. || test_squeeze_all_params(RandomMat(120))
  166. || test_squeeze_all_params(RandomMat(1));
  167. }
  168. int main()
  169. {
  170. SRAND(7767517);
  171. return test_squeeze_0() || test_squeeze_1() || test_squeeze_2() || test_squeeze_3();
  172. }