blob: ad0ac1be689efdda06f9bcc442abacfd2f767053 [file] [log] [blame]
Manuel Bottini769c6382019-08-22 13:13:48 +01001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
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];
49
50 for(size_t n_i = 0; n_i < n_size; ++n_i)
51 {
52 for(size_t c_i = 0; c_i < c_size; ++c_i)
53 {
54 float sum_h_w = 0;
Manuel Bottini6665f822019-10-03 12:03:19 +010055 float sum_sq_h_w = 0;
Manuel Bottini769c6382019-08-22 13:13:48 +010056
Manuel Bottini769c6382019-08-22 13:13:48 +010057 for(size_t h_i = 0; h_i < h_size; ++h_i)
58 {
59 for(size_t w_i = 0; w_i < w_size; ++w_i)
60 {
Manuel Bottini6665f822019-10-03 12:03:19 +010061 float val = src[coord2index(src.shape(), Coordinates(w_i, h_i, c_i, n_i))];
62 sum_h_w += val;
63 sum_sq_h_w += val*val;
Manuel Bottini769c6382019-08-22 13:13:48 +010064 }
65 }
Manuel Bottini6665f822019-10-03 12:03:19 +010066 //Compute mean
67 const float mean_h_w = sum_h_w / (h_size * w_size);
68 //Compute variance
69 const float var_h_w = sum_sq_h_w / (h_size * w_size) - mean_h_w * mean_h_w;;
Manuel Bottini769c6382019-08-22 13:13:48 +010070
71 //Apply mean
72 for(size_t h_i = 0; h_i < h_size; ++h_i)
73 {
74 for(size_t w_i = 0; w_i < w_size; ++w_i)
75 {
76 //Compute output
77 size_t index = coord2index(src.shape(), Coordinates(w_i, h_i, c_i, n_i));
78 dst[index] = (src[index] - mean_h_w) * gamma / std::sqrt(var_h_w + epsilon) + beta;
79 }
80 }
81 }
82 }
83 return dst;
84}
85
86template SimpleTensor<float> instance_normalization(const SimpleTensor<float> &src, float gamma, float beta, float epsilon);
87template SimpleTensor<half> instance_normalization(const SimpleTensor<half> &src, float gamma, float beta, float epsilon);
88} // namespace reference
89} // namespace validation
90} // namespace test
91} // namespace arm_compute