1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
22
support_inplace = true;
25
int BatchNorm::load_param(const ParamDict& pd)
27
channels = pd.get(0, 0);
33
int BatchNorm::load_model(const ModelBin& mb)
35
slope_data = mb.load(channels, 1);
36
if (slope_data.empty())
39
mean_data = mb.load(channels, 1);
40
if (mean_data.empty())
43
var_data = mb.load(channels, 1);
47
bias_data = mb.load(channels, 1);
48
if (bias_data.empty())
51
a_data.create(channels);
54
b_data.create(channels);
58
for (int i = 0; i < channels; i++)
60
float sqrt_var = sqrtf(var_data[i] + eps);
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;
70
int BatchNorm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
72
// a = bias - slope * mean / sqrt(var)
73
// b = slope / sqrt(var)
74
// value = b * value + a
76
int dims = bottom_top_blob.dims;
80
int w = bottom_top_blob.w;
82
float* ptr = bottom_top_blob;
84
#pragma omp parallel for num_threads(opt.num_threads)
85
for (int i = 0; i < w; i++)
87
ptr[i] = b_data[i] * ptr[i] + a_data[i];
93
int w = bottom_top_blob.w;
94
int h = bottom_top_blob.h;
96
#pragma omp parallel for num_threads(opt.num_threads)
97
for (int i = 0; i < h; i++)
99
float* ptr = bottom_top_blob.row(i);
103
for (int j = 0; j < w; j++)
105
ptr[j] = b * ptr[j] + a;
110
if (dims == 3 || dims == 4)
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;
118
#pragma omp parallel for num_threads(opt.num_threads)
119
for (int q = 0; q < c; q++)
121
float* ptr = bottom_top_blob.channel(q);
125
for (int i = 0; i < size; i++)
127
ptr[i] = b * ptr[i] + a;