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_reshape.cpp 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. static int test_reshape(const ncnn::Mat& a, int outw, int outh, int outd, int outc)
  16. {
  17. ncnn::ParamDict pd;
  18. pd.set(0, outw); // w
  19. pd.set(1, outh); // h
  20. pd.set(11, outd); // d
  21. pd.set(2, outc); // c
  22. std::vector<ncnn::Mat> weights(0);
  23. int ret = test_layer("Reshape", pd, weights, a);
  24. if (ret != 0)
  25. {
  26. fprintf(stderr, "test_reshape failed a.dims=%d a=(%d %d %d %d) outw=%d outh=%d outd=%d outc=%d\n", a.dims, a.w, a.h, a.d, a.c, outw, outh, outd, outc);
  27. }
  28. return ret;
  29. }
  30. static int test_reshape_0()
  31. {
  32. ncnn::Mat a = RandomMat(3, 8, 5, 32);
  33. return 0
  34. || test_reshape(a, 5, 8, 3, 32)
  35. || test_reshape(a, 3, 8, 32, 5)
  36. || test_reshape(a, 32, 5, 8, 3)
  37. || test_reshape(a, 4, 3, 16, -1)
  38. || test_reshape(a, 6, 16, -1, 2)
  39. || test_reshape(a, 4, -1, 8, 8)
  40. || test_reshape(a, -1, 16, 6, 2)
  41. || test_reshape(a, 16, 6, -233, -1)
  42. || test_reshape(a, 8, -1, -233, 8)
  43. || test_reshape(a, -1, 6, -233, 16)
  44. || test_reshape(a, 8, -1, -233, -233)
  45. || test_reshape(a, -1, 6, -233, -233)
  46. || test_reshape(a, -1, -233, -233, -233);
  47. }
  48. static int test_reshape_1()
  49. {
  50. ncnn::Mat a = RandomMat(4, 5, 12, 26);
  51. return 0
  52. || test_reshape(a, 12, 5, 4, 26)
  53. || test_reshape(a, 4, 26, 12, 5)
  54. || test_reshape(a, 13, 5, 12, 8)
  55. || test_reshape(a, 2, 10, 8, -1)
  56. || test_reshape(a, 13, 4, -1, 10)
  57. || test_reshape(a, 13, -1, 12, 4)
  58. || test_reshape(a, -1, 26, 3, 16)
  59. || test_reshape(a, 13, 4, -233, -1)
  60. || test_reshape(a, 26, -1, -233, 12)
  61. || test_reshape(a, -1, 13, -233, 16)
  62. || test_reshape(a, 12, -1, -233, -233)
  63. || test_reshape(a, -1, 24, -233, -233)
  64. || test_reshape(a, -1, -233, -233, -233);
  65. }
  66. static int test_reshape_2()
  67. {
  68. ncnn::Mat a = RandomMat(6, 7, 32);
  69. return 0
  70. || test_reshape(a, 6, 8, 4, 7)
  71. || test_reshape(a, 2, 6, 7, 16)
  72. || test_reshape(a, 7, 6, -233, 32)
  73. || test_reshape(a, 6, 32, -233, 7)
  74. || test_reshape(a, 32, 7, -233, 6)
  75. || test_reshape(a, 2, 6, -233, -1)
  76. || test_reshape(a, -1, 8, -233, 2)
  77. || test_reshape(a, -1, 4, -233, -233)
  78. || test_reshape(a, 8, -1, -233, -233)
  79. || test_reshape(a, 32, 42, -233, -233)
  80. || test_reshape(a, -1, -233, -233, -233);
  81. }
  82. static int test_reshape_3()
  83. {
  84. ncnn::Mat a = RandomMat(4, 14, 26);
  85. return 0
  86. || test_reshape(a, 13, 4, 2, 14)
  87. || test_reshape(a, 1, 13, 7, 16)
  88. || test_reshape(a, 14, 8, -233, 13)
  89. || test_reshape(a, 4, 13, -233, 28)
  90. || test_reshape(a, 13, 14, -233, 8)
  91. || test_reshape(a, 2, 7, -233, -1)
  92. || test_reshape(a, -1, 13, -233, 2)
  93. || test_reshape(a, -1, 4, -233, -233)
  94. || test_reshape(a, 8, -1, -233, -233)
  95. || test_reshape(a, 16, 91, -233, -233)
  96. || test_reshape(a, -1, -233, -233, -233);
  97. }
  98. static int test_reshape_4()
  99. {
  100. ncnn::Mat a = RandomMat(14, 32);
  101. return 0
  102. || test_reshape(a, 2, 7, 2, 16)
  103. || test_reshape(a, 16, 1, 7, 4)
  104. || test_reshape(a, 7, 2, -233, 32)
  105. || test_reshape(a, 4, 16, -233, 7)
  106. || test_reshape(a, 16, 14, -233, 2)
  107. || test_reshape(a, 2, 4, -233, -1)
  108. || test_reshape(a, -1, 8, -233, 2)
  109. || test_reshape(a, 28, 16, -233, -233)
  110. || test_reshape(a, -1, 14, -233, -233)
  111. || test_reshape(a, 16, -1, -233, -233)
  112. || test_reshape(a, -1, -233, -233, -233);
  113. }
  114. static int test_reshape_5()
  115. {
  116. ncnn::Mat a = RandomMat(12, 28);
  117. return 0
  118. || test_reshape(a, 4, 3, 2, 14)
  119. || test_reshape(a, 1, 3, 14, 8)
  120. || test_reshape(a, 7, 2, -233, 24)
  121. || test_reshape(a, 2, 24, -233, 7)
  122. || test_reshape(a, 12, 14, -233, 2)
  123. || test_reshape(a, 2, 4, -233, -1)
  124. || test_reshape(a, -1, 4, -233, 2)
  125. || test_reshape(a, 21, 16, -233, -233)
  126. || test_reshape(a, -1, 14, -233, -233)
  127. || test_reshape(a, 3, -1, -233, -233)
  128. || test_reshape(a, -1, -233, -233, -233);
  129. }
  130. static int test_reshape_6()
  131. {
  132. ncnn::Mat a = RandomMat(240);
  133. return 0
  134. || test_reshape(a, 1, 1, 1, 240)
  135. || test_reshape(a, 10, 1, 1, 24)
  136. || test_reshape(a, 3, 5, -233, 16)
  137. || test_reshape(a, 3, 8, -233, 10)
  138. || test_reshape(a, 8, 5, -233, 6)
  139. || test_reshape(a, 2, 5, -233, -1)
  140. || test_reshape(a, -1, 5, -233, 4)
  141. || test_reshape(a, 8, 30, -233, -233)
  142. || test_reshape(a, -1, 2, -233, -233)
  143. || test_reshape(a, 24, -1, -233, -233)
  144. || test_reshape(a, -1, -233, -233, -233);
  145. }
  146. static int test_reshape_7()
  147. {
  148. ncnn::Mat a = RandomMat(210);
  149. return 0
  150. || test_reshape(a, 1, 1, 210, 1)
  151. || test_reshape(a, 5, 2, 7, 3)
  152. || test_reshape(a, 3, 5, -233, 14)
  153. || test_reshape(a, 3, 14, -233, 5)
  154. || test_reshape(a, 14, 5, -233, 3)
  155. || test_reshape(a, 2, 5, -233, -1)
  156. || test_reshape(a, -1, 5, -233, 2)
  157. || test_reshape(a, 6, 35, -233, -233)
  158. || test_reshape(a, -1, 7, -233, -233)
  159. || test_reshape(a, 21, -1, -233, -233)
  160. || test_reshape(a, -1, -233, -233, -233);
  161. }
  162. static int test_reshape_8()
  163. {
  164. ncnn::Mat a = RandomMat(1, 19, 15, 18);
  165. return test_reshape(a, 19, 15, -233, 18);
  166. }
  167. int main()
  168. {
  169. SRAND(7767517);
  170. return 0
  171. || test_reshape_0()
  172. || test_reshape_1()
  173. || test_reshape_2()
  174. || test_reshape_3()
  175. || test_reshape_4()
  176. || test_reshape_5()
  177. || test_reshape_6()
  178. || test_reshape_7()
  179. || test_reshape_8();
  180. }