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

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "testutil.h"
  15. #include "layer/crop.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, bool use_packing_layout)
  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::ModelBinFromMatArray mb(weights.data());
  30. ncnn::Option opt;
  31. opt.num_threads = 1;
  32. opt.use_vulkan_compute = true;
  33. opt.use_int8_inference = false;
  34. opt.use_fp16_packed = false;
  35. opt.use_fp16_storage = false;
  36. opt.use_fp16_arithmetic = false;
  37. opt.use_int8_storage = false;
  38. opt.use_int8_arithmetic = false;
  39. opt.use_packing_layout = use_packing_layout;
  40. int ret = test_layer<ncnn::Crop>("Crop", pd, mb, opt, a);
  41. if (ret != 0)
  42. {
  43. 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 use_packing_layout=%d\n", a.dims, a.w, a.h, a.c, woffset, hoffset, coffset, outw, outh, outc, woffset2, hoffset2, coffset2, use_packing_layout);
  44. }
  45. return ret;
  46. }
  47. static ncnn::Mat IntArrayMat(int a0)
  48. {
  49. ncnn::Mat m(1);
  50. int* p = m;
  51. p[0] = a0;
  52. return m;
  53. }
  54. static ncnn::Mat IntArrayMat(int a0, int a1)
  55. {
  56. ncnn::Mat m(2);
  57. int* p = m;
  58. p[0] = a0;
  59. p[1] = a1;
  60. return m;
  61. }
  62. static ncnn::Mat IntArrayMat(int a0, int a1, int a2)
  63. {
  64. ncnn::Mat m(3);
  65. int* p = m;
  66. p[0] = a0;
  67. p[1] = a1;
  68. p[2] = a2;
  69. return m;
  70. }
  71. static void print_int_array(const ncnn::Mat& a)
  72. {
  73. const int* pa = a;
  74. fprintf(stderr, "[");
  75. for (int i=0; i<a.w; i++)
  76. {
  77. fprintf(stderr, " %d", pa[i]);
  78. }
  79. fprintf(stderr, " ]");
  80. }
  81. static int test_crop(const ncnn::Mat& a, const ncnn::Mat& starts, const ncnn::Mat& ends, const ncnn::Mat& axes, bool use_packing_layout)
  82. {
  83. ncnn::ParamDict pd;
  84. pd.set(9, starts);// starts
  85. pd.set(10, ends);// ends
  86. pd.set(11, axes);// axes
  87. std::vector<ncnn::Mat> weights(0);
  88. ncnn::ModelBinFromMatArray mb(weights.data());
  89. ncnn::Option opt;
  90. opt.num_threads = 1;
  91. opt.use_vulkan_compute = true;
  92. opt.use_int8_inference = false;
  93. opt.use_fp16_packed = false;
  94. opt.use_fp16_storage = false;
  95. opt.use_fp16_arithmetic = false;
  96. opt.use_int8_storage = false;
  97. opt.use_int8_arithmetic = false;
  98. opt.use_packing_layout = use_packing_layout;
  99. int ret = test_layer<ncnn::Crop>("Crop", pd, mb, opt, a);
  100. if (ret != 0)
  101. {
  102. fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d)", a.dims, a.w, a.h, a.c);
  103. fprintf(stderr, " starts="); print_int_array(starts);
  104. fprintf(stderr, " ends="); print_int_array(ends);
  105. fprintf(stderr, " axes="); print_int_array(axes);
  106. fprintf(stderr, " use_packing_layout=%d\n", use_packing_layout);
  107. }
  108. return ret;
  109. }
  110. static int test_crop_0()
  111. {
  112. ncnn::Mat a = RandomMat(13, 11, 16);
  113. return 0
  114. || test_crop(a, 0, 0, 0, -233, -233, -233, 0, 0, 0, false)
  115. || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 0, 0, false)
  116. || test_crop(a, 5, 0, 0, -233, 6, -233, 0, 0, 0, false)
  117. || test_crop(a, 5, 3, 0, -233, -233, 12, 0, 0, 0, false)
  118. || test_crop(a, 0, 3, 4, 7, -233, 8, 0, 0, 0, false)
  119. || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 0, 0, false)
  120. || test_crop(a, 5, 3, 6, 2, 7, 9, 0, 0, 0, false)
  121. || test_crop(a, 0, 3, 4, -233, 4, 5, 0, 0, 0, false)
  122. || test_crop(a, 5, 0, 5, 6, 6, 8, 0, 0, 0, false)
  123. || test_crop(a, 5, 3, 6, 4, 4, 4, 0, 0, 0, false)
  124. || test_crop(a, 0, 0, 0, -233, -233, -233, 0, 0, 0, true)
  125. || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 0, 0, true)
  126. || test_crop(a, 5, 0, 0, -233, 6, -233, 0, 0, 0, true)
  127. || test_crop(a, 5, 3, 0, -233, -233, 12, 0, 0, 0, true)
  128. || test_crop(a, 0, 3, 4, 7, -233, 8, 0, 0, 0, true)
  129. || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 0, 0, true)
  130. || test_crop(a, 5, 3, 6, 2, 7, 9, 0, 0, 0, true)
  131. || test_crop(a, 0, 3, 4, -233, 4, 5, 0, 0, 0, true)
  132. || test_crop(a, 5, 0, 5, 6, 6, 8, 0, 0, 0, true)
  133. || test_crop(a, 5, 3, 6, 4, 4, 4, 0, 0, 0, true)
  134. ;
  135. }
  136. static int test_crop_1()
  137. {
  138. ncnn::Mat a = RandomMat(13, 11, 16);
  139. return 0
  140. || test_crop(a, 0, 0, 0, -233, -233, -233, 3, 4, 6, false)
  141. || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 3, 4, false)
  142. || test_crop(a, 5, 0, 0, -233, 6, -233, 5, 0, 2, false)
  143. || test_crop(a, 5, 3, 0, -233, -233, 12, 2, 1, 1, false)
  144. || test_crop(a, 0, 3, 4, 7, -233, 8, 3, 4, 4, false)
  145. || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 3, 0, false)
  146. || test_crop(a, 5, 3, 6, 2, 7, 9, 1, 2, 3, false)
  147. || test_crop(a, 0, 3, 4, -233, 4, 5, 3, 2, 1, false)
  148. || test_crop(a, 0, 0, 0, -233, -233, -233, 3, 4, 6, true)
  149. || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 3, 4, true)
  150. || test_crop(a, 5, 0, 0, -233, 6, -233, 5, 0, 2, true)
  151. || test_crop(a, 5, 3, 0, -233, -233, 12, 2, 1, 1, true)
  152. || test_crop(a, 0, 3, 4, 7, -233, 8, 3, 4, 4, true)
  153. || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 3, 0, true)
  154. || test_crop(a, 5, 3, 6, 2, 7, 9, 1, 2, 3, true)
  155. || test_crop(a, 0, 3, 4, -233, 4, 5, 3, 2, 1, true)
  156. ;
  157. }
  158. static int test_crop_2()
  159. {
  160. ncnn::Mat a = RandomMat(13, 11, 17);
  161. return 0
  162. || test_crop(a, 0, 0, 0, -233, -233, -233, 3, 4, 6, false)
  163. || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 3, 4, false)
  164. || test_crop(a, 5, 0, 0, -233, 6, -233, 5, 0, 2, false)
  165. || test_crop(a, 5, 3, 0, -233, -233, 12, 2, 1, 1, false)
  166. || test_crop(a, 0, 3, 4, 7, -233, 8, 3, 4, 4, false)
  167. || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 3, 0, false)
  168. || test_crop(a, 5, 3, 6, 2, 7, 9, 1, 2, 3, false)
  169. || test_crop(a, 0, 3, 4, -233, 4, 5, 3, 2, 1, false)
  170. || test_crop(a, 0, 0, 0, -233, -233, -233, 3, 4, 6, true)
  171. || test_crop(a, 0, 3, 0, 10, -233, -233, 0, 3, 4, true)
  172. || test_crop(a, 5, 0, 0, -233, 6, -233, 5, 0, 2, true)
  173. || test_crop(a, 5, 3, 0, -233, -233, 12, 2, 1, 1, true)
  174. || test_crop(a, 0, 3, 4, 7, -233, 8, 3, 4, 4, true)
  175. || test_crop(a, 5, 0, 5, 4, -233, 4, 0, 3, 0, true)
  176. || test_crop(a, 5, 3, 6, 2, 7, 9, 1, 2, 3, true)
  177. || test_crop(a, 0, 3, 4, -233, 4, 5, 3, 2, 1, true)
  178. ;
  179. }
  180. static int test_crop_3()
  181. {
  182. ncnn::Mat a = RandomMat(13, 11, 16);
  183. return 0
  184. || test_crop(a, IntArrayMat(0, 0, 0), IntArrayMat(100, 100, 100), IntArrayMat(1, 2, 3), false)
  185. || test_crop(a, IntArrayMat(4), IntArrayMat(8), IntArrayMat(1), false)
  186. || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(2), false)
  187. || test_crop(a, IntArrayMat(3), IntArrayMat(5), IntArrayMat(3), false)
  188. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(1, 2), false)
  189. || test_crop(a, IntArrayMat(1, 4), IntArrayMat(-5, 7), IntArrayMat(2, -3), false)
  190. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(-1, -2), false)
  191. || test_crop(a, IntArrayMat(1, 2, 3), IntArrayMat(-3, -2, -1), IntArrayMat(-3, -2, -1), false)
  192. || test_crop(a, IntArrayMat(0, 0, 0), IntArrayMat(100, 100, 100), IntArrayMat(1, 2, 3), true)
  193. || test_crop(a, IntArrayMat(4), IntArrayMat(8), IntArrayMat(1), true)
  194. || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(2), true)
  195. || test_crop(a, IntArrayMat(3), IntArrayMat(5), IntArrayMat(3), true)
  196. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(1, 2), true)
  197. || test_crop(a, IntArrayMat(1, 4), IntArrayMat(-5, 7), IntArrayMat(3, -3), true)
  198. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(-1, -2), true)
  199. || test_crop(a, IntArrayMat(1, 2, 3), IntArrayMat(-3, -2, -1), IntArrayMat(-3, -2, -1), true)
  200. ;
  201. }
  202. static int test_crop_4()
  203. {
  204. ncnn::Mat a = RandomMat(13, 11, 17);
  205. return 0
  206. || test_crop(a, IntArrayMat(0, 0, 0), IntArrayMat(100, 100, 100), IntArrayMat(1, 2, 3), false)
  207. || test_crop(a, IntArrayMat(4), IntArrayMat(8), IntArrayMat(1), false)
  208. || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(2), false)
  209. || test_crop(a, IntArrayMat(3), IntArrayMat(5), IntArrayMat(3), false)
  210. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(1, 2), false)
  211. || test_crop(a, IntArrayMat(1, 4), IntArrayMat(-5, 7), IntArrayMat(2, -3), false)
  212. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(-1, -2), false)
  213. || test_crop(a, IntArrayMat(1, 2, 3), IntArrayMat(-3, -2, -1), IntArrayMat(-3, -2, -1), false)
  214. || test_crop(a, IntArrayMat(0, 0, 0), IntArrayMat(100, 100, 100), IntArrayMat(1, 2, 3), true)
  215. || test_crop(a, IntArrayMat(4), IntArrayMat(8), IntArrayMat(1), true)
  216. || test_crop(a, IntArrayMat(2), IntArrayMat(7), IntArrayMat(2), true)
  217. || test_crop(a, IntArrayMat(3), IntArrayMat(5), IntArrayMat(3), true)
  218. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(1, 2), true)
  219. || test_crop(a, IntArrayMat(1, 4), IntArrayMat(-5, 7), IntArrayMat(3, -3), true)
  220. || test_crop(a, IntArrayMat(2, 1), IntArrayMat(4, -2), IntArrayMat(-1, -2), true)
  221. || test_crop(a, IntArrayMat(1, 2, 3), IntArrayMat(-3, -2, -1), IntArrayMat(-3, -2, -1), true)
  222. ;
  223. }
  224. int main()
  225. {
  226. SRAND(7767517);
  227. return 0
  228. || test_crop_0()
  229. || test_crop_1()
  230. || test_crop_2()
  231. || test_crop_3()
  232. || test_crop_4()
  233. ;
  234. }