Elffuss BraidHub

kikocis/ncnn public

master · 2.0 KiBRawPermalink
1// Copyright 2016 SoundAI Technology Co., Ltd. (author: Charles Wang)
2//
3// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
4// in compliance with the License. You may obtain a copy of the License at
5//
6// https://opensource.org/licenses/BSD-3-Clause
7//
8// Unless required by applicable law or agreed to in writing, software distributed
9// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10// CONDITIONS OF ANY KIND, either express or implied. See the License for the
11// specific language governing permissions and limitations under the License.
12
13#include "statisticspooling.h"
14
15#include <float.h>
16#include <limits.h>
17
18namespace ncnn {
19
20StatisticsPooling::StatisticsPooling()
21{
22 one_blob_only = true;
23 support_inplace = false;
24}
25
26int StatisticsPooling::load_param(const ParamDict& pd)
27{
28 include_stddev = pd.get(0, 0);
29
30 return 0;
31}
32
33int StatisticsPooling::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 int size = w * h;
39 size_t elemsize = bottom_blob.elemsize;
40
41 int out_channels = channels;
42 if (include_stddev)
43 {
44 out_channels *= 2;
45 }
46
47 top_blob.create(out_channels, elemsize, opt.blob_allocator);
48
49 #pragma omp parallel for num_threads(opt.num_threads)
50 for (int q = 0; q < channels; q++)
51 {
52 const float* ptr = bottom_blob.channel(q);
53
54 float mean = 0.f;
55 for (int i = 0; i < size; i++)
56 {
57 mean += ptr[i];
58 }
59 top_blob[q] = mean / w / h;
60 }
61
62 #pragma omp parallel for num_threads(opt.num_threads)
63 for (int q = channels; q < out_channels; q++)
64 {
65 const float* ptr = bottom_blob.channel(q - channels);
66
67 float std = 0.f;
68 for (int i = 0; i < size; i++)
69 {
70 std += powf((ptr[i] - top_blob[q - channels]), 2);
71 }
72 top_blob[q] = sqrtf(std / w / h);
73 }
74
75 return 0;
76}
77
78} // namespace ncnn