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

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