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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. DEFINE_LAYER_CREATOR(ExpandDims)
  17. ExpandDims::ExpandDims()
  18. {
  19. one_blob_only = true;
  20. support_inplace = false;
  21. }
  22. int ExpandDims::load_param(const ParamDict& pd)
  23. {
  24. expand_w = pd.get(0, 0);
  25. expand_h = pd.get(1, 0);
  26. expand_c = pd.get(2, 0);
  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. top_blob = bottom_blob;
  35. if (dims == 1)
  36. {
  37. if (expand_w)
  38. {
  39. if (expand_h)
  40. top_blob = bottom_blob.reshape(1, 1, w, opt.blob_allocator);
  41. else if (expand_c)
  42. top_blob = bottom_blob.reshape(1, w, 1, opt.blob_allocator);
  43. else
  44. top_blob = bottom_blob.reshape(1, w, opt.blob_allocator);
  45. }
  46. else if (expand_h)
  47. {
  48. if (expand_c)
  49. top_blob = bottom_blob.reshape(w, 1, 1, opt.blob_allocator);
  50. else
  51. top_blob = bottom_blob.reshape(w, 1, opt.blob_allocator);
  52. }
  53. }
  54. else if (dims == 2)
  55. {
  56. if (expand_w)
  57. top_blob = bottom_blob.reshape(1, w, h, opt.blob_allocator);
  58. else if (expand_h)
  59. top_blob = bottom_blob.reshape(w, 1, h, opt.blob_allocator);
  60. else if (expand_c)
  61. top_blob = bottom_blob.reshape(w, h, 1, opt.blob_allocator);
  62. }
  63. if (top_blob.empty())
  64. return -100;
  65. return 0;
  66. }
  67. } // namespace ncnn