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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_0()
  79. {
  80. ncnn::Mat a[] = {
  81. RandomMat(30, 32, 36, 48),
  82. RandomMat(36, 30, 32, 51),
  83. RandomMat(30, 32, 36, 60)
  84. };
  85. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  86. {
  87. int ret = 0
  88. || test_slice(a[i], IntArrayMat(-233, -233, -233), 0)
  89. || test_slice(a[i], IntArrayMat(-233, -233, -233), 1)
  90. || test_slice(a[i], IntArrayMat(-233, -233, -233), -2)
  91. || test_slice(a[i], IntArrayMat(-233, -233, -233), 3)
  92. || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0)
  93. || test_slice(a[i], IntArrayMat(12, 16, -233), 0)
  94. || test_slice(a[i], IntArrayMat(32, 8, -233), 0)
  95. || test_slice(a[i], IntArrayMat(2, 12, 16, -233), 1)
  96. || test_slice(a[i], IntArrayMat(16, 4, 5, -233), -2)
  97. || test_slice(a[i], IntArrayMat(8, 2, 16, -233), 3);
  98. if (ret != 0)
  99. return ret;
  100. }
  101. return 0;
  102. }
  103. static int test_slice_1()
  104. {
  105. ncnn::Mat a[] = {
  106. RandomMat(51, 36, 48),
  107. RandomMat(48, 48, 51),
  108. RandomMat(51, 36, 60)
  109. };
  110. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  111. {
  112. int ret = 0
  113. || test_slice(a[i], IntArrayMat(-233, -233, -233), 0)
  114. || test_slice(a[i], IntArrayMat(-233, -233, -233), 1)
  115. || test_slice(a[i], IntArrayMat(-233, -233, -233), -1)
  116. || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0)
  117. || test_slice(a[i], IntArrayMat(12, 16, -233), 0)
  118. || test_slice(a[i], IntArrayMat(32, 8, -233), 0)
  119. || test_slice(a[i], IntArrayMat(2, 12, 16, -233), 1)
  120. || test_slice(a[i], IntArrayMat(16, 4, 5, -233), -1);
  121. if (ret != 0)
  122. return ret;
  123. }
  124. return 0;
  125. }
  126. static int test_slice_2()
  127. {
  128. ncnn::Mat a[] = {
  129. RandomMat(36, 48),
  130. RandomMat(48, 51),
  131. RandomMat(36, 60)
  132. };
  133. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  134. {
  135. int ret = 0
  136. || test_slice(a[i], IntArrayMat(-233, -233, -233), 0)
  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), -2)
  141. || test_slice(a[i], IntArrayMat(2, 12, 16, -233), -1);
  142. if (ret != 0)
  143. return ret;
  144. }
  145. return 0;
  146. }
  147. static int test_slice_3()
  148. {
  149. ncnn::Mat a[] = {
  150. RandomMat(48),
  151. RandomMat(51),
  152. RandomMat(60)
  153. };
  154. for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  155. {
  156. int ret = 0
  157. || test_slice(a[i], IntArrayMat(-233, -233, -233), 0)
  158. || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0)
  159. || test_slice(a[i], IntArrayMat(12, 16, -233), 0)
  160. || test_slice(a[i], IntArrayMat(32, 8, -233), -1);
  161. if (ret != 0)
  162. return ret;
  163. }
  164. return 0;
  165. }
  166. int main()
  167. {
  168. SRAND(7767517);
  169. return 0
  170. || test_slice_0()
  171. || test_slice_1()
  172. || test_slice_2()
  173. || test_slice_3();
  174. }