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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. }
  76. static int test_slice_1()
  77. {
  78. ncnn::Mat a = RandomMat(7, 3, 16);
  79. return test_slice(a, IntArrayMat(3, 8, -233), 0);
  80. }
  81. static int test_slice_2()
  82. {
  83. ncnn::Mat a = RandomMat(7, 16, 2);
  84. ncnn::Mat b = RandomMat(7, 16, 24);
  85. return 0
  86. || test_slice(a, IntArrayMat(3, 8, -233), 1)
  87. || test_slice(b, IntArrayMat(3, 8, 5), 1);
  88. }
  89. static int test_slice_3()
  90. {
  91. ncnn::Mat a = RandomMat(16, 7, 2);
  92. ncnn::Mat b = RandomMat(16, 7, 8);
  93. return 0
  94. || test_slice(a, IntArrayMat(5, 4, 7), 2)
  95. || test_slice(b, IntArrayMat(5, 4, 7), 2);
  96. }
  97. static int test_slice_4()
  98. {
  99. ncnn::Mat a = RandomMat(7, 16);
  100. ncnn::Mat b = RandomMat(16, 2);
  101. ncnn::Mat c = RandomMat(16, 8);
  102. return 0
  103. || test_slice(a, IntArrayMat(3, 8, 5), 0)
  104. || test_slice(b, IntArrayMat(3, -233, -233), 1)
  105. || test_slice(c, IntArrayMat(3, 8, 5), 1);
  106. }
  107. static int test_slice_5()
  108. {
  109. ncnn::Mat a = RandomMat(16);
  110. ncnn::Mat b = RandomMat(24);
  111. return 0
  112. || test_slice(a, IntArrayMat(3, 8, 5), 0)
  113. || test_slice(b, IntArrayMat(4, 8, -233), 0);
  114. }
  115. int main()
  116. {
  117. SRAND(7767517);
  118. return 0
  119. || test_slice_0()
  120. || test_slice_1()
  121. || test_slice_2()
  122. || test_slice_3()
  123. || test_slice_4()
  124. || test_slice_5();
  125. }