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