Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 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> |
| 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]; |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 49 | #if defined(_OPENMP) |
| 50 | #pragma omp parallel for collapse(2) |
| 51 | #endif /* _OPENMP */ |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 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 | { |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 56 | float sum_h_w = 0; |
Manuel Bottini | 6665f82 | 2019-10-03 12:03:19 +0100 | [diff] [blame] | 57 | float sum_sq_h_w = 0; |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 58 | |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 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 | { |
Manuel Bottini | 6665f82 | 2019-10-03 12:03:19 +0100 | [diff] [blame] | 63 | float val = src[coord2index(src.shape(), Coordinates(w_i, h_i, c_i, n_i))]; |
| 64 | sum_h_w += val; |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 65 | sum_sq_h_w += val * val; |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 66 | } |
| 67 | } |
Manuel Bottini | 6665f82 | 2019-10-03 12:03:19 +0100 | [diff] [blame] | 68 | //Compute mean |
| 69 | const float mean_h_w = sum_h_w / (h_size * w_size); |
| 70 | //Compute variance |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 71 | const float var_h_w = sum_sq_h_w / (h_size * w_size) - mean_h_w * mean_h_w; |
| 72 | ; |
Manuel Bottini | 769c638 | 2019-08-22 13:13:48 +0100 | [diff] [blame] | 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 |