Elffuss BraidHub

kikocis/ncnn public

master · 4.6 KiBRawPermalink
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 "mvn.h"
16
17namespace ncnn {
18
19MVN::MVN()
20{
21 one_blob_only = true;
22 support_inplace = false;
23}
24
25int MVN::load_param(const ParamDict& pd)
26{
27 normalize_variance = pd.get(0, 0);
28 across_channels = pd.get(1, 0);
29 eps = pd.get(2, 0.0001f);
30
31 return 0;
32}
33
34int MVN::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 size_t elemsize = bottom_blob.elemsize;
40 int size = w * h;
41
42 top_blob.create(w, h, channels, elemsize, opt.blob_allocator);
43 if (top_blob.empty())
44 return -100;
45
46 // prepare sum per channel
47 Mat sum(channels, elemsize, opt.workspace_allocator);
48 if (sum.empty())
49 return -100;
50
51 #pragma omp parallel for num_threads(opt.num_threads)
52 for (int q = 0; q < channels; q++)
53 {
54 const float* ptr = bottom_blob.channel(q);
55
56 float s = 0.f;
57 for (int i = 0; i < size; i++)
58 {
59 s += ptr[i];
60 }
61
62 sum[q] = s;
63 }
64
65 if (across_channels)
66 {
67 // compute mean across channels
68 float mean = 0.f;
69 for (int q = 0; q < channels; q++)
70 {
71 mean += sum[q];
72 }
73 mean = mean / (channels * size);
74
75 // subtract mean
76 #pragma omp parallel for num_threads(opt.num_threads)
77 for (int q = 0; q < channels; q++)
78 {
79 const float* ptr = bottom_blob.channel(q);
80 float* outptr = top_blob.channel(q);
81
82 for (int i = 0; i < size; i++)
83 {
84 outptr[i] = ptr[i] - mean;
85 }
86 }
87 }
88 else
89 {
90 // subtract mean
91 #pragma omp parallel for num_threads(opt.num_threads)
92 for (int q = 0; q < channels; q++)
93 {
94 const float* ptr = bottom_blob.channel(q);
95 float* outptr = top_blob.channel(q);
96 float mean = sum[q] / size;
97
98 for (int i = 0; i < size; i++)
99 {
100 outptr[i] = ptr[i] - mean;
101 }
102 }
103 }
104
105 if (normalize_variance)
106 {
107 // prepare squared sum per channel
108 Mat sqsum(channels, elemsize, opt.workspace_allocator);
109 if (sqsum.empty())
110 return -100;
111
112 #pragma omp parallel for num_threads(opt.num_threads)
113 for (int q = 0; q < channels; q++)
114 {
115 const float* ptr = top_blob.channel(q);
116
117 float s = 0.f;
118 for (int i = 0; i < size; i++)
119 {
120 s += ptr[i] * ptr[i];
121 }
122
123 sqsum[q] = s;
124 }
125
126 if (across_channels)
127 {
128 // compute squared mean across channels
129 float sqmean = 0.f;
130 for (int q = 0; q < channels; q++)
131 {
132 sqmean += sqsum[q];
133 }
134 sqmean = sqmean / (channels * size);
135
136 // normalize variance
137 float norm_var = sqrtf(sqmean) + eps;
138 float norm_var_inv = 1.f / norm_var;
139
140 // apply normalize_variance
141 #pragma omp parallel for num_threads(opt.num_threads)
142 for (int q = 0; q < channels; q++)
143 {
144 float* outptr = top_blob.channel(q);
145
146 for (int i = 0; i < size; i++)
147 {
148 outptr[i] = outptr[i] * norm_var_inv;
149 }
150 }
151 }
152 else
153 {
154 // apply normalize_variance
155 #pragma omp parallel for num_threads(opt.num_threads)
156 for (int q = 0; q < channels; q++)
157 {
158 float* outptr = top_blob.channel(q);
159 float sqmean = sqsum[q] / size;
160 float norm_var = sqrt(sqmean) + eps;
161 float norm_var_inv = 1.f / norm_var;
162
163 for (int i = 0; i < size; i++)
164 {
165 outptr[i] = outptr[i] * norm_var_inv;
166 }
167 }
168 }
169 }
170
171 return 0;
172}
173
174} // namespace ncnn