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_concat.cpp 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/concat.h"
  15. #include "testutil.h"
  16. static int test_concat(const std::vector<ncnn::Mat>& a, int axis)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, axis); //axis
  20. std::vector<ncnn::Mat> weights(0);
  21. int ret = test_layer<ncnn::Concat>("Concat", pd, weights, a);
  22. if (ret != 0)
  23. {
  24. fprintf(stderr, "test_concat failed a[0].dims=%d a[0]=(%d %d %d %d) axis=%d\n", a[0].dims, a[0].w, a[0].h, a[0].d, a[0].c, axis);
  25. }
  26. return ret;
  27. }
  28. static int test_concat_0()
  29. {
  30. ncnn::Mat a[] = {
  31. RandomMat(15, 5, 6, 13),
  32. RandomMat(15, 5, 6, 20),
  33. RandomMat(15, 5, 6, 24),
  34. RandomMat(15, 5, 6, 48)
  35. };
  36. const int n = sizeof(a) / sizeof(a[0]);
  37. for (int i = 0; i < n; i++)
  38. {
  39. for (int j = 0; j < n; j++)
  40. {
  41. for (int k = 0; k < n; k++)
  42. {
  43. std::vector<ncnn::Mat> as(4);
  44. as[0] = a[i];
  45. as[1] = a[j];
  46. as[2] = a[k];
  47. as[3] = a[k];
  48. int ret = test_concat(as, 0) || test_concat(as, -4);
  49. if (ret != 0)
  50. return ret;
  51. }
  52. }
  53. }
  54. return 0;
  55. }
  56. static int test_concat_1()
  57. {
  58. ncnn::Mat a[] = {
  59. RandomMat(15, 3, 15, 13),
  60. RandomMat(15, 3, 16, 20),
  61. RandomMat(15, 3, 17, 24),
  62. RandomMat(15, 3, 18, 48)
  63. };
  64. const int n = sizeof(a) / sizeof(a[0]);
  65. for (int i = 0; i < n; i++)
  66. {
  67. std::vector<ncnn::Mat> as(3);
  68. as[0] = a[i];
  69. as[1] = a[i];
  70. as[2] = a[i];
  71. int ret = test_concat(as, 1) || test_concat(as, -3);
  72. if (ret != 0)
  73. return ret;
  74. }
  75. return 0;
  76. }
  77. static int test_concat_2()
  78. {
  79. ncnn::Mat a[] = {
  80. RandomMat(15, 15, 6, 13),
  81. RandomMat(15, 16, 6, 20),
  82. RandomMat(15, 17, 6, 24),
  83. RandomMat(15, 18, 6, 48)
  84. };
  85. const int n = sizeof(a) / sizeof(a[0]);
  86. for (int i = 0; i < n; i++)
  87. {
  88. std::vector<ncnn::Mat> as(3);
  89. as[0] = a[i];
  90. as[1] = a[i];
  91. as[2] = a[i];
  92. int ret = test_concat(as, 2) || test_concat(as, -2);
  93. if (ret != 0)
  94. return ret;
  95. }
  96. return 0;
  97. }
  98. static int test_concat_3()
  99. {
  100. ncnn::Mat a[] = {
  101. RandomMat(15, 5, 7, 13),
  102. RandomMat(16, 5, 7, 20),
  103. RandomMat(17, 5, 7, 24),
  104. RandomMat(18, 5, 7, 48)
  105. };
  106. const int n = sizeof(a) / sizeof(a[0]);
  107. for (int i = 0; i < n; i++)
  108. {
  109. std::vector<ncnn::Mat> as(3);
  110. as[0] = a[i];
  111. as[1] = a[i];
  112. as[2] = a[i];
  113. int ret = test_concat(as, 3) || test_concat(as, -1);
  114. if (ret != 0)
  115. return ret;
  116. }
  117. return 0;
  118. }
  119. static int test_concat_4()
  120. {
  121. ncnn::Mat a[] = {
  122. RandomMat(15, 13, 13),
  123. RandomMat(15, 13, 20),
  124. RandomMat(15, 13, 24),
  125. RandomMat(15, 13, 48)
  126. };
  127. const int n = sizeof(a) / sizeof(a[0]);
  128. for (int i = 0; i < n; i++)
  129. {
  130. for (int j = 0; j < n; j++)
  131. {
  132. for (int k = 0; k < n; k++)
  133. {
  134. std::vector<ncnn::Mat> as(4);
  135. as[0] = a[i];
  136. as[1] = a[j];
  137. as[2] = a[k];
  138. as[3] = a[k];
  139. int ret = test_concat(as, 0) || test_concat(as, -3);
  140. if (ret != 0)
  141. return ret;
  142. }
  143. }
  144. }
  145. return 0;
  146. }
  147. static int test_concat_5()
  148. {
  149. ncnn::Mat a[] = {
  150. RandomMat(15, 15, 13),
  151. RandomMat(15, 16, 20),
  152. RandomMat(15, 17, 24),
  153. RandomMat(15, 18, 48)
  154. };
  155. const int n = sizeof(a) / sizeof(a[0]);
  156. for (int i = 0; i < n; i++)
  157. {
  158. std::vector<ncnn::Mat> as(3);
  159. as[0] = a[i];
  160. as[1] = a[i];
  161. as[2] = a[i];
  162. int ret = test_concat(as, 1) || test_concat(as, -2);
  163. if (ret != 0)
  164. return ret;
  165. }
  166. return 0;
  167. }
  168. static int test_concat_6()
  169. {
  170. ncnn::Mat a[] = {
  171. RandomMat(15, 13, 13),
  172. RandomMat(16, 13, 20),
  173. RandomMat(17, 13, 24),
  174. RandomMat(18, 13, 48)
  175. };
  176. const int n = sizeof(a) / sizeof(a[0]);
  177. for (int i = 0; i < n; i++)
  178. {
  179. std::vector<ncnn::Mat> as(3);
  180. as[0] = a[i];
  181. as[1] = a[i];
  182. as[2] = a[i];
  183. int ret = test_concat(as, 2) || test_concat(as, -1);
  184. if (ret != 0)
  185. return ret;
  186. }
  187. return 0;
  188. }
  189. static int test_concat_7()
  190. {
  191. ncnn::Mat a[] = {
  192. RandomMat(19, 29),
  193. RandomMat(19, 44),
  194. RandomMat(19, 56),
  195. RandomMat(19, 80)
  196. };
  197. const int n = sizeof(a) / sizeof(a[0]);
  198. for (int i = 0; i < n; i++)
  199. {
  200. for (int j = 0; j < n; j++)
  201. {
  202. for (int k = 0; k < n; k++)
  203. {
  204. std::vector<ncnn::Mat> as(4);
  205. as[0] = a[i];
  206. as[1] = a[j];
  207. as[2] = a[k];
  208. as[3] = a[k];
  209. int ret = test_concat(as, 0) || test_concat(as, -2);
  210. if (ret != 0)
  211. return ret;
  212. }
  213. }
  214. }
  215. return 0;
  216. }
  217. static int test_concat_8()
  218. {
  219. ncnn::Mat a[] = {
  220. RandomMat(19, 29),
  221. RandomMat(16, 44),
  222. RandomMat(17, 56),
  223. RandomMat(18, 80)
  224. };
  225. const int n = sizeof(a) / sizeof(a[0]);
  226. for (int i = 0; i < n; i++)
  227. {
  228. std::vector<ncnn::Mat> as(3);
  229. as[0] = a[i];
  230. as[1] = a[i];
  231. as[2] = a[i];
  232. int ret = test_concat(as, 1) || test_concat(as, -1);
  233. if (ret != 0)
  234. return ret;
  235. }
  236. return 0;
  237. }
  238. static int test_concat_9()
  239. {
  240. ncnn::Mat a[] = {
  241. RandomMat(29),
  242. RandomMat(44),
  243. RandomMat(56),
  244. RandomMat(80)
  245. };
  246. const int n = sizeof(a) / sizeof(a[0]);
  247. for (int i = 0; i < n; i++)
  248. {
  249. for (int j = 0; j < n; j++)
  250. {
  251. for (int k = 0; k < n; k++)
  252. {
  253. std::vector<ncnn::Mat> as(4);
  254. as[0] = a[i];
  255. as[1] = a[j];
  256. as[2] = a[k];
  257. as[3] = a[k];
  258. int ret = test_concat(as, 0) || test_concat(as, -1);
  259. if (ret != 0)
  260. return ret;
  261. }
  262. }
  263. }
  264. return 0;
  265. }
  266. int main()
  267. {
  268. SRAND(7767517);
  269. return 0
  270. || test_concat_0()
  271. || test_concat_1()
  272. || test_concat_2()
  273. || test_concat_3()
  274. || test_concat_4()
  275. || test_concat_5()
  276. || test_concat_6()
  277. || test_concat_7()
  278. || test_concat_8()
  279. || test_concat_9();
  280. }