Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | */ |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [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" |
| 29 | #include "tests/ILutAccessor.h" |
| 30 | #include "tests/Types.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 31 | |
| 32 | #include <array> |
| 33 | #include <random> |
| 34 | #include <type_traits> |
| 35 | #include <utility> |
| 36 | #include <vector> |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 44 | // Return a tensor element at a specified coordinate with different border modes |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 45 | template <typename T> |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 46 | T tensor_elem_at(const SimpleTensor<T> &src, Coordinates coord, BorderMode border_mode, T constant_border_value) |
| 47 | { |
| 48 | const int x = coord.x(); |
| 49 | const int y = coord.y(); |
| 50 | const int width = src.shape().x(); |
| 51 | const int height = src.shape().y(); |
| 52 | |
| 53 | // If coordinates beyond range of tensor's width or height |
| 54 | if(x < 0 || y < 0 || x >= width || y >= height) |
| 55 | { |
| 56 | if(border_mode == BorderMode::REPLICATE) |
| 57 | { |
| 58 | coord.set(0, std::max(0, std::min(x, width - 1))); |
| 59 | coord.set(1, std::max(0, std::min(y, height - 1))); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | return constant_border_value; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return src[coord2index(src.shape(), coord)]; |
| 68 | } |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 69 | |
| 70 | template <typename T> |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 71 | 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] | 72 | |
Moritz Pflanzer | 7655a67 | 2017-09-23 11:57:33 +0100 | [diff] [blame] | 73 | /* Apply 2D spatial filter on a single element of @p in at coordinates @p coord |
| 74 | * |
| 75 | * - filter sizes have to be odd number |
| 76 | * - Row major order of filter assumed |
| 77 | * - TO_ZERO rounding policy assumed |
| 78 | * - SATURATE convert policy assumed |
| 79 | */ |
| 80 | template <typename T, typename U, typename V> |
| 81 | 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, |
| 82 | T constant_border_value = T(0)) |
| 83 | { |
| 84 | double val = 0.; |
| 85 | const int x = coord.x(); |
| 86 | const int y = coord.y(); |
| 87 | for(int j = y - static_cast<int>(filter_shape[1] / 2); j <= y + static_cast<int>(filter_shape[1] / 2); ++j) |
| 88 | { |
| 89 | for(int i = x - static_cast<int>(filter_shape[0] / 2); i <= x + static_cast<int>(filter_shape[0] / 2); ++i) |
| 90 | { |
| 91 | coord.set(0, i); |
| 92 | coord.set(1, j); |
| 93 | val += static_cast<double>(*filter_itr) * tensor_elem_at(src, coord, border_mode, constant_border_value); |
| 94 | ++filter_itr; |
| 95 | } |
| 96 | } |
| 97 | coord.set(0, x); |
| 98 | coord.set(1, y); |
| 99 | dst[coord2index(src.shape(), coord)] = saturate_cast<U>(support::cpp11::trunc(val * scale)); |
| 100 | } |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 101 | |
| 102 | RawTensor transpose(const RawTensor &src, int chunk_width = 1); |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame^] | 103 | |
| 104 | /** Fill matrix random. |
| 105 | * |
| 106 | * @param[in,out] matrix Matrix |
| 107 | * @param[in] cols Columns (width) of matrix |
| 108 | * @param[in] rows Rows (height) of matrix |
| 109 | */ |
| 110 | template <std::size_t SIZE> |
| 111 | inline void fill_warp_matrix(std::array<float, SIZE> &matrix, int cols, int rows) |
| 112 | { |
| 113 | std::mt19937 gen(library.get()->seed()); |
| 114 | std::uniform_real_distribution<float> dist(-1, 1); |
| 115 | |
| 116 | for(int v = 0, r = 0; r < rows; ++r) |
| 117 | { |
| 118 | for(int c = 0; c < cols; ++c, ++v) |
| 119 | { |
| 120 | matrix[v] = dist(gen); |
| 121 | } |
| 122 | } |
| 123 | if(SIZE == 9) |
| 124 | { |
| 125 | matrix[(cols * rows) - 1] = 1; |
| 126 | } |
| 127 | } |
| 128 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 129 | } // namespace validation |
| 130 | } // namespace test |
| 131 | } // namespace arm_compute |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 132 | #endif /* __ARM_COMPUTE_TEST_VALIDATION_UTILS_H__ */ |