• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019-2020 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 
31 namespace arm_compute
32 {
33 namespace test
34 {
35 namespace validation
36 {
37 namespace reference
38 {
39 template <typename T>
instance_normalization(const SimpleTensor<T> & src,float gamma,float beta,float epsilon)40 SimpleTensor<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 #if defined(_OPENMP)
50     #pragma omp parallel for collapse(2)
51 #endif /* _OPENMP */
52     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         {
56             float sum_h_w    = 0;
57             float sum_sq_h_w = 0;
58 
59             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                 {
63                     float val = src[coord2index(src.shape(), Coordinates(w_i, h_i, c_i, n_i))];
64                     sum_h_w += val;
65                     sum_sq_h_w += val * val;
66                 }
67             }
68             //Compute mean
69             const float mean_h_w = sum_h_w / (h_size * w_size);
70             //Compute variance
71             const float var_h_w = sum_sq_h_w / (h_size * w_size) - mean_h_w * mean_h_w;
72             ;
73 
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 
89 template SimpleTensor<float> instance_normalization(const SimpleTensor<float> &src, float gamma, float beta, float epsilon);
90 template 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
95