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_crop.cpp 6.5 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/crop.h"
  15. #include "testutil.h"
  16. static int test_crop(const ncnn::Mat& a, int woffset, int hoffset, int coffset, int outw, int outh, int outc, int woffset2, int hoffset2, int coffset2)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, woffset); // woffset
  20. pd.set(1, hoffset); // hoffset
  21. pd.set(2, coffset); // coffset
  22. pd.set(3, outw); // outw
  23. pd.set(4, outh); // outh
  24. pd.set(5, outc); // outc
  25. pd.set(6, woffset2); // woffset2
  26. pd.set(7, hoffset2); // hoffset2
  27. pd.set(8, coffset2); // coffset2
  28. std::vector<ncnn::Mat> weights(0);
  29. int ret = test_layer<ncnn::Crop>("Crop", pd, weights, a);
  30. if (ret != 0)
  31. {
  32. fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d) woffset=%d hoffset=%d coffset=%d outw=%d outh=%d outc=%d woffset2=%d hoffset2=%d coffset2=%d\n", a.dims, a.w, a.h, a.c, woffset, hoffset, coffset, outw, outh, outc, woffset2, hoffset2, coffset2);
  33. }
  34. return ret;
  35. }
  36. static ncnn::Mat IntArrayMat(int a0)
  37. {
  38. ncnn::Mat m(1);
  39. int* p = m;
  40. p[0] = a0;
  41. return m;
  42. }
  43. static ncnn::Mat IntArrayMat(int a0, int a1)
  44. {
  45. ncnn::Mat m(2);
  46. int* p = m;
  47. p[0] = a0;
  48. p[1] = a1;
  49. return m;
  50. }
  51. static ncnn::Mat IntArrayMat(int a0, int a1, int a2)
  52. {
  53. ncnn::Mat m(3);
  54. int* p = m;
  55. p[0] = a0;
  56. p[1] = a1;
  57. p[2] = a2;
  58. return m;
  59. }
  60. static void print_int_array(const ncnn::Mat& a)
  61. {
  62. const int* pa = a;
  63. fprintf(stderr, "[");
  64. for (int i = 0; i < a.w; i++)
  65. {
  66. fprintf(stderr, " %d", pa[i]);
  67. }
  68. fprintf(stderr, " ]");
  69. }
  70. static int test_crop(const ncnn::Mat& a, const ncnn::Mat& starts, const ncnn::Mat& ends, const ncnn::Mat& axes)
  71. {
  72. ncnn::ParamDict pd;
  73. pd.set(9, starts); // starts
  74. pd.set(10, ends); // ends
  75. pd.set(11, axes); // axes
  76. std::vector<ncnn::Mat> weights(0);
  77. int ret = test_layer<ncnn::Crop>("Crop", pd, weights, a);
  78. if (ret != 0)
  79. {
  80. fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d)", a.dims, a.w, a.h, a.c);
  81. fprintf(stderr, " starts=");
  82. print_int_array(starts);
  83. fprintf(stderr, " ends=");
  84. print_int_array(ends);
  85. fprintf(stderr, " axes=");
  86. print_int_array(axes);
  87. fprintf(stderr, "\n");
  88. }
  89. return ret;
  90. }
  91. static int test_crop_0()
  92. {
  93. ncnn::Mat a = RandomMat(13, 11, 16);
  94. return 0
  95. || test_crop(a, 0, 0, 0, -233, -233, -233, 0, 0, 0)
  96. || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 0, 0)
  97. || test_crop(a, 5, 0, 0, -233, 6, -233, 0, 0, 0)
  98. || test_crop(a, 5, 3, 0, -233, -233, 12, 0, 0, 0)
  99. || test_crop(a, 0, 3, 4, 7, -233, 8, 0, 0, 0)
  100. || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 0, 0)
  101. || test_crop(a, 5, 3, 6, 2, 7, 9, 0, 0, 0)
  102. || test_crop(a, 0, 3, 4, -233, 4, 5, 0, 0, 0)
  103. || test_crop(a, 5, 0, 5, 6, 6, 8, 0, 0, 0)
  104. || test_crop(a, 5, 3, 6, 4, 4, 4, 0, 0, 0);
  105. }
  106. static int test_crop_1()
  107. {
  108. ncnn::Mat a = RandomMat(13, 11, 16);
  109. return 0
  110. || test_crop(a, 0, 0, 0, -233, -233, -233, 3, 4, 6)
  111. || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 3, 4)
  112. || test_crop(a, 5, 0, 0, -233, 6, -233, 5, 0, 2)
  113. || test_crop(a, 5, 3, 0, -233, -233, 12, 2, 1, 1)
  114. || test_crop(a, 0, 3, 4, 7, -233, 8, 3, 4, 4)
  115. || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 3, 0)
  116. || test_crop(a, 5, 3, 6, 2, 7, 9, 1, 2, 3)
  117. || test_crop(a, 0, 3, 4, -233, 4, 5, 3, 2, 1);
  118. }
  119. static int test_crop_2()
  120. {
  121. ncnn::Mat a = RandomMat(13, 11, 17);
  122. return 0
  123. || test_crop(a, 0, 0, 0, -233, -233, -233, 3, 4, 6)
  124. || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 3, 4)
  125. || test_crop(a, 5, 0, 0, -233, 6, -233, 5, 0, 2)
  126. || test_crop(a, 5, 3, 0, -233, -233, 12, 2, 1, 1)
  127. || test_crop(a, 0, 3, 4, 7, -233, 8, 3, 4, 4)
  128. || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 3, 0)
  129. || test_crop(a, 5, 3, 6, 2, 7, 9, 1, 2, 3)
  130. || test_crop(a, 0, 3, 4, -233, 4, 5, 3, 2, 1);
  131. }
  132. static int test_crop_3()
  133. {
  134. ncnn::Mat a = RandomMat(13, 11, 16);
  135. return 0
  136. || test_crop(a, IntArrayMat(0, 0, 0), IntArrayMat(100, 100, 100), IntArrayMat(0, 1, 2))
  137. || test_crop(a, IntArrayMat(4), IntArrayMat(8), IntArrayMat(0))
  138. || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(1))
  139. || test_crop(a, IntArrayMat(3), IntArrayMat(5), IntArrayMat(2))
  140. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(0, 1))
  141. || test_crop(a, IntArrayMat(1, 4), IntArrayMat(-5, 7), IntArrayMat(1, -3))
  142. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(-1, -2))
  143. || test_crop(a, IntArrayMat(1, 2, 3), IntArrayMat(-3, -2, -1), IntArrayMat(-3, -2, -1));
  144. }
  145. static int test_crop_4()
  146. {
  147. ncnn::Mat a = RandomMat(13, 11, 17);
  148. return 0
  149. || test_crop(a, IntArrayMat(0, 0, 0), IntArrayMat(100, 100, 100), IntArrayMat(0, 1, 2))
  150. || test_crop(a, IntArrayMat(4), IntArrayMat(8), IntArrayMat(0))
  151. || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(1))
  152. || test_crop(a, IntArrayMat(3), IntArrayMat(5), IntArrayMat(2))
  153. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(0, 1))
  154. || test_crop(a, IntArrayMat(1, 4), IntArrayMat(-5, 7), IntArrayMat(1, -3))
  155. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(-1, -2))
  156. || test_crop(a, IntArrayMat(1, 2, 3), IntArrayMat(-3, -2, -1), IntArrayMat(-3, -2, -1));
  157. }
  158. int main()
  159. {
  160. SRAND(7767517);
  161. return 0
  162. || test_crop_0()
  163. || test_crop_1()
  164. || test_crop_2()
  165. || test_crop_3()
  166. || test_crop_4();
  167. }