blob: 7a010855144db97d2dc5e3cfdb520544a44a4889 [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"
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +010030#include "support/ToolchainSupport.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#include "tests/IAccessor.h"
32#include "tests/SimpleTensor.h"
John Richardsonf89a49f2017-09-05 11:21:56 +010033#include "tests/Types.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010034#include "tests/Utils.h"
35#include "tests/framework/Asserts.h"
36#include "tests/framework/Exceptions.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010037#include "utils/TypePrinter.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010039#include <iomanip>
40#include <ios>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041#include <vector>
42
43namespace arm_compute
44{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045namespace test
46{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047namespace validation
48{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010049/** Class reprensenting an absolute tolerance value. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010051class AbsoluteTolerance
52{
53public:
54 /** Underlying type. */
55 using value_type = T;
56
57 /* Default constructor.
58 *
59 * Initialises the tolerance to 0.
60 */
61 AbsoluteTolerance() = default;
62
63 /** Constructor.
64 *
65 * @param[in] value Absolute tolerance value.
66 */
67 explicit constexpr AbsoluteTolerance(T value)
68 : _value{ value }
69 {
70 }
71
72 /** Implicit conversion to the underlying type. */
73 constexpr operator T() const
74 {
75 return _value;
76 }
77
78private:
79 T _value{ std::numeric_limits<T>::epsilon() };
80};
81
82/** Class reprensenting a relative tolerance value. */
steniu013e05e4e2017-08-25 17:18:01 +010083template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010084class RelativeTolerance
85{
86public:
87 /** Underlying type. */
steniu013e05e4e2017-08-25 17:18:01 +010088 using value_type = T;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010089
90 /* Default constructor.
91 *
92 * Initialises the tolerance to 0.
93 */
94 RelativeTolerance() = default;
95
96 /** Constructor.
97 *
98 * @param[in] value Relative tolerance value.
99 */
100 explicit constexpr RelativeTolerance(value_type value)
101 : _value{ value }
102 {
103 }
104
105 /** Implicit conversion to the underlying type. */
106 constexpr operator value_type() const
107 {
108 return _value;
109 }
110
111private:
steniu013e05e4e2017-08-25 17:18:01 +0100112 value_type _value{ std::numeric_limits<T>::epsilon() };
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100113};
114
115/** Print AbsoluteTolerance type. */
116template <typename T>
117inline ::std::ostream &operator<<(::std::ostream &os, const AbsoluteTolerance<T> &tolerance)
118{
119 os << static_cast<typename AbsoluteTolerance<T>::value_type>(tolerance);
120
121 return os;
122}
123
124/** Print RelativeTolerance type. */
steniu013e05e4e2017-08-25 17:18:01 +0100125template <typename T>
126inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100127{
steniu013e05e4e2017-08-25 17:18:01 +0100128 os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100129
130 return os;
131}
132
133template <typename T>
134bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135{
136 if(dimensions1.num_dimensions() != dimensions2.num_dimensions())
137 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100138 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 }
140
141 for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i)
142 {
143 if(dimensions1[i] != dimensions2[i])
144 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100145 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 }
147 }
148
149 return true;
150}
151
152/** Validate valid regions.
153 *
154 * - Dimensionality has to be the same.
155 * - Anchors have to match.
156 * - Shapes have to match.
157 */
158void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference);
159
160/** Validate padding.
161 *
162 * Padding on all sides has to be the same.
163 */
164void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference);
165
166/** Validate tensors.
167 *
168 * - Dimensionality has to be the same.
169 * - All values have to match.
170 *
171 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
172 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
173 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
174 * other test cases.
175 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100176template <typename T, typename U = AbsoluteTolerance<T>>
177void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178
179/** Validate tensors with valid region.
180 *
181 * - Dimensionality has to be the same.
182 * - All values have to match.
183 *
184 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
185 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
186 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
187 * other test cases.
188 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100189template <typename T, typename U = AbsoluteTolerance<T>>
190void 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 +0100191
Isabella Gottardi83be7452017-08-29 13:47:03 +0100192/** Validate tensors with valid mask.
193 *
194 * - Dimensionality has to be the same.
195 * - All values have to match.
196 *
197 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
198 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
199 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
200 * other test cases.
201 */
202template <typename T, typename U = AbsoluteTolerance<T>>
203void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value = U(), float tolerance_number = 0.f);
204
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205/** Validate tensors against constant value.
206 *
207 * - All values have to match.
208 */
209void validate(const IAccessor &tensor, const void *reference_value);
210
211/** Validate border against a constant value.
212 *
213 * - All border values have to match the specified value if mode is CONSTANT.
214 * - All border values have to be replicated if mode is REPLICATE.
215 * - Nothing is validated for mode UNDEFINED.
216 */
217void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value);
218
219/** Validate classified labels against expected ones.
220 *
221 * - All values should match
222 */
223void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
steniu01960b0842017-06-23 11:44:34 +0100224
225/** Validate float value.
226 *
227 * - All values should match
228 */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100229template <typename T, typename U = AbsoluteTolerance<T>>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100230bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
231
232/** Validate key points. */
233template <typename T, typename U, typename V = AbsoluteTolerance<float>>
234void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance = AbsoluteTolerance<float>());
steniu01960b0842017-06-23 11:44:34 +0100235
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100236template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100237struct compare_base
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100238{
John Richardsondd715f22017-09-18 16:10:48 +0100239 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0), bool wrap_range = false)
240 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }, _wrap_range{ wrap_range }
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100241 {
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100242 }
243
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100244 typename T::value_type _target{};
245 typename T::value_type _reference{};
246 T _tolerance{};
John Richardsondd715f22017-09-18 16:10:48 +0100247 bool _wrap_range{};
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100248};
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100249
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100250template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100251struct compare;
252
253template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100254struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100255{
256 using compare_base<AbsoluteTolerance<U>>::compare_base;
257
258 operator bool() const
259 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100260 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100261 {
262 return false;
263 }
264 else if(this->_target == this->_reference)
265 {
266 return true;
267 }
268
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100269 using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type;
270
John Richardsondd715f22017-09-18 16:10:48 +0100271 if(this->_wrap_range)
272 {
273 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target)) - std::abs(static_cast<comparison_type>(this->_reference)));
274 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
275 }
276
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100277 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference)));
278
279 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100280 }
281};
282
283template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100284struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100285{
steniu013e05e4e2017-08-25 17:18:01 +0100286 using compare_base<RelativeTolerance<U>>::compare_base;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100287
288 operator bool() const
289 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100290 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100291 {
292 return false;
293 }
steniu013e05e4e2017-08-25 17:18:01 +0100294 else if(this->_target == this->_reference)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100295 {
296 return true;
297 }
298
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100299 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 +0100300
steniu013e05e4e2017-08-25 17:18:01 +0100301 if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon)
302 {
303 return true;
304 }
305 else
306 {
307 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
308 {
309 return false;
310 }
311
312 const double relative_change = std::abs(static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference;
313
314 return relative_change <= static_cast<U>(this->_tolerance);
315 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100316 }
317};
318
319template <typename T, typename U>
320void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
321{
322 // Validate with valid region covering the entire shape
323 validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number);
324}
325
326template <typename T, typename U>
John Richardsondd715f22017-09-18 16:10:48 +0100327void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
328{
329 // Validate with valid region covering the entire shape
330 validate_wrap(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number);
331}
332
333template <typename T, typename U>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100334void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
335{
336 int64_t num_mismatches = 0;
337 int64_t num_elements = 0;
338
339 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
340 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
341
342 if(reference.format() != Format::UNKNOWN)
343 {
344 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
345 }
346
347 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
348 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
349
350 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
351 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
352
353 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
354 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
355 {
356 const Coordinates id = index2coord(reference.shape(), element_idx);
357
358 if(is_in_valid_region(valid_region, id))
359 {
360 // Iterate over all channels within one element
361 for(int c = 0; c < min_channels; ++c)
362 {
363 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
364 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
365
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100366 if(!compare<U>(target_value, reference_value, tolerance_value))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100367 {
368 ARM_COMPUTE_TEST_INFO("id = " << id);
369 ARM_COMPUTE_TEST_INFO("channel = " << c);
370 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
371 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
372 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 +0100373 framework::ARM_COMPUTE_PRINT_INFO();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100374
375 ++num_mismatches;
376 }
377
378 ++num_elements;
379 }
380 }
381 }
382
383 if(num_elements > 0)
384 {
385 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
386 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
387
388 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
389 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
390 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100391 }
392}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100393
John Richardsondd715f22017-09-18 16:10:48 +0100394template <typename T, typename U>
395void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
396{
397 int64_t num_mismatches = 0;
398 int64_t num_elements = 0;
399
400 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
401 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
402
403 if(reference.format() != Format::UNKNOWN)
404 {
405 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
406 }
407
408 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
409 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
410
411 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
412 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
413
414 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
415 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
416 {
417 const Coordinates id = index2coord(reference.shape(), element_idx);
418
419 if(is_in_valid_region(valid_region, id))
420 {
421 // Iterate over all channels within one element
422 for(int c = 0; c < min_channels; ++c)
423 {
424 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
425 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
426
427 bool equal = compare<U>(target_value, reference_value, tolerance_value);
428
429 if(!equal)
430 {
431 equal = compare<U>(target_value, reference_value, tolerance_value, true);
432 }
433
434 if(!equal)
435 {
436 ARM_COMPUTE_TEST_INFO("id = " << id);
437 ARM_COMPUTE_TEST_INFO("channel = " << c);
438 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
439 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
440 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
441 framework::ARM_COMPUTE_PRINT_INFO();
442
443 ++num_mismatches;
444 }
445
446 ++num_elements;
447 }
448 }
449 }
450
451 if(num_elements > 0)
452 {
453 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
454 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
455
456 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
457 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
458 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
459 }
460}
461
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100462/** Check which keypoints from [first1, last1) are missing in [first2, last2) */
463template <typename T, typename U, typename V>
464std::pair<int64_t, int64_t> compare_keypoints(T first1, T last1, U first2, U last2, V tolerance)
465{
466 int64_t num_missing = 0;
467 int64_t num_mismatches = 0;
468
469 while(first1 != last1)
470 {
471 const auto point = std::find_if(first2, last2, [&](KeyPoint point)
472 {
473 return point.x == first1->x && point.y == first1->y;
474 });
475
476 if(point == last2)
477 {
478 ++num_missing;
479 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1)
480 ARM_COMPUTE_EXPECT_FAIL("Key point not found", framework::LogLevel::DEBUG);
481 }
482 else if(!validate(point->tracking_status, first1->tracking_status) || !validate(point->strength, first1->strength, tolerance) || !validate(point->scale, first1->scale)
483 || !validate(point->orientation, first1->orientation) || !validate(point->error, first1->error))
484 {
485 ++num_mismatches;
486 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1)
487 ARM_COMPUTE_TEST_INFO("keypoint2 = " << *point)
488 ARM_COMPUTE_EXPECT_FAIL("Mismatching keypoint", framework::LogLevel::DEBUG);
489 }
490
491 ++first1;
492 }
493
494 return std::make_pair(num_missing, num_mismatches);
495}
496
497template <typename T, typename U, typename V>
498void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance)
499{
500 const int64_t num_elements_target = std::distance(target_first, target_last);
501 const int64_t num_elements_reference = std::distance(reference_first, reference_last);
502
503 ARM_COMPUTE_EXPECT_EQUAL(num_elements_target, num_elements_reference, framework::LogLevel::ERRORS);
504
505 int64_t num_missing = 0;
506 int64_t num_mismatches = 0;
507
508 if(num_elements_reference > 0)
509 {
510 std::tie(num_missing, num_mismatches) = compare_keypoints(reference_first, reference_last, target_first, target_last, tolerance);
511
512 const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f;
513 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f;
514
515 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) are missing in target");
516 ARM_COMPUTE_EXPECT_EQUAL(num_missing, 0, framework::LogLevel::ERRORS);
517
518 ARM_COMPUTE_TEST_INFO(num_mismatches << " keypoints (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched");
519 ARM_COMPUTE_EXPECT_EQUAL(num_mismatches, 0, framework::LogLevel::ERRORS);
520 }
521
522 if(num_elements_target > 0)
523 {
524 std::tie(num_missing, num_mismatches) = compare_keypoints(target_first, target_last, reference_first, reference_last, tolerance);
525
526 const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f;
527
528 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) are not part of target");
529 ARM_COMPUTE_EXPECT_EQUAL(num_missing, 0, framework::LogLevel::ERRORS);
530 }
531}
532
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100533template <typename T, typename U>
Isabella Gottardi83be7452017-08-29 13:47:03 +0100534void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value, float tolerance_number)
535{
536 int64_t num_mismatches = 0;
537 int64_t num_elements = 0;
538
539 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
540 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
541
542 if(reference.format() != Format::UNKNOWN)
543 {
544 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
545 }
546
547 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
548 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
549
550 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
551 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
552
553 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
554 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
555 {
556 const Coordinates id = index2coord(reference.shape(), element_idx);
557
558 if(valid_mask[element_idx] == 1)
559 {
560 // Iterate over all channels within one element
561 for(int c = 0; c < min_channels; ++c)
562 {
563 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
564 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
565
566 if(!compare<U>(target_value, reference_value, tolerance_value))
567 {
568 ARM_COMPUTE_TEST_INFO("id = " << id);
569 ARM_COMPUTE_TEST_INFO("channel = " << c);
570 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
571 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
572 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
573 framework::ARM_COMPUTE_PRINT_INFO();
574
575 ++num_mismatches;
576 }
577
578 ++num_elements;
579 }
580 }
581 else
582 {
583 ++num_elements;
584 }
585 }
586
587 if(num_elements > 0)
588 {
589 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
590 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
591
592 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
593 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
594 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
595 }
596}
597
598template <typename T, typename U>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100599bool validate(T target, T reference, U tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100600{
601 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
602 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
603 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 +0100604
605 const bool equal = compare<U>(target, reference, tolerance);
606
607 ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS);
608
609 return equal;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100610}
John Richardsonf89a49f2017-09-05 11:21:56 +0100611
612template <typename T, typename U>
613void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference)
614{
615 ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS);
616 ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS);
617
618 ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS);
619 ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS);
620
621 for(uint32_t i = 0; i < target.min_loc.size(); ++i)
622 {
623 const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord)
624 {
625 return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y;
626 });
627
628 ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS);
629 }
630
631 for(uint32_t i = 0; i < target.max_loc.size(); ++i)
632 {
633 const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord)
634 {
635 return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y;
636 });
637
638 ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS);
639 }
640}
641
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100642} // namespace validation
643} // namespace test
644} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100645#endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */