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