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 4.7 kB

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