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