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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "layer/expanddims.h"
  15. #include "testutil.h"
  16. static int test_expanddims(const ncnn::Mat& a, int expand_w, int expand_h, int expand_c)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, expand_w);
  20. pd.set(1, expand_h);
  21. pd.set(2, expand_c);
  22. std::vector<ncnn::Mat> weights(0);
  23. int ret = test_layer<ncnn::ExpandDims>("ExpandDims", pd, weights, a);
  24. if (ret != 0)
  25. {
  26. fprintf(stderr, "test_expanddims failed a.dims=%d a=(%d %d %d) expand_w=%d expand_h=%d expand_c=%d\n", a.dims, a.w, a.h, a.c, expand_w, expand_h, expand_c);
  27. }
  28. return ret;
  29. }
  30. static ncnn::Mat IntArrayMat(int a0)
  31. {
  32. ncnn::Mat m(1);
  33. int* p = m;
  34. p[0] = a0;
  35. return m;
  36. }
  37. static ncnn::Mat IntArrayMat(int a0, int a1)
  38. {
  39. ncnn::Mat m(2);
  40. int* p = m;
  41. p[0] = a0;
  42. p[1] = a1;
  43. return m;
  44. }
  45. static ncnn::Mat IntArrayMat(int a0, int a1, int a2)
  46. {
  47. ncnn::Mat m(3);
  48. int* p = m;
  49. p[0] = a0;
  50. p[1] = a1;
  51. p[2] = a2;
  52. return m;
  53. }
  54. static void print_int_array(const ncnn::Mat& a)
  55. {
  56. const int* pa = a;
  57. fprintf(stderr, "[");
  58. for (int i = 0; i < a.w; i++)
  59. {
  60. fprintf(stderr, " %d", pa[i]);
  61. }
  62. fprintf(stderr, " ]");
  63. }
  64. static int test_expanddims_axes(const ncnn::Mat& a, const ncnn::Mat& axes)
  65. {
  66. ncnn::ParamDict pd;
  67. pd.set(3, axes);
  68. std::vector<ncnn::Mat> weights(0);
  69. int ret = test_layer<ncnn::ExpandDims>("ExpandDims", pd, weights, a);
  70. if (ret != 0)
  71. {
  72. fprintf(stderr, "test_expanddims_axes failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c);
  73. fprintf(stderr, " axes=");
  74. print_int_array(axes);
  75. fprintf(stderr, "\n");
  76. }
  77. return ret;
  78. }
  79. static int test_expand_0()
  80. {
  81. ncnn::Mat as[7];
  82. as[0] = RandomMat(1, 1, 1);
  83. as[1] = RandomMat(14, 16);
  84. as[2] = RandomMat(1, 14);
  85. as[3] = RandomMat(11, 1);
  86. as[4] = RandomMat(1, 1);
  87. as[5] = RandomMat(120);
  88. as[6] = RandomMat(1);
  89. for (int i = 0; i < 7; i++)
  90. {
  91. const ncnn::Mat& a = as[i];
  92. int ret = 0
  93. || test_expanddims(a, 0, 0, 0)
  94. || test_expanddims(a, 0, 0, 1)
  95. || test_expanddims(a, 0, 1, 0)
  96. || test_expanddims(a, 0, 1, 1)
  97. || test_expanddims(a, 1, 0, 0)
  98. || test_expanddims(a, 1, 0, 1)
  99. || test_expanddims(a, 1, 1, 0)
  100. || test_expanddims(a, 1, 1, 1)
  101. || test_expanddims_axes(a, IntArrayMat(0))
  102. || test_expanddims_axes(a, IntArrayMat(1))
  103. || test_expanddims_axes(a, IntArrayMat(2))
  104. || test_expanddims_axes(a, IntArrayMat(0, 1))
  105. || test_expanddims_axes(a, IntArrayMat(0, 2))
  106. || test_expanddims_axes(a, IntArrayMat(1, 2))
  107. || test_expanddims_axes(a, IntArrayMat(0, 1, 2));
  108. if (ret != 0)
  109. return ret;
  110. }
  111. return 0;
  112. }
  113. int main()
  114. {
  115. SRAND(7767517);
  116. return test_expand_0();
  117. }