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

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