blob: 71a674b515d8261920d2c57c5f9da93aa6b87fc8 [file] [log] [blame]
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +01001/*
Pablo Tello3dd5b682019-03-04 14:14:02 +00002 * Copyright (c) 2017-2019 ARM Limited.
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +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 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010024#include "tests/validation/Helpers.h"
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010025
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000026#include <algorithm>
27#include <cmath>
28
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010029namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010035void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern)
36{
37 unsigned int v = 0;
38 std::mt19937 gen(library->seed());
39 std::bernoulli_distribution dist(0.5);
40
41 for(int r = 0; r < rows; ++r)
42 {
43 for(int c = 0; c < cols; ++c, ++v)
44 {
45 uint8_t val = 0;
46
47 switch(pattern)
48 {
49 case MatrixPattern::BOX:
50 val = 255;
51 break;
52 case MatrixPattern::CROSS:
53 val = ((r == (rows / 2)) || (c == (cols / 2))) ? 255 : 0;
54 break;
55 case MatrixPattern::DISK:
56 val = (((r - rows / 2.0f + 0.5f) * (r - rows / 2.0f + 0.5f)) / ((rows / 2.0f) * (rows / 2.0f)) + ((c - cols / 2.0f + 0.5f) * (c - cols / 2.0f + 0.5f)) / ((cols / 2.0f) *
57 (cols / 2.0f))) <= 1.0f ? 255 : 0;
58 break;
59 case MatrixPattern::OTHER:
60 val = (dist(gen) ? 0 : 255);
61 break;
62 default:
63 return;
64 }
65
66 mask[v] = val;
67 }
68 }
69
70 if(pattern == MatrixPattern::OTHER)
71 {
72 std::uniform_int_distribution<uint8_t> distribution_u8(0, ((cols * rows) - 1));
73 mask[distribution_u8(gen)] = 255;
74 }
75}
76
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010077HarrisCornersParameters harris_corners_parameters()
78{
79 HarrisCornersParameters params;
80
81 std::mt19937 gen(library->seed());
Vidhya Sudhan Loganathan851a3222018-05-11 14:26:51 +010082 std::uniform_real_distribution<float> threshold_dist(0.f, 0.001f);
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010083 std::uniform_real_distribution<float> sensitivity(0.04f, 0.15f);
84 std::uniform_real_distribution<float> euclidean_distance(0.f, 30.f);
85 std::uniform_int_distribution<uint8_t> int_dist(0, 255);
86
87 params.threshold = threshold_dist(gen);
88 params.sensitivity = sensitivity(gen);
89 params.min_dist = euclidean_distance(gen);
90 params.constant_border_value = int_dist(gen);
91
92 return params;
93}
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000094
Abe Mbise1b993382017-12-19 13:51:59 +000095CannyEdgeParameters canny_edge_parameters()
96{
97 CannyEdgeParameters params;
98
99 std::mt19937 gen(library->seed());
100 std::uniform_int_distribution<uint8_t> int_dist(0, 255);
Michele Di Giorgioef915162018-07-30 12:01:44 +0100101 std::uniform_int_distribution<uint8_t> threshold_dist(2, 255);
Abe Mbise1b993382017-12-19 13:51:59 +0000102
103 params.constant_border_value = int_dist(gen);
Michele Di Giorgioef915162018-07-30 12:01:44 +0100104 params.upper_thresh = threshold_dist(gen); // upper_threshold >= 2
Michele Di Giorgiobb71fe52018-06-20 11:45:35 +0100105 threshold_dist = std::uniform_int_distribution<uint8_t>(1, params.upper_thresh - 1);
Michele Di Giorgioef915162018-07-30 12:01:44 +0100106 params.lower_thresh = threshold_dist(gen); // lower_threshold >= 1 && lower_threshold < upper_threshold
Abe Mbise1b993382017-12-19 13:51:59 +0000107
108 return params;
109}
110
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000111SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint8_t> &src)
112{
113 const QuantizationInfo &quantization_info = src.quantization_info();
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100114 SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() };
Michalis Spyrou57dac842018-03-01 16:03:50 +0000115
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000116 for(int i = 0; i < src.num_elements(); ++i)
117 {
118 dst[i] = quantization_info.dequantize(src[i]);
119 }
120 return dst;
121}
122
123SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info)
124{
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100125 SimpleTensor<uint8_t> dst{ src.shape(), DataType::QASYMM8, 1, quantization_info };
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000126 for(int i = 0; i < src.num_elements(); ++i)
127 {
Jaroslaw Rzepecki0a878ae2017-11-22 17:16:39 +0000128 dst[i] = quantization_info.quantize(src[i], RoundingPolicy::TO_NEAREST_UP);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000129 }
130 return dst;
131}
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000132
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100133template <typename T>
134void matrix_multiply(const SimpleTensor<T> &a, const SimpleTensor<T> &b, SimpleTensor<T> &out)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000135{
136 ARM_COMPUTE_ERROR_ON(a.shape()[0] != b.shape()[1]);
137 ARM_COMPUTE_ERROR_ON(a.shape()[1] != out.shape()[1]);
138 ARM_COMPUTE_ERROR_ON(b.shape()[0] != out.shape()[0]);
139
140 const int M = a.shape()[1]; // Rows
141 const int N = b.shape()[0]; // Cols
142 const int K = b.shape()[1];
143
144 for(int y = 0; y < M; ++y)
145 {
146 for(int x = 0; x < N; ++x)
147 {
148 float acc = 0.0f;
149 for(int k = 0; k < K; ++k)
150 {
151 acc += a[y * K + k] * b[x + k * N];
152 }
153
154 out[x + y * N] = acc;
155 }
156 }
157}
158
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100159template <typename T>
160void transpose_matrix(const SimpleTensor<T> &in, SimpleTensor<T> &out)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000161{
162 ARM_COMPUTE_ERROR_ON((in.shape()[0] != out.shape()[1]) || (in.shape()[1] != out.shape()[0]));
163
164 const int width = in.shape()[0];
165 const int height = in.shape()[1];
166
167 for(int y = 0; y < height; ++y)
168 {
169 for(int x = 0; x < width; ++x)
170 {
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000171 const T val = in[x + y * width];
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000172
173 out[x * height + y] = val;
174 }
175 }
176}
177
178template <typename T>
179void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord)
180{
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100181 ARM_COMPUTE_ERROR_ON(tile.shape().num_dimensions() > 2);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000182
183 const int w_tile = tile.shape()[0];
184 const int h_tile = tile.shape()[1];
185
186 // Fill the tile with zeros
187 std::fill(tile.data() + 0, (tile.data() + (w_tile * h_tile)), static_cast<T>(0));
188
189 // Check if with the dimensions greater than 2 we could have out-of-bound reads
190 for(size_t d = 2; d < Coordinates::num_max_dimensions; ++d)
191 {
192 if(coord[d] < 0 || coord[d] >= static_cast<int>(in.shape()[d]))
193 {
194 ARM_COMPUTE_ERROR("coord[d] < 0 || coord[d] >= in.shape()[d] with d >= 2");
195 }
196 }
197
198 // Since we could have out-of-bound reads along the X and Y dimensions,
199 // we start calculating the input address with x = 0 and y = 0
200 Coordinates start_coord = coord;
201 start_coord[0] = 0;
202 start_coord[1] = 0;
203
204 // Get input and roi pointers
205 auto in_ptr = static_cast<const T *>(in(start_coord));
206 auto roi_ptr = static_cast<T *>(tile.data());
207
208 const int x_in_start = std::max(0, coord[0]);
209 const int y_in_start = std::max(0, coord[1]);
210 const int x_in_end = std::min(static_cast<int>(in.shape()[0]), coord[0] + w_tile);
211 const int y_in_end = std::min(static_cast<int>(in.shape()[1]), coord[1] + h_tile);
212
213 // Number of elements to copy per row
214 const int n = x_in_end - x_in_start;
215
216 // Starting coordinates for the ROI
217 const int x_tile_start = coord[0] > 0 ? 0 : std::abs(coord[0]);
218 const int y_tile_start = coord[1] > 0 ? 0 : std::abs(coord[1]);
219
220 // Update input pointer
221 in_ptr += x_in_start;
222 in_ptr += (y_in_start * in.shape()[0]);
223
224 // Update ROI pointer
225 roi_ptr += x_tile_start;
226 roi_ptr += (y_tile_start * tile.shape()[0]);
227
228 for(int y = y_in_start; y < y_in_end; ++y)
229 {
230 // Copy per row
231 std::copy(in_ptr, in_ptr + n, roi_ptr);
232
233 in_ptr += in.shape()[0];
234 roi_ptr += tile.shape()[0];
235 }
236}
237
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100238template <typename T>
239void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape)
240{
241 ARM_COMPUTE_ERROR_ON(anchor.num_dimensions() != shape.num_dimensions());
242 ARM_COMPUTE_ERROR_ON(in.shape().num_dimensions() > 2);
243 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() > 2);
244
245 // Check if with the dimensions greater than 2 we could have out-of-bound reads
246 for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
247 {
248 if(anchor[d] < 0 || ((anchor[d] + shape[d]) > in.shape()[d]))
249 {
250 ARM_COMPUTE_ERROR("anchor[d] < 0 || (anchor[d] + shape[d]) > in.shape()[d]");
251 }
252 }
253
254 // Get input pointer
255 auto in_ptr = static_cast<T *>(in(anchor[0] + anchor[1] * in.shape()[0]));
256
257 const unsigned int n = in.shape()[0];
258
259 for(unsigned int y = 0; y < shape[1]; ++y)
260 {
261 std::fill(in_ptr, in_ptr + shape[0], 0);
262 in_ptr += n;
263 }
264}
265
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100266std::pair<int, int> get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max)
267{
268 ARM_COMPUTE_ERROR_ON_MSG(min > max, "min must be lower equal than max");
269
270 const int min_bound = quant_info.quantize(min, RoundingPolicy::TO_NEAREST_UP);
271 const int max_bound = quant_info.quantize(max, RoundingPolicy::TO_NEAREST_UP);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100272 return std::pair<int, int> { min_bound, max_bound };
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100273}
274
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000275template void get_tile(const SimpleTensor<float> &in, SimpleTensor<float> &roi, const Coordinates &coord);
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100276template void get_tile(const SimpleTensor<half> &in, SimpleTensor<half> &roi, const Coordinates &coord);
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000277template void get_tile(const SimpleTensor<int> &in, SimpleTensor<int> &roi, const Coordinates &coord);
278template void get_tile(const SimpleTensor<short> &in, SimpleTensor<short> &roi, const Coordinates &coord);
279template void get_tile(const SimpleTensor<char> &in, SimpleTensor<char> &roi, const Coordinates &coord);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100280template void zeros(SimpleTensor<float> &in, const Coordinates &anchor, const TensorShape &shape);
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100281template void zeros(SimpleTensor<half> &in, const Coordinates &anchor, const TensorShape &shape);
282template void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out);
283template void transpose_matrix(const SimpleTensor<half> &in, SimpleTensor<half> &out);
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000284template void transpose_matrix(const SimpleTensor<int> &in, SimpleTensor<int> &out);
285template void transpose_matrix(const SimpleTensor<short> &in, SimpleTensor<short> &out);
286template void transpose_matrix(const SimpleTensor<char> &in, SimpleTensor<char> &out);
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100287template void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out);
288template void matrix_multiply(const SimpleTensor<half> &a, const SimpleTensor<half> &b, SimpleTensor<half> &out);
289
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +0100290} // namespace validation
291} // namespace test
292} // namespace arm_compute