Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 3.3 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 "batchnorm.h"
16
17namespace ncnn {
18
19BatchNorm::BatchNorm()
20{
21 one_blob_only = true;
22 support_inplace = true;
23}
24
25int BatchNorm::load_param(const ParamDict& pd)
26{
27 channels = pd.get(0, 0);
28 eps = pd.get(1, 0.f);
29
30 return 0;
31}
32
33int BatchNorm::load_model(const ModelBin& mb)
34{
35 slope_data = mb.load(channels, 1);
36 if (slope_data.empty())
37 return -100;
38
39 mean_data = mb.load(channels, 1);
40 if (mean_data.empty())
41 return -100;
42
43 var_data = mb.load(channels, 1);
44 if (var_data.empty())
45 return -100;
46
47 bias_data = mb.load(channels, 1);
48 if (bias_data.empty())
49 return -100;
50
51 a_data.create(channels);
52 if (a_data.empty())
53 return -100;
54 b_data.create(channels);
55 if (b_data.empty())
56 return -100;
57
58 for (int i = 0; i < channels; i++)
59 {
60 float sqrt_var = sqrtf(var_data[i] + eps);
61 if (sqrt_var == 0.f)
62 sqrt_var = 0.0001f; // sanitize divide by zero
63 a_data[i] = bias_data[i] - slope_data[i] * mean_data[i] / sqrt_var;
64 b_data[i] = slope_data[i] / sqrt_var;
65 }
66
67 return 0;
68}
69
70int BatchNorm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
71{
72 // a = bias - slope * mean / sqrt(var)
73 // b = slope / sqrt(var)
74 // value = b * value + a
75
76 int dims = bottom_top_blob.dims;
77
78 if (dims == 1)
79 {
80 int w = bottom_top_blob.w;
81
82 float* ptr = bottom_top_blob;
83
84 #pragma omp parallel for num_threads(opt.num_threads)
85 for (int i = 0; i < w; i++)
86 {
87 ptr[i] = b_data[i] * ptr[i] + a_data[i];
88 }
89 }
90
91 if (dims == 2)
92 {
93 int w = bottom_top_blob.w;
94 int h = bottom_top_blob.h;
95
96 #pragma omp parallel for num_threads(opt.num_threads)
97 for (int i = 0; i < h; i++)
98 {
99 float* ptr = bottom_top_blob.row(i);
100 float a = a_data[i];
101 float b = b_data[i];
102
103 for (int j = 0; j < w; j++)
104 {
105 ptr[j] = b * ptr[j] + a;
106 }
107 }
108 }
109
110 if (dims == 3 || dims == 4)
111 {
112 int w = bottom_top_blob.w;
113 int h = bottom_top_blob.h;
114 int d = bottom_top_blob.d;
115 int c = bottom_top_blob.c;
116 int size = w * h * d;
117
118 #pragma omp parallel for num_threads(opt.num_threads)
119 for (int q = 0; q < c; q++)
120 {
121 float* ptr = bottom_top_blob.channel(q);
122 float a = a_data[q];
123 float b = b_data[q];
124
125 for (int i = 0; i < size; i++)
126 {
127 ptr[i] = b * ptr[i] + a;
128 }
129 }
130 }
131
132 return 0;
133}
134
135} // namespace ncnn