Elffuss BraidHub

kikocis/ncnn public

master · 2.3 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2018 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 "reorg.h"
16
17namespace ncnn {
18
19Reorg::Reorg()
20{
21 one_blob_only = true;
22 support_inplace = false;
23}
24
25int Reorg::load_param(const ParamDict& pd)
26{
27 stride = pd.get(0, 1);
28 mode = pd.get(1, 0);
29
30 return 0;
31}
32
33int Reorg::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 int outw = w / stride;
41 int outh = h / stride;
42 int outc = channels * stride * stride;
43
44 top_blob.create(outw, outh, outc, elemsize, opt.blob_allocator);
45 if (top_blob.empty())
46 return -100;
47
48 #pragma omp parallel for num_threads(opt.num_threads)
49 for (int q = 0; q < channels; q++)
50 {
51 const Mat m = bottom_blob.channel(q);
52
53 for (int sh = 0; sh < stride; sh++)
54 {
55 for (int sw = 0; sw < stride; sw++)
56 {
57 int p;
58 if (mode == 0)
59 p = q * stride * stride + sh * stride + sw;
60 else // if (mode == 1)
61 p = (sh * stride + sw) * channels + q;
62
63 float* outptr = top_blob.channel(p);
64
65 for (int i = 0; i < outh; i++)
66 {
67 const float* sptr = m.row(i * stride + sh) + sw;
68 for (int j = 0; j < outw; j++)
69 {
70 outptr[0] = sptr[0];
71
72 sptr += stride;
73 outptr++;
74 }
75 }
76 }
77 }
78 }
79
80 return 0;
81}
82
83} // namespace ncnn