2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "kernels/InstanceNorm.h"
20 #include "kernels/Utils.h"
22 #include <tensorflow/lite/kernels/internal/common.h>
25 namespace luci_interpreter
30 InstanceNorm::InstanceNorm(const Tensor *input, const Tensor *gamma, const Tensor *beta,
31 Tensor *output, const InstanceNormParams ¶ms)
32 : KernelWithParams<InstanceNormParams>({input, gamma, beta}, {output}, params)
36 void InstanceNorm::configure()
38 LUCI_INTERPRETER_CHECK(input()->shape().num_dims() == 4);
39 LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
40 LUCI_INTERPRETER_CHECK(gamma()->element_type() == input()->element_type());
41 LUCI_INTERPRETER_CHECK(gamma()->shape().num_dims() == 1);
42 LUCI_INTERPRETER_CHECK(gamma()->shape().dim(0) == input()->shape().dim(3) ||
43 gamma()->shape().dim(0) == 1);
44 LUCI_INTERPRETER_CHECK(beta()->element_type() == input()->element_type());
45 LUCI_INTERPRETER_CHECK(beta()->shape().num_dims() == 1);
46 LUCI_INTERPRETER_CHECK(beta()->shape().dim(0) == input()->shape().dim(3) ||
47 beta()->shape().dim(0) == 1);
48 // TODO: enable it only if kernel with dynamic shapes
49 output()->resize(input()->shape());
52 void InstanceNorm::execute() const
54 switch (input()->element_type())
56 case DataType::FLOAT32:
60 assert(false && "Unsupported type.");
64 void InstanceNorm::evalFloat() const
66 float activation_min, activation_max;
67 calculateActivationRange(params().activation, &activation_min, &activation_max);
68 auto input_shape = getTensorShape(input());
69 auto output_shape = getTensorShape(output());
70 const int32_t batches = tflite::MatchingDim(input_shape, 0, output_shape, 0);
71 const int32_t heights = tflite::MatchingDim(input_shape, 1, output_shape, 1);
72 const int32_t widths = tflite::MatchingDim(input_shape, 2, output_shape, 2);
73 const int32_t channels = tflite::MatchingDim(input_shape, 3, output_shape, 3);
74 const float *input_data = getTensorData<float>(input());
75 const float *gamma_data = getTensorData<float>(gamma());
76 auto gamma_shape = getTensorShape(gamma());
77 bool single_gamma = gamma_shape.DimensionsCount() == 1 && gamma_shape.Dims(0) == 1;
78 const float *beta_data = getTensorData<float>(beta());
79 auto beta_shape = getTensorShape(beta());
80 bool single_beta = beta_shape.DimensionsCount() == 1 && beta_shape.Dims(0) == 1;
81 float *output_data = getTensorData<float>(output());
82 for (int32_t batch = 0; batch < batches; batch++)
84 for (int32_t channel = 0; channel < channels; channel++)
87 double square_sum = 0.0f;
88 int32_t size = heights * widths;
89 for (int32_t height = 0; height < heights; height++)
91 for (int32_t width = 0; width < widths; width++)
93 double input_val = input_data[tflite::Offset(input_shape, batch, height, width, channel)];
95 square_sum += (input_val * input_val);
98 double mean = sum / size;
99 double var = square_sum / size - mean * mean;
101 double gamma = single_gamma ? gamma_data[0] : gamma_data[channel];
102 double beta = single_beta ? beta_data[0] : beta_data[channel];
103 double a = gamma / (std::sqrt(var + params().epsilon));
104 double b = -mean * a + beta;
106 for (int32_t height = 0; height < heights; height++)
108 for (int32_t width = 0; width < widths; width++)
111 input_data[tflite::Offset(output_shape, batch, height, width, channel)];
112 double output_value = input_value * a + b;
113 output_data[tflite::Offset(output_shape, batch, height, width, channel)] =
114 tflite::ActivationFunctionWithMinMax((float)output_value, activation_min,
122 } // namespace kernels
123 } // namespace luci_interpreter