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.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. DEFINE_LAYER_CREATOR(ShuffleChannel)
  17. ShuffleChannel::ShuffleChannel()
  18. {
  19. one_blob_only = true;
  20. support_inplace = false;
  21. }
  22. int ShuffleChannel::load_param(const ParamDict& pd)
  23. {
  24. group = pd.get(0, 1);
  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 c = bottom_blob.c;
  32. size_t elemsize = bottom_blob.elemsize;
  33. int chs_per_group = c / group;
  34. if (c != chs_per_group * group)
  35. {
  36. // reject invalid group
  37. return -100;
  38. }
  39. top_blob.create(w, h, c, elemsize, opt.blob_allocator);
  40. if (top_blob.empty())
  41. return -100;
  42. const size_t feature_sz = w * h * elemsize;
  43. for (int i = 0; i != group; i++)
  44. {
  45. for (int j = 0; j != chs_per_group; j++)
  46. {
  47. int src_q = chs_per_group * i + j;
  48. int dst_q = group * j + i;
  49. memcpy(top_blob.channel(dst_q), bottom_blob.channel(src_q), feature_sz);
  50. }
  51. }
  52. return 0;
  53. }
  54. } // namespace ncnn