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_expanddims.cpp 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2021 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_expanddims(const ncnn::Mat& a, int expand_w, int expand_h, int expand_d, int expand_c)
  16. {
  17. ncnn::ParamDict pd;
  18. pd.set(0, expand_w);
  19. pd.set(1, expand_h);
  20. pd.set(11, expand_d);
  21. pd.set(2, expand_c);
  22. std::vector<ncnn::Mat> weights(0);
  23. int ret = test_layer("ExpandDims", pd, weights, a);
  24. if (ret != 0)
  25. {
  26. fprintf(stderr, "test_expanddims failed a.dims=%d a=(%d %d %d %d) expand_w=%d expand_h=%d expand_d=%d expand_c=%d\n", a.dims, a.w, a.h, a.d, a.c, expand_w, expand_h, expand_d, expand_c);
  27. }
  28. return ret;
  29. }
  30. static std::vector<int> IntArray(int a0)
  31. {
  32. std::vector<int> m(1);
  33. m[0] = a0;
  34. return m;
  35. }
  36. static std::vector<int> IntArray(int a0, int a1)
  37. {
  38. std::vector<int> m(2);
  39. m[0] = a0;
  40. m[1] = a1;
  41. return m;
  42. }
  43. static std::vector<int> IntArray(int a0, int a1, int a2)
  44. {
  45. std::vector<int> m(3);
  46. m[0] = a0;
  47. m[1] = a1;
  48. m[2] = a2;
  49. return m;
  50. }
  51. static std::vector<int> IntArray(int a0, int a1, int a2, int a3)
  52. {
  53. std::vector<int> m(4);
  54. m[0] = a0;
  55. m[1] = a1;
  56. m[2] = a2;
  57. m[3] = a3;
  58. return m;
  59. }
  60. static void print_int_array(const std::vector<int>& a)
  61. {
  62. fprintf(stderr, "[");
  63. for (size_t i = 0; i < a.size(); i++)
  64. {
  65. fprintf(stderr, " %d", a[i]);
  66. }
  67. fprintf(stderr, " ]");
  68. }
  69. static int test_expanddims_axes(const ncnn::Mat& a, const std::vector<int>& axes_array)
  70. {
  71. ncnn::Mat axes(axes_array.size());
  72. {
  73. int* p = axes;
  74. for (size_t i = 0; i < axes_array.size(); i++)
  75. {
  76. p[i] = axes_array[i];
  77. }
  78. }
  79. ncnn::ParamDict pd;
  80. pd.set(3, axes);
  81. std::vector<ncnn::Mat> weights(0);
  82. int ret = test_layer("ExpandDims", pd, weights, a);
  83. if (ret != 0)
  84. {
  85. fprintf(stderr, "test_expanddims_axes failed a.dims=%d a=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c);
  86. fprintf(stderr, " axes=");
  87. print_int_array(axes_array);
  88. fprintf(stderr, "\n");
  89. }
  90. return ret;
  91. }
  92. static int test_expanddims_all_params(const ncnn::Mat& a)
  93. {
  94. return 0
  95. || test_expanddims(a, 0, 0, 0, 0)
  96. || test_expanddims(a, 0, 0, 0, 1)
  97. || test_expanddims(a, 0, 0, 1, 0)
  98. || test_expanddims(a, 0, 0, 1, 1)
  99. || test_expanddims(a, 0, 1, 0, 0)
  100. || test_expanddims(a, 0, 1, 0, 1)
  101. || test_expanddims(a, 0, 1, 1, 0)
  102. || test_expanddims(a, 0, 1, 1, 1)
  103. || test_expanddims(a, 1, 0, 0, 0)
  104. || test_expanddims(a, 1, 0, 0, 1)
  105. || test_expanddims(a, 1, 0, 1, 0)
  106. || test_expanddims(a, 1, 0, 1, 1)
  107. || test_expanddims(a, 1, 1, 0, 0)
  108. || test_expanddims(a, 1, 1, 0, 1)
  109. || test_expanddims(a, 1, 1, 1, 0)
  110. || test_expanddims(a, 1, 1, 1, 1)
  111. || test_expanddims_axes(a, IntArray(0))
  112. || test_expanddims_axes(a, IntArray(1))
  113. || test_expanddims_axes(a, IntArray(2))
  114. || test_expanddims_axes(a, IntArray(3))
  115. || test_expanddims_axes(a, IntArray(0, 1))
  116. || test_expanddims_axes(a, IntArray(0, 2))
  117. || test_expanddims_axes(a, IntArray(0, 3))
  118. || test_expanddims_axes(a, IntArray(1, 2))
  119. || test_expanddims_axes(a, IntArray(1, 3))
  120. || test_expanddims_axes(a, IntArray(2, 3))
  121. || test_expanddims_axes(a, IntArray(0, 1, 2))
  122. || test_expanddims_axes(a, IntArray(0, 1, 3))
  123. || test_expanddims_axes(a, IntArray(0, 2, 3))
  124. || test_expanddims_axes(a, IntArray(1, 2, 3))
  125. || test_expanddims_axes(a, IntArray(0, 1, 2, 3));
  126. }
  127. static int test_expanddims_0()
  128. {
  129. return 0
  130. || test_expanddims_all_params(RandomMat(3, 12, 16))
  131. || test_expanddims_all_params(RandomMat(3, 1, 16))
  132. || test_expanddims_all_params(RandomMat(1, 33, 15))
  133. || test_expanddims_all_params(RandomMat(1, 14, 1))
  134. || test_expanddims_all_params(RandomMat(12, 13, 1))
  135. || test_expanddims_all_params(RandomMat(1, 1, 1));
  136. }
  137. static int test_expanddims_1()
  138. {
  139. return 0
  140. || test_expanddims_all_params(RandomMat(14, 16))
  141. || test_expanddims_all_params(RandomMat(1, 14))
  142. || test_expanddims_all_params(RandomMat(11, 1))
  143. || test_expanddims_all_params(RandomMat(1, 1));
  144. }
  145. static int test_expanddims_2()
  146. {
  147. return 0
  148. || test_expanddims_all_params(RandomMat(120))
  149. || test_expanddims_all_params(RandomMat(1));
  150. }
  151. int main()
  152. {
  153. SRAND(7767517);
  154. return test_expanddims_0() || test_expanddims_1() || test_expanddims_2();
  155. }