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 #include <math.h>
18 
19 namespace ncnn {
20 
StatisticsPooling()21 StatisticsPooling::StatisticsPooling()
22 {
23     one_blob_only = true;
24     support_inplace = false;
25 }
26 
load_param(const ParamDict & pd)27 int StatisticsPooling::load_param(const ParamDict& pd)
28 {
29     include_stddev = pd.get(0, 0);
30 
31     return 0;
32 }
33 
forward(const Mat & bottom_blob,Mat & top_blob,const Option & opt) const34 int StatisticsPooling::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
35 {
36     int w = bottom_blob.w;
37     int h = bottom_blob.h;
38     int channels = bottom_blob.c;
39     int size = w * h;
40     size_t elemsize = bottom_blob.elemsize;
41 
42     int out_channels = channels;
43     if (include_stddev)
44     {
45         out_channels *= 2;
46     }
47 
48     top_blob.create(out_channels, elemsize, opt.blob_allocator);
49 
50     #pragma omp parallel for num_threads(opt.num_threads)
51     for (int q = 0; q < channels; q++)
52     {
53         const float* ptr = bottom_blob.channel(q);
54 
55         float mean = 0.f;
56         for (int i = 0; i < size; i++)
57         {
58             mean += ptr[i];
59         }
60         top_blob[q] = mean / w / h;
61     }
62 
63     #pragma omp parallel for num_threads(opt.num_threads)
64     for (int q = channels; q < out_channels; q++)
65     {
66         const float* ptr = bottom_blob.channel(q - channels);
67 
68         float std = 0.f;
69         for (int i = 0; i < size; i++)
70         {
71             std += pow((ptr[i] - top_blob[q - channels]), 2);
72         }
73         top_blob[q] = sqrt(std / w / h);
74     }
75 
76     return 0;
77 }
78 
79 } // namespace ncnn
80