Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 669d17d | 2021-02-21 10:15:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TEST_VALIDATION_UTILS_H |
| 25 | #define ARM_COMPUTE_TEST_VALIDATION_UTILS_H |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 26 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Types.h" |
| 28 | #include "tests/Globals.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 29 | #include "tests/Types.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 30 | |
| 31 | #include <array> |
| 32 | #include <random> |
| 33 | #include <type_traits> |
| 34 | #include <utility> |
| 35 | #include <vector> |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 43 | /** Checks if a pixel has valid coordinates |
| 44 | * |
| 45 | * @param x X coordinate |
| 46 | * @param y Y coordinate |
| 47 | * @param width Width of the image |
| 48 | * @param height Height of the image |
| 49 | * @param border_size Border size |
| 50 | * |
| 51 | * @return True if pixel is valid else false |
| 52 | */ |
| 53 | inline bool is_valid_pixel_index(int x, int y, int width, int height, int border_size) |
| 54 | { |
| 55 | return ((x >= -border_size) && (y >= -border_size) && (x < (width + border_size)) && (y < height + border_size)); |
| 56 | } |
| 57 | |
Giorgio Arena | 669d17d | 2021-02-21 10:15:12 +0000 | [diff] [blame] | 58 | #pragma GCC diagnostic push |
| 59 | #pragma GCC diagnostic ignored "-Wstrict-overflow" |
| 60 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 61 | // Return a tensor element at a specified coordinate with different border modes |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 62 | template <typename T> |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 63 | T tensor_elem_at(const SimpleTensor<T> &src, Coordinates coord, BorderMode border_mode, T constant_border_value) |
| 64 | { |
| 65 | const int x = coord.x(); |
| 66 | const int y = coord.y(); |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 67 | const int z = coord.z(); |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 68 | const int width = src.shape().x(); |
| 69 | const int height = src.shape().y(); |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 70 | const int depth = src.shape().z(); |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 71 | |
| 72 | // If coordinates beyond range of tensor's width or height |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 73 | if(x < 0 || y < 0 || z < 0 || x >= width || y >= height || z >= depth) |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 74 | { |
| 75 | if(border_mode == BorderMode::REPLICATE) |
| 76 | { |
| 77 | coord.set(0, std::max(0, std::min(x, width - 1))); |
| 78 | coord.set(1, std::max(0, std::min(y, height - 1))); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | return constant_border_value; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return src[coord2index(src.shape(), coord)]; |
| 87 | } |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 88 | |
Giorgio Arena | 669d17d | 2021-02-21 10:15:12 +0000 | [diff] [blame] | 89 | #pragma GCC diagnostic pop |
| 90 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 91 | template <typename T> |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 92 | T bilinear_policy(const SimpleTensor<T> &in, Coordinates id, float xn, float yn, BorderMode border_mode, T constant_border_value); |
Abe Mbise | 3f0ab6e | 2017-09-01 16:43:47 +0100 | [diff] [blame] | 93 | |
Moritz Pflanzer | 7655a67 | 2017-09-23 11:57:33 +0100 | [diff] [blame] | 94 | /* Apply 2D spatial filter on a single element of @p in at coordinates @p coord |
| 95 | * |
| 96 | * - filter sizes have to be odd number |
| 97 | * - Row major order of filter assumed |
| 98 | * - TO_ZERO rounding policy assumed |
| 99 | * - SATURATE convert policy assumed |
| 100 | */ |
| 101 | template <typename T, typename U, typename V> |
| 102 | void apply_2d_spatial_filter(Coordinates coord, const SimpleTensor<T> &src, SimpleTensor<U> &dst, const TensorShape &filter_shape, const V *filter_itr, double scale, BorderMode border_mode, |
| 103 | T constant_border_value = T(0)) |
| 104 | { |
| 105 | double val = 0.; |
| 106 | const int x = coord.x(); |
| 107 | const int y = coord.y(); |
| 108 | for(int j = y - static_cast<int>(filter_shape[1] / 2); j <= y + static_cast<int>(filter_shape[1] / 2); ++j) |
| 109 | { |
| 110 | for(int i = x - static_cast<int>(filter_shape[0] / 2); i <= x + static_cast<int>(filter_shape[0] / 2); ++i) |
| 111 | { |
| 112 | coord.set(0, i); |
| 113 | coord.set(1, j); |
| 114 | val += static_cast<double>(*filter_itr) * tensor_elem_at(src, coord, border_mode, constant_border_value); |
| 115 | ++filter_itr; |
| 116 | } |
| 117 | } |
| 118 | coord.set(0, x); |
| 119 | coord.set(1, y); |
| 120 | dst[coord2index(src.shape(), coord)] = saturate_cast<U>(support::cpp11::trunc(val * scale)); |
| 121 | } |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 122 | |
| 123 | RawTensor transpose(const RawTensor &src, int chunk_width = 1); |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 124 | |
Abe Mbise | 31bbce1 | 2017-09-19 16:19:13 +0100 | [diff] [blame] | 125 | bool valid_bilinear_policy(float xn, float yn, int width, int height, BorderMode border_mode); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 126 | } // namespace validation |
| 127 | } // namespace test |
| 128 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 129 | #endif /* ARM_COMPUTE_TEST_VALIDATION_UTILS_H */ |