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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 void print_int_array(const ncnn::Mat& a)
  41. {
  42. const int* pa = a;
  43. fprintf(stderr, "[");
  44. for (int i = 0; i < a.w; i++)
  45. {
  46. fprintf(stderr, " %d", pa[i]);
  47. }
  48. fprintf(stderr, " ]");
  49. }
  50. static int test_slice(const ncnn::Mat& a, const ncnn::Mat& slices, int axis)
  51. {
  52. ncnn::ParamDict pd;
  53. pd.set(0, slices);
  54. pd.set(1, axis);
  55. std::vector<ncnn::Mat> weights(0);
  56. std::vector<ncnn::Mat> a0(1);
  57. a0[0] = a;
  58. int ret = test_layer<ncnn::Slice>("Slice", pd, weights, a0, slices.w);
  59. if (ret != 0)
  60. {
  61. fprintf(stderr, "test_slice failed a.dims=%d a=(%d %d %d)", a.dims, a.w, a.h, a.c);
  62. fprintf(stderr, " slices=");
  63. print_int_array(slices);
  64. fprintf(stderr, " axis=%d\n", axis);
  65. }
  66. return ret;
  67. }
  68. static int test_slice_0()
  69. {
  70. ncnn::Mat a = RandomMat(48, 36, 24);
  71. return 0
  72. || test_slice(a, IntArrayMat(-233, -233, -233), 0)
  73. || test_slice(a, IntArrayMat(-233, -233, -233), 1)
  74. || test_slice(a, IntArrayMat(-233, -233, -233), 2)
  75. || test_slice(a, IntArrayMat(-233, -233, -233), -1)
  76. || test_slice(a, IntArrayMat(-233, -233, -233), -2)
  77. || test_slice(a, IntArrayMat(-233, -233, -233), -3);
  78. }
  79. static int test_slice_1()
  80. {
  81. ncnn::Mat a = RandomMat(7, 3, 16);
  82. return 0
  83. || test_slice(a, IntArrayMat(3, 8, -233), 0)
  84. || test_slice(a, IntArrayMat(3, 8, -233), -3);
  85. }
  86. static int test_slice_2()
  87. {
  88. ncnn::Mat a = RandomMat(7, 16, 2);
  89. ncnn::Mat b = RandomMat(7, 16, 24);
  90. return 0
  91. || test_slice(a, IntArrayMat(3, 8, -233), 1)
  92. || test_slice(a, IntArrayMat(3, 8, -233), -2)
  93. || test_slice(b, IntArrayMat(3, 8, 5), 1)
  94. || test_slice(b, IntArrayMat(3, 8, 5), -2);
  95. }
  96. static int test_slice_3()
  97. {
  98. ncnn::Mat a = RandomMat(16, 7, 2);
  99. ncnn::Mat b = RandomMat(16, 7, 8);
  100. return 0
  101. || test_slice(a, IntArrayMat(5, 4, 7), 2)
  102. || test_slice(a, IntArrayMat(5, 4, 7), -1)
  103. || test_slice(b, IntArrayMat(5, 4, 7), 2)
  104. || test_slice(b, IntArrayMat(5, 4, 7), -1);
  105. }
  106. static int test_slice_4()
  107. {
  108. ncnn::Mat a = RandomMat(7, 16);
  109. ncnn::Mat b = RandomMat(16, 2);
  110. ncnn::Mat c = RandomMat(16, 8);
  111. return 0
  112. || test_slice(a, IntArrayMat(3, 8, 5), 0)
  113. || test_slice(a, IntArrayMat(3, 8, 5), -2)
  114. || test_slice(b, IntArrayMat(3, -233, -233), 1)
  115. || test_slice(b, IntArrayMat(3, -233, -233), -1)
  116. || test_slice(c, IntArrayMat(3, 8, 5), 1)
  117. || test_slice(c, IntArrayMat(3, 8, 5), -1);
  118. }
  119. static int test_slice_5()
  120. {
  121. ncnn::Mat a = RandomMat(16);
  122. ncnn::Mat b = RandomMat(24);
  123. return 0
  124. || test_slice(a, IntArrayMat(3, 8, 5), 0)
  125. || test_slice(a, IntArrayMat(3, 8, 5), -1)
  126. || test_slice(b, IntArrayMat(4, 8, -233), 0)
  127. || test_slice(b, IntArrayMat(4, 8, -233), -1);
  128. }
  129. int main()
  130. {
  131. SRAND(7767517);
  132. return 0
  133. || test_slice_0()
  134. || test_slice_1()
  135. || test_slice_2()
  136. || test_slice_3()
  137. || test_slice_4()
  138. || test_slice_5();
  139. }