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.

expanddims.cpp 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 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 "expanddims.h"
  15. namespace ncnn {
  16. ExpandDims::ExpandDims()
  17. {
  18. one_blob_only = true;
  19. support_inplace = false;
  20. }
  21. int ExpandDims::load_param(const ParamDict& pd)
  22. {
  23. expand_w = pd.get(0, 0);
  24. expand_h = pd.get(1, 0);
  25. expand_c = pd.get(2, 0);
  26. axes = pd.get(3, Mat());
  27. return 0;
  28. }
  29. int ExpandDims::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  30. {
  31. int w = bottom_blob.w;
  32. int h = bottom_blob.h;
  33. int dims = bottom_blob.dims;
  34. bool _expand_w = false;
  35. bool _expand_h = false;
  36. bool _expand_c = false;
  37. if (axes.empty())
  38. {
  39. _expand_w = expand_w;
  40. _expand_h = expand_h;
  41. _expand_c = expand_c;
  42. }
  43. else
  44. {
  45. const int* axes_ptr = axes;
  46. for (int i = 0; i < axes.w; i++)
  47. {
  48. int axis = axes_ptr[i];
  49. if (axis < 0)
  50. axis = dims + 1 + axis; // +1 for N-dim
  51. if (dims == 1 && axis == 1)
  52. {
  53. _expand_h = true;
  54. }
  55. if (dims == 1 && axis == 2)
  56. {
  57. _expand_w = true;
  58. }
  59. if (dims == 2 && axis == 1)
  60. {
  61. _expand_c = true;
  62. }
  63. if (dims == 2 && axis == 2)
  64. {
  65. _expand_h = true;
  66. }
  67. if (dims == 2 && axis == 3)
  68. {
  69. _expand_w = true;
  70. }
  71. }
  72. }
  73. top_blob = bottom_blob;
  74. if (dims == 1)
  75. {
  76. if (_expand_w && _expand_h)
  77. {
  78. top_blob = bottom_blob.reshape(1, w, 1, opt.blob_allocator);
  79. }
  80. else if (_expand_w)
  81. {
  82. top_blob = bottom_blob.reshape(1, w, opt.blob_allocator);
  83. }
  84. else if (_expand_h)
  85. {
  86. top_blob = bottom_blob.reshape(w, 1, opt.blob_allocator);
  87. }
  88. }
  89. if (dims == 2)
  90. {
  91. if (_expand_w)
  92. {
  93. top_blob = bottom_blob.reshape(1, w, h, opt.blob_allocator);
  94. }
  95. else if (_expand_h)
  96. {
  97. top_blob = bottom_blob.reshape(w, 1, h, opt.blob_allocator);
  98. }
  99. else if (_expand_c)
  100. {
  101. top_blob = bottom_blob.reshape(w, h, 1, opt.blob_allocator);
  102. }
  103. }
  104. if (top_blob.empty())
  105. return -100;
  106. return 0;
  107. }
  108. } // namespace ncnn