ONE - On-device Neural Engine
Loading...
Searching...
No Matches
InstanceNorm.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __NNFW_CKER_INSTANCE_NORM_H__
18#define __NNFW_CKER_INSTANCE_NORM_H__
19
20#include "cker/Shape.h"
21#include "cker/Types.h"
22#include "cker/Utils.h"
23
24#include <cmath>
25
26namespace nnfw
27{
28namespace cker
29{
30
31inline void InstanceNorm(const InstanceNormParams &params, const Shape &input_shape,
32 const float *input_data, [[maybe_unused]] const Shape &gamma_shape,
33 const float *gamma_data, [[maybe_unused]] const Shape &beta_shape,
34 const float *beta_data, const Shape &output_shape, float *output_data)
35{
36 const int32_t batches = MatchingDim(input_shape, 0, output_shape, 0);
37 const int32_t heights = MatchingDim(input_shape, 1, output_shape, 1);
38 const int32_t widths = MatchingDim(input_shape, 2, output_shape, 2);
39 const int32_t channels = MatchingDim(input_shape, 3, output_shape, 3);
40 const float output_activation_min = params.float_activation_min;
41 const float output_activation_max = params.float_activation_max;
42
43 assert(output_activation_min <= output_activation_max);
44
45 for (int32_t batch = 0; batch < batches; batch++)
46 {
47 for (int32_t channel = 0; channel < channels; channel++)
48 {
49 double sum = 0.0f;
50 double square_sum = 0.0f;
51 int32_t size = heights * widths;
52
53 for (int32_t height = 0; height < heights; height++)
54 {
55 for (int32_t width = 0; width < widths; width++)
56 {
57 double input_val = input_data[Offset(input_shape, batch, height, width, channel)];
58 sum += input_val;
59 square_sum += (input_val * input_val);
60 }
61 }
62
63 double mean = sum / size;
64 double var = square_sum / size - mean * mean;
65
66 double gamma = gamma_data[channel];
67 double beta = beta_data[channel];
68
69 double a = gamma / (std::sqrt(var + params.epsilon));
70 double b = -mean * a + beta;
71
72 for (int32_t height = 0; height < heights; height++)
73 {
74 for (int32_t width = 0; width < widths; width++)
75 {
76 double input_value = input_data[Offset(output_shape, batch, height, width, channel)];
77 double output_value = input_value * a + b;
78 output_data[Offset(output_shape, batch, height, width, channel)] =
79 ActivationFunctionWithMinMax((float)output_value, output_activation_min,
80 output_activation_max);
81 }
82 }
83 }
84 }
85}
86
87} // namespace cker
88} // namespace nnfw
89
90#endif // __NNFW_CKER_INSTANCE_NORM_H__
const luci_interpreter::RuntimeShape output_shape
int MatchingDim(const Shape &shape1, int index1, const Shape &shape2, int index2)
Definition Shape.h:220
int Offset(const Shape &shape, int i0, int i1, int i2, int i3)
Definition Shape.h:237
T ActivationFunctionWithMinMax(T x, T output_activation_min, T output_activation_max)
Definition Utils.h:43
void InstanceNorm(const InstanceNormParams &params, const Shape &input_shape, const float *input_data, const Shape &gamma_shape, const float *gamma_data, const Shape &beta_shape, const float *beta_data, const Shape &output_shape, float *output_data)
Definition topk_v2.h:30
int32_t size[5]
Definition Slice.cpp:35