blob: 8a5757bc782faaaba6e86f9baeaeed27936f48c7 [file] [log] [blame]
Manuel Bottini769c6382019-08-22 13:13:48 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 Arm Limited.
Manuel Bottini769c6382019-08-22 13:13:48 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "InstanceNormalizationLayer.h"
25
26#include "tests/validation/Helpers.h"
27
28#include <algorithm>
29#include <cmath>
30
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
39template <typename T>
40SimpleTensor<T> instance_normalization(const SimpleTensor<T> &src, float gamma, float beta, float epsilon)
41{
42 SimpleTensor<T> dst{ src.shape(), src.data_type() };
43
44 //NCHW
45 const size_t w_size = src.shape()[0];
46 const size_t h_size = src.shape()[1];
47 const size_t c_size = src.shape()[2];
48 const size_t n_size = src.shape()[3];
Michalis Spyroud1d77222020-04-08 14:10:15 +010049#if defined(_OPENMP)
50 #pragma omp parallel for collapse(2)
51#endif /* _OPENMP */
Manuel Bottini769c6382019-08-22 13:13:48 +010052 for(size_t n_i = 0; n_i < n_size; ++n_i)
53 {
54 for(size_t c_i = 0; c_i < c_size; ++c_i)
55 {
Michalis Spyroud1d77222020-04-08 14:10:15 +010056 float sum_h_w = 0;
Manuel Bottini6665f822019-10-03 12:03:19 +010057 float sum_sq_h_w = 0;
Manuel Bottini769c6382019-08-22 13:13:48 +010058
Manuel Bottini769c6382019-08-22 13:13:48 +010059 for(size_t h_i = 0; h_i < h_size; ++h_i)
60 {
61 for(size_t w_i = 0; w_i < w_size; ++w_i)
62 {
Manuel Bottini6665f822019-10-03 12:03:19 +010063 float val = src[coord2index(src.shape(), Coordinates(w_i, h_i, c_i, n_i))];
64 sum_h_w += val;
Michalis Spyroud1d77222020-04-08 14:10:15 +010065 sum_sq_h_w += val * val;
Manuel Bottini769c6382019-08-22 13:13:48 +010066 }
67 }
Manuel Bottini6665f822019-10-03 12:03:19 +010068 //Compute mean
69 const float mean_h_w = sum_h_w / (h_size * w_size);
70 //Compute variance
Michalis Spyroud1d77222020-04-08 14:10:15 +010071 const float var_h_w = sum_sq_h_w / (h_size * w_size) - mean_h_w * mean_h_w;
72 ;
Manuel Bottini769c6382019-08-22 13:13:48 +010073
74 //Apply mean
75 for(size_t h_i = 0; h_i < h_size; ++h_i)
76 {
77 for(size_t w_i = 0; w_i < w_size; ++w_i)
78 {
79 //Compute output
80 size_t index = coord2index(src.shape(), Coordinates(w_i, h_i, c_i, n_i));
81 dst[index] = (src[index] - mean_h_w) * gamma / std::sqrt(var_h_w + epsilon) + beta;
82 }
83 }
84 }
85 }
86 return dst;
87}
88
89template SimpleTensor<float> instance_normalization(const SimpleTensor<float> &src, float gamma, float beta, float epsilon);
90template SimpleTensor<half> instance_normalization(const SimpleTensor<half> &src, float gamma, float beta, float epsilon);
91} // namespace reference
92} // namespace validation
93} // namespace test
94} // namespace arm_compute