blob: b5f3a4a57ad5b2b03330e26b805d1bd778e0ff55 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Abe Mbise562fe0f2018-02-09 14:13:02 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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#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
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000166/** Validate padding.
167 *
168 * Padding on all sides has to be the same.
169 */
170void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &width_reference, const arm_compute::PaddingSize &height_reference);
171
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172/** Validate tensors.
173 *
174 * - Dimensionality has to be the same.
175 * - All values have to match.
176 *
177 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
178 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
179 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
180 * other test cases.
181 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100182template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000183void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f, float absolute_tolerance_value = 0.f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184
185/** Validate tensors with valid region.
186 *
187 * - Dimensionality has to be the same.
188 * - All values have to match.
189 *
190 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
191 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
192 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
193 * other test cases.
194 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100195template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000196void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value = U(), float tolerance_number = 0.f, float absolute_tolerance_value = 0.f);
Isabella Gottardi62031532017-07-04 11:21:28 +0100197
Isabella Gottardi83be7452017-08-29 13:47:03 +0100198/** Validate tensors with valid mask.
199 *
200 * - Dimensionality has to be the same.
201 * - All values have to match.
202 *
203 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
204 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
205 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
206 * other test cases.
207 */
208template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000209void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value = U(), float tolerance_number = 0.f,
210 float absolute_tolerance_value = 0.f);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100211
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212/** Validate tensors against constant value.
213 *
214 * - All values have to match.
215 */
216void validate(const IAccessor &tensor, const void *reference_value);
217
218/** Validate border against a constant value.
219 *
220 * - All border values have to match the specified value if mode is CONSTANT.
221 * - All border values have to be replicated if mode is REPLICATE.
222 * - Nothing is validated for mode UNDEFINED.
223 */
224void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value);
225
226/** Validate classified labels against expected ones.
227 *
228 * - All values should match
229 */
230void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
steniu01960b0842017-06-23 11:44:34 +0100231
232/** Validate float value.
233 *
234 * - All values should match
235 */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100236template <typename T, typename U = AbsoluteTolerance<T>>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100237bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
238
239/** Validate key points. */
240template <typename T, typename U, typename V = AbsoluteTolerance<float>>
Georgios Pinitas5962f132017-12-11 16:59:29 +0000241void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance = AbsoluteTolerance<float>(),
242 float allowed_missing_percentage = 5.f, float allowed_mismatch_percentage = 5.f);
steniu01960b0842017-06-23 11:44:34 +0100243
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100244template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100245struct compare_base
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100246{
John Richardson9c450cc2017-11-22 12:00:41 +0000247 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0))
248 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100249 {
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100250 }
251
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100252 typename T::value_type _target{};
253 typename T::value_type _reference{};
254 T _tolerance{};
255};
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100256
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100257template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100258struct compare;
259
260template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100261struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100262{
263 using compare_base<AbsoluteTolerance<U>>::compare_base;
264
265 operator bool() const
266 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100267 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100268 {
269 return false;
270 }
271 else if(this->_target == this->_reference)
272 {
273 return true;
274 }
275
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100276 using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type;
277
278 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference)));
279
280 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100281 }
282};
283
284template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100285struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100286{
steniu013e05e4e2017-08-25 17:18:01 +0100287 using compare_base<RelativeTolerance<U>>::compare_base;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100288
289 operator bool() const
290 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100291 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100292 {
293 return false;
294 }
steniu013e05e4e2017-08-25 17:18:01 +0100295 else if(this->_target == this->_reference)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100296 {
297 return true;
298 }
299
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100300 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 +0100301
steniu013e05e4e2017-08-25 17:18:01 +0100302 if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon)
303 {
304 return true;
305 }
306 else
307 {
308 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
309 {
310 return false;
311 }
312
Isabella Gottardi8df6c452018-03-12 13:26:28 +0000313 const double relative_change = std::abs((static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference);
steniu013e05e4e2017-08-25 17:18:01 +0100314
315 return relative_change <= static_cast<U>(this->_tolerance);
316 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100317 }
318};
319
320template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000321void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number, float absolute_tolerance_value)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100322{
323 // Validate with valid region covering the entire shape
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000324 validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number, absolute_tolerance_value);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100325}
326
John Richardson9c450cc2017-11-22 12:00:41 +0000327template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100328void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
329{
330 // Validate with valid region covering the entire shape
331 validate_wrap(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number);
332}
333
334template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000335void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number, float absolute_tolerance_value)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100336{
337 int64_t num_mismatches = 0;
338 int64_t num_elements = 0;
339
340 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
341 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
342
343 if(reference.format() != Format::UNKNOWN)
344 {
345 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
346 }
347
348 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
349 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
350
351 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
352 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
353
354 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
355 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
356 {
357 const Coordinates id = index2coord(reference.shape(), element_idx);
358
359 if(is_in_valid_region(valid_region, id))
360 {
361 // Iterate over all channels within one element
362 for(int c = 0; c < min_channels; ++c)
363 {
364 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
365 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
366
Michalis Spyrou23fe7c22018-02-26 10:42:01 +0000367 if(!compare<U>(target_value, reference_value, tolerance_value))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100368 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000369 if(absolute_tolerance_value != 0.f)
370 {
371 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
372 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
373 {
374 continue;
375 }
376 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100377 ARM_COMPUTE_TEST_INFO("id = " << id);
378 ARM_COMPUTE_TEST_INFO("channel = " << c);
379 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
380 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
381 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 +0100382 framework::ARM_COMPUTE_PRINT_INFO();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100383
384 ++num_mismatches;
385 }
386
387 ++num_elements;
388 }
389 }
390 }
391
392 if(num_elements > 0)
393 {
394 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
395 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
396
397 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
398 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
399 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100400 }
401}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100402
John Richardson9c450cc2017-11-22 12:00:41 +0000403template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100404void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
405{
406 int64_t num_mismatches = 0;
407 int64_t num_elements = 0;
408
409 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
410 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
411
412 if(reference.format() != Format::UNKNOWN)
413 {
414 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
415 }
416
417 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
418 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
419
420 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
421 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
422
423 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
424 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
425 {
426 const Coordinates id = index2coord(reference.shape(), element_idx);
427
428 if(is_in_valid_region(valid_region, id))
429 {
430 // Iterate over all channels within one element
431 for(int c = 0; c < min_channels; ++c)
432 {
433 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
434 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
435
436 bool equal = compare<U>(target_value, reference_value, tolerance_value);
437
John Richardson9c450cc2017-11-22 12:00:41 +0000438 // check for wrapping
John Richardsondd715f22017-09-18 16:10:48 +0100439 if(!equal)
440 {
John Richardson9c450cc2017-11-22 12:00:41 +0000441 if(!support::cpp11::isfinite(target_value) || !support::cpp11::isfinite(reference_value))
442 {
443 equal = false;
444 }
445 else
446 {
447 using limits_type = typename std::make_unsigned<T>::type;
448
449 uint64_t max = std::numeric_limits<limits_type>::max();
450 uint64_t abs_sum = std::abs(static_cast<int64_t>(target_value)) + std::abs(static_cast<int64_t>(reference_value));
451 uint64_t wrap_difference = max - abs_sum;
452
453 equal = wrap_difference < static_cast<uint64_t>(tolerance_value);
454 }
John Richardsondd715f22017-09-18 16:10:48 +0100455 }
456
457 if(!equal)
458 {
459 ARM_COMPUTE_TEST_INFO("id = " << id);
460 ARM_COMPUTE_TEST_INFO("channel = " << c);
461 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
462 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
John Richardson9c450cc2017-11-22 12:00:41 +0000463 ARM_COMPUTE_TEST_INFO("wrap_tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
John Richardsondd715f22017-09-18 16:10:48 +0100464 framework::ARM_COMPUTE_PRINT_INFO();
465
466 ++num_mismatches;
467 }
468
469 ++num_elements;
470 }
471 }
472 }
473
474 if(num_elements > 0)
475 {
476 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
477 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
478
479 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
480 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
481 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
482 }
483}
484
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100485template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000486void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value, float tolerance_number, float absolute_tolerance_value)
Isabella Gottardi83be7452017-08-29 13:47:03 +0100487{
488 int64_t num_mismatches = 0;
489 int64_t num_elements = 0;
490
491 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
492 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
493
494 if(reference.format() != Format::UNKNOWN)
495 {
496 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
497 }
498
499 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
500 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
501
502 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
503 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
504
505 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
506 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
507 {
508 const Coordinates id = index2coord(reference.shape(), element_idx);
509
510 if(valid_mask[element_idx] == 1)
511 {
512 // Iterate over all channels within one element
513 for(int c = 0; c < min_channels; ++c)
514 {
515 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
516 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
517
518 if(!compare<U>(target_value, reference_value, tolerance_value))
519 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000520 if(absolute_tolerance_value != 0.f)
521 {
522 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
523 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
524 {
525 continue;
526 }
527 }
Isabella Gottardi83be7452017-08-29 13:47:03 +0100528 ARM_COMPUTE_TEST_INFO("id = " << id);
529 ARM_COMPUTE_TEST_INFO("channel = " << c);
530 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
531 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
532 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
533 framework::ARM_COMPUTE_PRINT_INFO();
534
535 ++num_mismatches;
536 }
537
538 ++num_elements;
539 }
540 }
541 else
542 {
543 ++num_elements;
544 }
545 }
546
547 if(num_elements > 0)
548 {
549 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
550 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
551
552 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
553 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
554 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
555 }
556}
557
558template <typename T, typename U>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100559bool validate(T target, T reference, U tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100560{
561 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
562 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
563 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 +0100564
565 const bool equal = compare<U>(target, reference, tolerance);
566
567 ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS);
568
569 return equal;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100570}
John Richardsonf89a49f2017-09-05 11:21:56 +0100571
572template <typename T, typename U>
573void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference)
574{
575 ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS);
576 ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS);
577
578 ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS);
579 ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS);
580
581 for(uint32_t i = 0; i < target.min_loc.size(); ++i)
582 {
583 const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord)
584 {
585 return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y;
586 });
587
588 ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS);
589 }
590
591 for(uint32_t i = 0; i < target.max_loc.size(); ++i)
592 {
593 const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord)
594 {
595 return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y;
596 });
597
598 ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS);
599 }
600}
601
Abe Mbise562fe0f2018-02-09 14:13:02 +0000602/** Check which keypoints from [first1, last1) are missing in [first2, last2) */
603template <typename T, typename U, typename V>
604std::pair<int64_t, int64_t> compare_keypoints(T first1, T last1, U first2, U last2, V tolerance, bool check_mismatches = true)
605{
606 /* Keypoint (x,y) should have similar strength (within tolerance) and other properties in both reference and target */
607 const auto compare_props_eq = [&](const KeyPoint & lhs, const KeyPoint & rhs)
608 {
609 return compare<V>(lhs.strength, rhs.strength, tolerance)
610 && lhs.tracking_status == rhs.tracking_status
611 && lhs.scale == rhs.scale
612 && lhs.orientation == rhs.orientation
613 && lhs.error == rhs.error;
614 };
615
616 /* Used to sort KeyPoints by coordinates (x, y) */
617 const auto compare_coords_lt = [](const KeyPoint & lhs, const KeyPoint & rhs)
618 {
619 return std::tie(lhs.x, lhs.y) < std::tie(rhs.x, rhs.y);
620 };
621
622 std::sort(first1, last1, compare_coords_lt);
623 std::sort(first2, last2, compare_coords_lt);
624
625 if(check_mismatches)
626 {
627 ARM_COMPUTE_TEST_INFO("Checking for mismatches: ref count = " << std::distance(first1, last1) << " \ttarget count = " << std::distance(first2, last2));
628 }
629
630 int64_t num_missing = 0;
631 int64_t num_mismatches = 0;
632 bool rest_missing = false;
633
634 while(first1 != last1)
635 {
636 if(first2 == last2)
637 {
638 rest_missing = true;
639 break;
640 }
641
642 if(compare_coords_lt(*first1, *first2))
643 {
644 ++num_missing;
645 ARM_COMPUTE_TEST_INFO("Key point not found");
646 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++);
647 }
648 else
649 {
650 if(!compare_coords_lt(*first2, *first1)) // Equal coordinates
651 {
652 if(check_mismatches && !compare_props_eq(*first1, *first2)) // Check other properties
653 {
654 ++num_mismatches;
655 ARM_COMPUTE_TEST_INFO("Mismatching keypoint");
656 ARM_COMPUTE_TEST_INFO("keypoint1 [ref] = " << *first1);
657 ARM_COMPUTE_TEST_INFO("keypoint2 [tgt] = " << *first2);
658 }
659 ++first1;
660 }
661 ++first2;
662 }
663 }
664
665 if(rest_missing)
666 {
667 while(first1 != last1)
668 {
669 ++num_missing;
670 ARM_COMPUTE_TEST_INFO("Key point not found");
671 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++);
672 }
673 }
674
675 return std::make_pair(num_missing, num_mismatches);
676}
677
678template <typename T, typename U, typename V>
679void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance, float allowed_missing_percentage, float allowed_mismatch_percentage)
680{
681 const int64_t num_elements_target = std::distance(target_first, target_last);
682 const int64_t num_elements_reference = std::distance(reference_first, reference_last);
683
684 int64_t num_missing = 0;
685 int64_t num_mismatches = 0;
686
687 if(num_elements_reference > 0)
688 {
689 std::tie(num_missing, num_mismatches) = compare_keypoints(reference_first, reference_last, target_first, target_last, tolerance);
690
691 const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f;
692 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f;
693
694 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in ref are missing from target");
695 ARM_COMPUTE_TEST_INFO("Missing (not in tgt): " << num_missing << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_missing
696 << "% \tMax allowed: " << allowed_missing_percentage << "%");
697 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
698
699 ARM_COMPUTE_TEST_INFO(num_mismatches << " keypoints (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched");
700 ARM_COMPUTE_TEST_INFO("Mismatched keypoints: " << num_mismatches << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_mismatches
701 << "% \tMax allowed: " << allowed_mismatch_percentage << "%");
702 ARM_COMPUTE_EXPECT(percent_mismatches <= allowed_mismatch_percentage, framework::LogLevel::ERRORS);
703 }
704
705 if(num_elements_target > 0)
706 {
707 // Note: no need to check for mismatches a second time (last argument is 'false')
708 std::tie(num_missing, num_mismatches) = compare_keypoints(target_first, target_last, reference_first, reference_last, tolerance, false);
709
710 const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f;
711
712 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in target are missing from ref");
713 ARM_COMPUTE_TEST_INFO("Missing (not in ref): " << num_missing << "/" << num_elements_target << " = " << std::fixed << std::setprecision(2) << percent_missing
714 << "% \tMax allowed: " << allowed_missing_percentage << "%");
715 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
716 }
717}
718
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100719} // namespace validation
720} // namespace test
721} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100722#endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */