blob: 17ef2357540e12b10fa1cf4057a8e09e76640e5a [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 Arm Limited.
Giorgio Arena93a690e2017-08-01 16:09:33 +01003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_TEST_VALIDATION_UTILS_H
25#define ARM_COMPUTE_TEST_VALIDATION_UTILS_H
Giorgio Arena93a690e2017-08-01 16:09:33 +010026
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010027#include "arm_compute/core/Types.h"
28#include "tests/Globals.h"
29#include "tests/ILutAccessor.h"
30#include "tests/Types.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010031
32#include <array>
33#include <random>
34#include <type_traits>
35#include <utility>
36#include <vector>
Giorgio Arena93a690e2017-08-01 16:09:33 +010037
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
Daniil Efremov7a49c792017-11-14 21:25:34 +070044/** Checks if a pixel has valid coordinates
45 *
46 * @param x X coordinate
47 * @param y Y coordinate
48 * @param width Width of the image
49 * @param height Height of the image
50 * @param border_size Border size
51 *
52 * @return True if pixel is valid else false
53 */
54inline bool is_valid_pixel_index(int x, int y, int width, int height, int border_size)
55{
56 return ((x >= -border_size) && (y >= -border_size) && (x < (width + border_size)) && (y < height + border_size));
57}
58
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010059// Return a tensor element at a specified coordinate with different border modes
Giorgio Arena93a690e2017-08-01 16:09:33 +010060template <typename T>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010061T tensor_elem_at(const SimpleTensor<T> &src, Coordinates coord, BorderMode border_mode, T constant_border_value)
62{
63 const int x = coord.x();
64 const int y = coord.y();
Pablo Tello764b1af2018-04-23 16:11:45 +010065 const int z = coord.z();
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010066 const int width = src.shape().x();
67 const int height = src.shape().y();
Pablo Tello764b1af2018-04-23 16:11:45 +010068 const int depth = src.shape().z();
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010069
70 // If coordinates beyond range of tensor's width or height
Pablo Tello764b1af2018-04-23 16:11:45 +010071 if(x < 0 || y < 0 || z < 0 || x >= width || y >= height || z >= depth)
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010072 {
73 if(border_mode == BorderMode::REPLICATE)
74 {
75 coord.set(0, std::max(0, std::min(x, width - 1)));
76 coord.set(1, std::max(0, std::min(y, height - 1)));
77 }
78 else
79 {
80 return constant_border_value;
81 }
82 }
83
84 return src[coord2index(src.shape(), coord)];
85}
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010086
87template <typename T>
Georgios Pinitas583137c2017-08-31 18:12:42 +010088T bilinear_policy(const SimpleTensor<T> &in, Coordinates id, float xn, float yn, BorderMode border_mode, T constant_border_value);
Abe Mbise3f0ab6e2017-09-01 16:43:47 +010089
Moritz Pflanzer7655a672017-09-23 11:57:33 +010090/* Apply 2D spatial filter on a single element of @p in at coordinates @p coord
91 *
92 * - filter sizes have to be odd number
93 * - Row major order of filter assumed
94 * - TO_ZERO rounding policy assumed
95 * - SATURATE convert policy assumed
96 */
97template <typename T, typename U, typename V>
98void 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,
99 T constant_border_value = T(0))
100{
101 double val = 0.;
102 const int x = coord.x();
103 const int y = coord.y();
104 for(int j = y - static_cast<int>(filter_shape[1] / 2); j <= y + static_cast<int>(filter_shape[1] / 2); ++j)
105 {
106 for(int i = x - static_cast<int>(filter_shape[0] / 2); i <= x + static_cast<int>(filter_shape[0] / 2); ++i)
107 {
108 coord.set(0, i);
109 coord.set(1, j);
110 val += static_cast<double>(*filter_itr) * tensor_elem_at(src, coord, border_mode, constant_border_value);
111 ++filter_itr;
112 }
113 }
114 coord.set(0, x);
115 coord.set(1, y);
116 dst[coord2index(src.shape(), coord)] = saturate_cast<U>(support::cpp11::trunc(val * scale));
117}
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100118
119RawTensor transpose(const RawTensor &src, int chunk_width = 1);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100120
121/** Fill matrix random.
122 *
123 * @param[in,out] matrix Matrix
Isabella Gottardi83be7452017-08-29 13:47:03 +0100124 */
125template <std::size_t SIZE>
Pablo Tellod616cb92017-09-27 10:07:45 +0100126inline void fill_warp_matrix(std::array<float, SIZE> &matrix)
Isabella Gottardi83be7452017-08-29 13:47:03 +0100127{
128 std::mt19937 gen(library.get()->seed());
129 std::uniform_real_distribution<float> dist(-1, 1);
Pablo Tellod616cb92017-09-27 10:07:45 +0100130 for(auto &x : matrix)
Isabella Gottardi83be7452017-08-29 13:47:03 +0100131 {
Pablo Tellod616cb92017-09-27 10:07:45 +0100132 x = dist(gen);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100133 }
134 if(SIZE == 9)
135 {
Pablo Tellod616cb92017-09-27 10:07:45 +0100136 // This is only used in Warp Perspective, we set M[3][3] = 1 so that Z0 is not 0 and we avoid division by 0.
137 matrix[8] = 1.f;
Isabella Gottardi83be7452017-08-29 13:47:03 +0100138 }
139}
140
Abe Mbise31bbce12017-09-19 16:19:13 +0100141bool valid_bilinear_policy(float xn, float yn, int width, int height, BorderMode border_mode);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100142} // namespace validation
143} // namespace test
144} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000145#endif /* ARM_COMPUTE_TEST_VALIDATION_UTILS_H */