blob: 8ed98fbc82d39e9dbbb45739a43f6b3be52d417c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010024#ifndef __ARM_COMPUTE_TEST_VALIDATION_H__
25#define __ARM_COMPUTE_TEST_VALIDATION_H__
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010027#include "arm_compute/core/FixedPoint.h"
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010028#include "arm_compute/core/IArray.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Types.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010030#include "tests/IAccessor.h"
31#include "tests/SimpleTensor.h"
John Richardsonf89a49f2017-09-05 11:21:56 +010032#include "tests/Types.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/Utils.h"
34#include "tests/framework/Asserts.h"
35#include "tests/framework/Exceptions.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010036#include "utils/TypePrinter.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010038#include <iomanip>
39#include <ios>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040#include <vector>
41
42namespace arm_compute
43{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044namespace test
45{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046namespace validation
47{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010048/** Class reprensenting an absolute tolerance value. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010050class AbsoluteTolerance
51{
52public:
53 /** Underlying type. */
54 using value_type = T;
55
56 /* Default constructor.
57 *
58 * Initialises the tolerance to 0.
59 */
60 AbsoluteTolerance() = default;
61
62 /** Constructor.
63 *
64 * @param[in] value Absolute tolerance value.
65 */
66 explicit constexpr AbsoluteTolerance(T value)
67 : _value{ value }
68 {
69 }
70
71 /** Implicit conversion to the underlying type. */
72 constexpr operator T() const
73 {
74 return _value;
75 }
76
77private:
78 T _value{ std::numeric_limits<T>::epsilon() };
79};
80
81/** Class reprensenting a relative tolerance value. */
steniu013e05e4e2017-08-25 17:18:01 +010082template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010083class RelativeTolerance
84{
85public:
86 /** Underlying type. */
steniu013e05e4e2017-08-25 17:18:01 +010087 using value_type = T;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010088
89 /* Default constructor.
90 *
91 * Initialises the tolerance to 0.
92 */
93 RelativeTolerance() = default;
94
95 /** Constructor.
96 *
97 * @param[in] value Relative tolerance value.
98 */
99 explicit constexpr RelativeTolerance(value_type value)
100 : _value{ value }
101 {
102 }
103
104 /** Implicit conversion to the underlying type. */
105 constexpr operator value_type() const
106 {
107 return _value;
108 }
109
110private:
steniu013e05e4e2017-08-25 17:18:01 +0100111 value_type _value{ std::numeric_limits<T>::epsilon() };
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100112};
113
114/** Print AbsoluteTolerance type. */
115template <typename T>
116inline ::std::ostream &operator<<(::std::ostream &os, const AbsoluteTolerance<T> &tolerance)
117{
118 os << static_cast<typename AbsoluteTolerance<T>::value_type>(tolerance);
119
120 return os;
121}
122
123/** Print RelativeTolerance type. */
steniu013e05e4e2017-08-25 17:18:01 +0100124template <typename T>
125inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100126{
steniu013e05e4e2017-08-25 17:18:01 +0100127 os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100128
129 return os;
130}
131
132template <typename T>
133bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134{
135 if(dimensions1.num_dimensions() != dimensions2.num_dimensions())
136 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100137 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 }
139
140 for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i)
141 {
142 if(dimensions1[i] != dimensions2[i])
143 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100144 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 }
146 }
147
148 return true;
149}
150
151/** Validate valid regions.
152 *
153 * - Dimensionality has to be the same.
154 * - Anchors have to match.
155 * - Shapes have to match.
156 */
157void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference);
158
159/** Validate padding.
160 *
161 * Padding on all sides has to be the same.
162 */
163void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference);
164
165/** Validate tensors.
166 *
167 * - Dimensionality has to be the same.
168 * - All values have to match.
169 *
170 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
171 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
172 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
173 * other test cases.
174 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100175template <typename T, typename U = AbsoluteTolerance<T>>
176void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177
178/** Validate tensors with valid region.
179 *
180 * - Dimensionality has to be the same.
181 * - All values have to match.
182 *
183 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
184 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
185 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
186 * other test cases.
187 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100188template <typename T, typename U = AbsoluteTolerance<T>>
189void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value = U(), float tolerance_number = 0.f);
Isabella Gottardi62031532017-07-04 11:21:28 +0100190
Isabella Gottardi83be7452017-08-29 13:47:03 +0100191/** Validate tensors with valid mask.
192 *
193 * - Dimensionality has to be the same.
194 * - All values have to match.
195 *
196 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
197 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
198 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
199 * other test cases.
200 */
201template <typename T, typename U = AbsoluteTolerance<T>>
202void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value = U(), float tolerance_number = 0.f);
203
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204/** Validate tensors against constant value.
205 *
206 * - All values have to match.
207 */
208void validate(const IAccessor &tensor, const void *reference_value);
209
210/** Validate border against a constant value.
211 *
212 * - All border values have to match the specified value if mode is CONSTANT.
213 * - All border values have to be replicated if mode is REPLICATE.
214 * - Nothing is validated for mode UNDEFINED.
215 */
216void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value);
217
218/** Validate classified labels against expected ones.
219 *
220 * - All values should match
221 */
222void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
steniu01960b0842017-06-23 11:44:34 +0100223
224/** Validate float value.
225 *
226 * - All values should match
227 */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100228template <typename T, typename U = AbsoluteTolerance<T>>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100229bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
230
231/** Validate key points. */
232template <typename T, typename U, typename V = AbsoluteTolerance<float>>
233void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance = AbsoluteTolerance<float>());
steniu01960b0842017-06-23 11:44:34 +0100234
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100235template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100236struct compare_base
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100237{
John Richardsondd715f22017-09-18 16:10:48 +0100238 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0), bool wrap_range = false)
239 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }, _wrap_range{ wrap_range }
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100240 {
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100241 }
242
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100243 typename T::value_type _target{};
244 typename T::value_type _reference{};
245 T _tolerance{};
John Richardsondd715f22017-09-18 16:10:48 +0100246 bool _wrap_range{};
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100247};
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100248
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100249template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100250struct compare;
251
252template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100253struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100254{
255 using compare_base<AbsoluteTolerance<U>>::compare_base;
256
257 operator bool() const
258 {
259 if(!std::isfinite(this->_target) || !std::isfinite(this->_reference))
260 {
261 return false;
262 }
263 else if(this->_target == this->_reference)
264 {
265 return true;
266 }
267
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100268 using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type;
269
John Richardsondd715f22017-09-18 16:10:48 +0100270 if(this->_wrap_range)
271 {
272 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target)) - std::abs(static_cast<comparison_type>(this->_reference)));
273 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
274 }
275
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100276 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference)));
277
278 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100279 }
280};
281
282template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100283struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100284{
steniu013e05e4e2017-08-25 17:18:01 +0100285 using compare_base<RelativeTolerance<U>>::compare_base;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100286
287 operator bool() const
288 {
steniu013e05e4e2017-08-25 17:18:01 +0100289 if(!std::isfinite(this->_target) || !std::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100290 {
291 return false;
292 }
steniu013e05e4e2017-08-25 17:18:01 +0100293 else if(this->_target == this->_reference)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100294 {
295 return true;
296 }
297
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100298 const U epsilon = (std::is_same<half, typename std::remove_cv<U>::type>::value || (this->_reference == 0)) ? static_cast<U>(0.01) : static_cast<U>(1e-05);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100299
steniu013e05e4e2017-08-25 17:18:01 +0100300 if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon)
301 {
302 return true;
303 }
304 else
305 {
306 if(static_cast<double>(this->_reference) == 0.0f) // We have checked whether _reference and _target is closing. If _reference is 0 but not closed to _target, it should return false
307 {
308 return false;
309 }
310
311 const double relative_change = std::abs(static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference;
312
313 return relative_change <= static_cast<U>(this->_tolerance);
314 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100315 }
316};
317
318template <typename T, typename U>
319void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
320{
321 // Validate with valid region covering the entire shape
322 validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number);
323}
324
325template <typename T, typename U>
John Richardsondd715f22017-09-18 16:10:48 +0100326void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
327{
328 // Validate with valid region covering the entire shape
329 validate_wrap(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number);
330}
331
332template <typename T, typename U>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100333void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
334{
335 int64_t num_mismatches = 0;
336 int64_t num_elements = 0;
337
338 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
339 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
340
341 if(reference.format() != Format::UNKNOWN)
342 {
343 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
344 }
345
346 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
347 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
348
349 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
350 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
351
352 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
353 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
354 {
355 const Coordinates id = index2coord(reference.shape(), element_idx);
356
357 if(is_in_valid_region(valid_region, id))
358 {
359 // Iterate over all channels within one element
360 for(int c = 0; c < min_channels; ++c)
361 {
362 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
363 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
364
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100365 if(!compare<U>(target_value, reference_value, tolerance_value))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100366 {
367 ARM_COMPUTE_TEST_INFO("id = " << id);
368 ARM_COMPUTE_TEST_INFO("channel = " << c);
369 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
370 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
371 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
steniu01172c58d2017-08-31 13:49:08 +0100372 framework::ARM_COMPUTE_PRINT_INFO();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100373
374 ++num_mismatches;
375 }
376
377 ++num_elements;
378 }
379 }
380 }
381
382 if(num_elements > 0)
383 {
384 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
385 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
386
387 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
388 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
389 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100390 }
391}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100392
John Richardsondd715f22017-09-18 16:10:48 +0100393template <typename T, typename U>
394void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
395{
396 int64_t num_mismatches = 0;
397 int64_t num_elements = 0;
398
399 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
400 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
401
402 if(reference.format() != Format::UNKNOWN)
403 {
404 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
405 }
406
407 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
408 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
409
410 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
411 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
412
413 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
414 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
415 {
416 const Coordinates id = index2coord(reference.shape(), element_idx);
417
418 if(is_in_valid_region(valid_region, id))
419 {
420 // Iterate over all channels within one element
421 for(int c = 0; c < min_channels; ++c)
422 {
423 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
424 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
425
426 bool equal = compare<U>(target_value, reference_value, tolerance_value);
427
428 if(!equal)
429 {
430 equal = compare<U>(target_value, reference_value, tolerance_value, true);
431 }
432
433 if(!equal)
434 {
435 ARM_COMPUTE_TEST_INFO("id = " << id);
436 ARM_COMPUTE_TEST_INFO("channel = " << c);
437 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
438 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
439 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
440 framework::ARM_COMPUTE_PRINT_INFO();
441
442 ++num_mismatches;
443 }
444
445 ++num_elements;
446 }
447 }
448 }
449
450 if(num_elements > 0)
451 {
452 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
453 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
454
455 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
456 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
457 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
458 }
459}
460
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100461/** Check which keypoints from [first1, last1) are missing in [first2, last2) */
462template <typename T, typename U, typename V>
463std::pair<int64_t, int64_t> compare_keypoints(T first1, T last1, U first2, U last2, V tolerance)
464{
465 int64_t num_missing = 0;
466 int64_t num_mismatches = 0;
467
468 while(first1 != last1)
469 {
470 const auto point = std::find_if(first2, last2, [&](KeyPoint point)
471 {
472 return point.x == first1->x && point.y == first1->y;
473 });
474
475 if(point == last2)
476 {
477 ++num_missing;
478 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1)
479 ARM_COMPUTE_EXPECT_FAIL("Key point not found", framework::LogLevel::DEBUG);
480 }
481 else if(!validate(point->tracking_status, first1->tracking_status) || !validate(point->strength, first1->strength, tolerance) || !validate(point->scale, first1->scale)
482 || !validate(point->orientation, first1->orientation) || !validate(point->error, first1->error))
483 {
484 ++num_mismatches;
485 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1)
486 ARM_COMPUTE_TEST_INFO("keypoint2 = " << *point)
487 ARM_COMPUTE_EXPECT_FAIL("Mismatching keypoint", framework::LogLevel::DEBUG);
488 }
489
490 ++first1;
491 }
492
493 return std::make_pair(num_missing, num_mismatches);
494}
495
496template <typename T, typename U, typename V>
497void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance)
498{
499 const int64_t num_elements_target = std::distance(target_first, target_last);
500 const int64_t num_elements_reference = std::distance(reference_first, reference_last);
501
502 ARM_COMPUTE_EXPECT_EQUAL(num_elements_target, num_elements_reference, framework::LogLevel::ERRORS);
503
504 int64_t num_missing = 0;
505 int64_t num_mismatches = 0;
506
507 if(num_elements_reference > 0)
508 {
509 std::tie(num_missing, num_mismatches) = compare_keypoints(reference_first, reference_last, target_first, target_last, tolerance);
510
511 const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f;
512 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f;
513
514 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) are missing in target");
515 ARM_COMPUTE_EXPECT_EQUAL(num_missing, 0, framework::LogLevel::ERRORS);
516
517 ARM_COMPUTE_TEST_INFO(num_mismatches << " keypoints (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched");
518 ARM_COMPUTE_EXPECT_EQUAL(num_mismatches, 0, framework::LogLevel::ERRORS);
519 }
520
521 if(num_elements_target > 0)
522 {
523 std::tie(num_missing, num_mismatches) = compare_keypoints(target_first, target_last, reference_first, reference_last, tolerance);
524
525 const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f;
526
527 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) are not part of target");
528 ARM_COMPUTE_EXPECT_EQUAL(num_missing, 0, framework::LogLevel::ERRORS);
529 }
530}
531
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100532template <typename T, typename U>
Isabella Gottardi83be7452017-08-29 13:47:03 +0100533void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value, float tolerance_number)
534{
535 int64_t num_mismatches = 0;
536 int64_t num_elements = 0;
537
538 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
539 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
540
541 if(reference.format() != Format::UNKNOWN)
542 {
543 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
544 }
545
546 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
547 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
548
549 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
550 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
551
552 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
553 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
554 {
555 const Coordinates id = index2coord(reference.shape(), element_idx);
556
557 if(valid_mask[element_idx] == 1)
558 {
559 // Iterate over all channels within one element
560 for(int c = 0; c < min_channels; ++c)
561 {
562 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
563 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
564
565 if(!compare<U>(target_value, reference_value, tolerance_value))
566 {
567 ARM_COMPUTE_TEST_INFO("id = " << id);
568 ARM_COMPUTE_TEST_INFO("channel = " << c);
569 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
570 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
571 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
572 framework::ARM_COMPUTE_PRINT_INFO();
573
574 ++num_mismatches;
575 }
576
577 ++num_elements;
578 }
579 }
580 else
581 {
582 ++num_elements;
583 }
584 }
585
586 if(num_elements > 0)
587 {
588 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
589 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
590
591 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
592 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
593 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
594 }
595}
596
597template <typename T, typename U>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100598bool validate(T target, T reference, U tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100599{
600 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
601 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
602 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance)));
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100603
604 const bool equal = compare<U>(target, reference, tolerance);
605
606 ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS);
607
608 return equal;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100609}
John Richardsonf89a49f2017-09-05 11:21:56 +0100610
611template <typename T, typename U>
612void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference)
613{
614 ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS);
615 ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS);
616
617 ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS);
618 ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS);
619
620 for(uint32_t i = 0; i < target.min_loc.size(); ++i)
621 {
622 const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord)
623 {
624 return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y;
625 });
626
627 ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS);
628 }
629
630 for(uint32_t i = 0; i < target.max_loc.size(); ++i)
631 {
632 const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord)
633 {
634 return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y;
635 });
636
637 ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS);
638 }
639}
640
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100641} // namespace validation
642} // namespace test
643} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100644#endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */