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_slice.cpp 6.7 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "testutil.h"
  15. static ncnn::Mat IntArrayMat(int a0)
  16. {
  17. ncnn::Mat m(1);
  18. int* p = m;
  19. p[0] = a0;
  20. return m;
  21. }
  22. static ncnn::Mat IntArrayMat(int a0, int a1)
  23. {
  24. ncnn::Mat m(2);
  25. int* p = m;
  26. p[0] = a0;
  27. p[1] = a1;
  28. return m;
  29. }
  30. static ncnn::Mat IntArrayMat(int a0, int a1, int a2)
  31. {
  32. ncnn::Mat m(3);
  33. int* p = m;
  34. p[0] = a0;
  35. p[1] = a1;
  36. p[2] = a2;
  37. return m;
  38. }
  39. static ncnn::Mat IntArrayMat(int a0, int a1, int a2, int a3)
  40. {
  41. ncnn::Mat m(4);
  42. int* p = m;
  43. p[0] = a0;
  44. p[1] = a1;
  45. p[2] = a2;
  46. p[3] = a3;
  47. return m;
  48. }
  49. static void print_int_array(const ncnn::Mat& a)
  50. {
  51. const int* pa = a;
  52. fprintf(stderr, "[");
  53. for (int i = 0; i < a.w; i++)
  54. {
  55. fprintf(stderr, " %d", pa[i]);
  56. }
  57. fprintf(stderr, " ]");
  58. }
  59. static int test_slice(const ncnn::Mat& a, const ncnn::Mat& slices, int axis)
  60. {
  61. ncnn::ParamDict pd;
  62. pd.set(0, slices);
  63. pd.set(1, axis);
  64. std::vector<ncnn::Mat> weights(0);
  65. std::vector<ncnn::Mat> a0(1);
  66. a0[0] = a;
  67. int ret = test_layer("Slice", pd, weights, a0, slices.w);
  68. if (ret != 0)
  69. {
  70. fprintf(stderr, "test_slice failed a.dims=%d a=(%d %d %d %d)", a.dims, a.w, a.h, a.d, a.c);
  71. fprintf(stderr, " slices=");
  72. print_int_array(slices);
  73. fprintf(stderr, " axis=%d\n", axis);
  74. }
  75. return ret;
  76. }
  77. static int test_slice_indices(const ncnn::Mat& a, const ncnn::Mat& indices, int axis)
  78. {
  79. ncnn::ParamDict pd;
  80. pd.set(1, axis);
  81. pd.set(2, indices);
  82. std::vector<ncnn::Mat> weights(0);
  83. std::vector<ncnn::Mat> a0(1);
  84. a0[0] = a;
  85. int ret = test_layer("Slice", pd, weights, a0, indices.w);
  86. if (ret != 0)
  87. {
  88. fprintf(stderr, "test_slice_indices failed a.dims=%d a=(%d %d %d %d)", a.dims, a.w, a.h, a.d, a.c);
  89. fprintf(stderr, " indices=");
  90. print_int_array(indices);
  91. fprintf(stderr, " axis=%d\n", axis);
  92. }
  93. return ret;
  94. }
  95. static int test_slice_0()
  96. {
  97. ncnn::Mat a[] = {
  98. RandomMat(30, 32, 36, 48),
  99. RandomMat(36, 30, 32, 51),
  100. RandomMat(30, 32, 36, 60)
  101. };
  102. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  103. {
  104. int ret = 0
  105. || test_slice(a[i], IntArrayMat(-233, -233, -233), 0)
  106. || test_slice(a[i], IntArrayMat(-233, -233, -233), 1)
  107. || test_slice(a[i], IntArrayMat(-233, -233, -233), -2)
  108. || test_slice(a[i], IntArrayMat(-233, -233, -233), 3)
  109. || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0)
  110. || test_slice(a[i], IntArrayMat(12, 16, -233), 0)
  111. || test_slice(a[i], IntArrayMat(32, 8, -233), 0)
  112. || test_slice(a[i], IntArrayMat(2, 12, 16, -233), 1)
  113. || test_slice(a[i], IntArrayMat(16, 4, 5, -233), -2)
  114. || test_slice(a[i], IntArrayMat(8, 2, 16, -233), 3)
  115. || test_slice_indices(a[i], IntArrayMat(2, -24, -8), 0)
  116. || test_slice_indices(a[i], IntArrayMat(4, 20, 4), 1)
  117. || test_slice_indices(a[i], IntArrayMat(16, -16), -2)
  118. || test_slice_indices(a[i], IntArrayMat(1, -12), 3);
  119. if (ret != 0)
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. static int test_slice_1()
  125. {
  126. ncnn::Mat a[] = {
  127. RandomMat(51, 36, 48),
  128. RandomMat(48, 48, 51),
  129. RandomMat(51, 36, 60)
  130. };
  131. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  132. {
  133. int ret = 0
  134. || test_slice(a[i], IntArrayMat(-233, -233, -233), 0)
  135. || test_slice(a[i], IntArrayMat(-233, -233, -233), 1)
  136. || test_slice(a[i], IntArrayMat(-233, -233, -233), -1)
  137. || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0)
  138. || test_slice(a[i], IntArrayMat(12, 16, -233), 0)
  139. || test_slice(a[i], IntArrayMat(32, 8, -233), 0)
  140. || test_slice(a[i], IntArrayMat(2, 12, 16, -233), 1)
  141. || test_slice(a[i], IntArrayMat(16, 4, 5, -233), -1)
  142. || test_slice_indices(a[i], IntArrayMat(2, -24, -8), 0)
  143. || test_slice_indices(a[i], IntArrayMat(4, 20, 4), 1)
  144. || test_slice_indices(a[i], IntArrayMat(1, -12), 2);
  145. if (ret != 0)
  146. return ret;
  147. }
  148. return 0;
  149. }
  150. static int test_slice_2()
  151. {
  152. ncnn::Mat a[] = {
  153. RandomMat(36, 48),
  154. RandomMat(48, 51),
  155. RandomMat(36, 60)
  156. };
  157. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  158. {
  159. int ret = 0
  160. || test_slice(a[i], IntArrayMat(-233, -233, -233), 0)
  161. || test_slice(a[i], IntArrayMat(-233, -233, -233), -1)
  162. || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0)
  163. || test_slice(a[i], IntArrayMat(12, 16, -233), 0)
  164. || test_slice(a[i], IntArrayMat(32, 8, -233), -2)
  165. || test_slice(a[i], IntArrayMat(2, 12, 16, -233), -1)
  166. || test_slice_indices(a[i], IntArrayMat(2, -24, -8), 0)
  167. || test_slice_indices(a[i], IntArrayMat(1, -12), 1);
  168. if (ret != 0)
  169. return ret;
  170. }
  171. return 0;
  172. }
  173. static int test_slice_3()
  174. {
  175. ncnn::Mat a[] = {
  176. RandomMat(48),
  177. RandomMat(51),
  178. RandomMat(60)
  179. };
  180. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  181. {
  182. int ret = 0
  183. || test_slice(a[i], IntArrayMat(-233, -233, -233), 0)
  184. || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0)
  185. || test_slice(a[i], IntArrayMat(12, 16, -233), 0)
  186. || test_slice(a[i], IntArrayMat(32, 8, -233), -1)
  187. || test_slice_indices(a[i], IntArrayMat(2, -24, -8), 0);
  188. if (ret != 0)
  189. return ret;
  190. }
  191. return 0;
  192. }
  193. int main()
  194. {
  195. SRAND(7767517);
  196. return 0
  197. || test_slice_0()
  198. || test_slice_1()
  199. || test_slice_2()
  200. || test_slice_3();
  201. }