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.

shufflechannel.cpp 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "shufflechannel.h"
  15. namespace ncnn {
  16. ShuffleChannel::ShuffleChannel()
  17. {
  18. one_blob_only = true;
  19. support_inplace = false;
  20. }
  21. int ShuffleChannel::load_param(const ParamDict& pd)
  22. {
  23. group = pd.get(0, 1);
  24. reverse = pd.get(1, 0);
  25. return 0;
  26. }
  27. int ShuffleChannel::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  28. {
  29. int w = bottom_blob.w;
  30. int h = bottom_blob.h;
  31. int channels = bottom_blob.c;
  32. size_t elemsize = bottom_blob.elemsize;
  33. if (channels % group != 0)
  34. {
  35. // reject invalid group
  36. return -100;
  37. }
  38. int _group = reverse ? channels / group : group;
  39. int channels_per_group = channels / _group;
  40. top_blob.create(w, h, channels, elemsize, opt.blob_allocator);
  41. if (top_blob.empty())
  42. return -100;
  43. const size_t feature_sz = w * h * elemsize;
  44. for (int i = 0; i < _group; i++)
  45. {
  46. for (int j = 0; j < channels_per_group; j++)
  47. {
  48. int src_q = channels_per_group * i + j;
  49. int dst_q = _group * j + i;
  50. memcpy(top_blob.channel(dst_q), bottom_blob.channel(src_q), feature_sz);
  51. }
  52. }
  53. return 0;
  54. }
  55. } // namespace ncnn