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.2 kB

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