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 Scale::load_param(const ParamDict& pd)
27
scale_data_size = pd.get(0, 0);
28
bias_term = pd.get(1, 0);
30
if (scale_data_size == -233)
31
one_blob_only = false;
36
int Scale::load_model(const ModelBin& mb)
38
if (scale_data_size == -233)
41
scale_data = mb.load(scale_data_size, 1);
42
if (scale_data.empty())
47
bias_data = mb.load(scale_data_size, 1);
48
if (bias_data.empty())
55
int Scale::forward_inplace(std::vector<Mat>& bottom_top_blobs, const Option& opt) const
57
Mat& bottom_top_blob = bottom_top_blobs[0];
58
const Mat& scale_blob = bottom_top_blobs[1];
60
int dims = bottom_top_blob.dims;
64
int w = bottom_top_blob.w;
66
float* ptr = bottom_top_blob;
70
#pragma omp parallel for num_threads(opt.num_threads)
71
for (int i = 0; i < w; i++)
73
ptr[i] = ptr[i] * scale_blob[i] + bias_data[i];
78
#pragma omp parallel for num_threads(opt.num_threads)
79
for (int i = 0; i < w; i++)
81
ptr[i] *= scale_blob[i];
88
int w = bottom_top_blob.w;
89
int h = bottom_top_blob.h;
93
#pragma omp parallel for num_threads(opt.num_threads)
94
for (int i = 0; i < h; i++)
96
float* ptr = bottom_top_blob.row(i);
97
float s = scale_blob[i];
98
float bias = bias_data[i];
100
for (int j = 0; j < w; j++)
102
ptr[j] = ptr[j] * s + bias;
108
#pragma omp parallel for num_threads(opt.num_threads)
109
for (int i = 0; i < h; i++)
111
float* ptr = bottom_top_blob.row(i);
112
float s = scale_blob[i];
114
for (int j = 0; j < w; j++)
124
int w = bottom_top_blob.w;
125
int h = bottom_top_blob.h;
126
int channels = bottom_top_blob.c;
131
#pragma omp parallel for num_threads(opt.num_threads)
132
for (int q = 0; q < channels; q++)
134
float* ptr = bottom_top_blob.channel(q);
136
float s = scale_blob[q];
137
float bias = bias_data[q];
139
for (int i = 0; i < size; i++)
141
ptr[i] = ptr[i] * s + bias;
147
#pragma omp parallel for num_threads(opt.num_threads)
148
for (int q = 0; q < channels; q++)
150
float* ptr = bottom_top_blob.channel(q);
152
float s = scale_blob[q];
154
for (int i = 0; i < size; i++)
165
int Scale::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
167
std::vector<Mat> bottom_top_blobs(2);
168
bottom_top_blobs[0] = bottom_top_blob;
169
bottom_top_blobs[1] = scale_data;
171
return forward_inplace(bottom_top_blobs, opt);