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 5.5 kB

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