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.2 kB

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