blob: 508fb027ca33f4107a723363ed79f83abb5e9635 [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
Alex Gildayc357c472018-03-21 13:54:09 +000072 /** Implicit conversion to the underlying type.
73 *
74 * @return the underlying type.
75 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010076 constexpr operator T() const
77 {
78 return _value;
79 }
80
81private:
82 T _value{ std::numeric_limits<T>::epsilon() };
83};
84
85/** Class reprensenting a relative tolerance value. */
steniu013e05e4e2017-08-25 17:18:01 +010086template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010087class RelativeTolerance
88{
89public:
90 /** Underlying type. */
steniu013e05e4e2017-08-25 17:18:01 +010091 using value_type = T;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010092
93 /* Default constructor.
94 *
95 * Initialises the tolerance to 0.
96 */
97 RelativeTolerance() = default;
98
99 /** Constructor.
100 *
101 * @param[in] value Relative tolerance value.
102 */
103 explicit constexpr RelativeTolerance(value_type value)
104 : _value{ value }
105 {
106 }
107
Alex Gildayc357c472018-03-21 13:54:09 +0000108 /** Implicit conversion to the underlying type.
109 *
110 * @return the underlying type.
111 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100112 constexpr operator value_type() const
113 {
114 return _value;
115 }
116
117private:
steniu013e05e4e2017-08-25 17:18:01 +0100118 value_type _value{ std::numeric_limits<T>::epsilon() };
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100119};
120
121/** Print AbsoluteTolerance type. */
122template <typename T>
123inline ::std::ostream &operator<<(::std::ostream &os, const AbsoluteTolerance<T> &tolerance)
124{
125 os << static_cast<typename AbsoluteTolerance<T>::value_type>(tolerance);
126
127 return os;
128}
129
130/** Print RelativeTolerance type. */
steniu013e05e4e2017-08-25 17:18:01 +0100131template <typename T>
132inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100133{
steniu013e05e4e2017-08-25 17:18:01 +0100134 os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100135
136 return os;
137}
138
139template <typename T>
140bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141{
142 if(dimensions1.num_dimensions() != dimensions2.num_dimensions())
143 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100144 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 }
146
147 for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i)
148 {
149 if(dimensions1[i] != dimensions2[i])
150 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100151 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 }
153 }
154
155 return true;
156}
157
158/** Validate valid regions.
159 *
160 * - Dimensionality has to be the same.
161 * - Anchors have to match.
162 * - Shapes have to match.
163 */
164void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference);
165
166/** 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 &reference);
171
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000172/** Validate padding.
173 *
174 * Padding on all sides has to be the same.
175 */
176void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &width_reference, const arm_compute::PaddingSize &height_reference);
177
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178/** Validate tensors.
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>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000189void 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 +0100190
191/** Validate tensors with valid region.
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 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100201template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000202void 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 +0100203
Isabella Gottardi83be7452017-08-29 13:47:03 +0100204/** Validate tensors with valid mask.
205 *
206 * - Dimensionality has to be the same.
207 * - All values have to match.
208 *
209 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
210 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
211 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
212 * other test cases.
213 */
214template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000215void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value = U(), float tolerance_number = 0.f,
216 float absolute_tolerance_value = 0.f);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100217
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218/** Validate tensors against constant value.
219 *
220 * - All values have to match.
221 */
222void validate(const IAccessor &tensor, const void *reference_value);
223
224/** Validate border against a constant value.
225 *
226 * - All border values have to match the specified value if mode is CONSTANT.
227 * - All border values have to be replicated if mode is REPLICATE.
228 * - Nothing is validated for mode UNDEFINED.
229 */
230void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value);
231
232/** Validate classified labels against expected ones.
233 *
234 * - All values should match
235 */
236void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
steniu01960b0842017-06-23 11:44:34 +0100237
238/** Validate float value.
239 *
240 * - All values should match
241 */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100242template <typename T, typename U = AbsoluteTolerance<T>>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100243bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
244
245/** Validate key points. */
246template <typename T, typename U, typename V = AbsoluteTolerance<float>>
Georgios Pinitas5962f132017-12-11 16:59:29 +0000247void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance = AbsoluteTolerance<float>(),
248 float allowed_missing_percentage = 5.f, float allowed_mismatch_percentage = 5.f);
steniu01960b0842017-06-23 11:44:34 +0100249
John Richardson684cb0f2018-01-09 11:17:00 +0000250/** Validate detection windows. */
251template <typename T, typename U, typename V = AbsoluteTolerance<float>>
252void validate_detection_windows(T target_first, T target_last, U reference_first, U reference_last, V tolerance = AbsoluteTolerance<float>(),
253 float allowed_missing_percentage = 5.f, float allowed_mismatch_percentage = 5.f);
254
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100255template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100256struct compare_base
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100257{
Alex Gildayc357c472018-03-21 13:54:09 +0000258 /** Construct a comparison object.
259 *
260 * @param[in] target Target value.
261 * @param[in] reference Reference value.
262 * @param[in] tolerance Allowed tolerance.
263 */
John Richardson9c450cc2017-11-22 12:00:41 +0000264 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0))
265 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100266 {
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100267 }
268
Alex Gildayc357c472018-03-21 13:54:09 +0000269 typename T::value_type _target{}; /**< Target value */
270 typename T::value_type _reference{}; /**< Reference value */
271 T _tolerance{}; /**< Tolerance value */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100272};
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100273
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100274template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100275struct compare;
276
Alex Gildayc357c472018-03-21 13:54:09 +0000277/** Compare values with an absolute tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100278template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100279struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100280{
281 using compare_base<AbsoluteTolerance<U>>::compare_base;
282
Alex Gildayc357c472018-03-21 13:54:09 +0000283 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100284 operator bool() const
285 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100286 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100287 {
288 return false;
289 }
290 else if(this->_target == this->_reference)
291 {
292 return true;
293 }
294
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100295 using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type;
296
297 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference)));
298
299 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100300 }
301};
302
Alex Gildayc357c472018-03-21 13:54:09 +0000303/** Compare values with a relative tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100304template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100305struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100306{
steniu013e05e4e2017-08-25 17:18:01 +0100307 using compare_base<RelativeTolerance<U>>::compare_base;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100308
Alex Gildayc357c472018-03-21 13:54:09 +0000309 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100310 operator bool() const
311 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100312 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100313 {
314 return false;
315 }
steniu013e05e4e2017-08-25 17:18:01 +0100316 else if(this->_target == this->_reference)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100317 {
318 return true;
319 }
320
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100321 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 +0100322
steniu013e05e4e2017-08-25 17:18:01 +0100323 if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon)
324 {
325 return true;
326 }
327 else
328 {
329 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
330 {
331 return false;
332 }
333
Isabella Gottardi8df6c452018-03-12 13:26:28 +0000334 const double relative_change = std::abs((static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference);
steniu013e05e4e2017-08-25 17:18:01 +0100335
336 return relative_change <= static_cast<U>(this->_tolerance);
337 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100338 }
339};
340
341template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000342void 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 +0100343{
344 // Validate with valid region covering the entire shape
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000345 validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number, absolute_tolerance_value);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100346}
347
John Richardson9c450cc2017-11-22 12:00:41 +0000348template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100349void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
350{
351 // Validate with valid region covering the entire shape
352 validate_wrap(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number);
353}
354
355template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000356void 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 +0100357{
358 int64_t num_mismatches = 0;
359 int64_t num_elements = 0;
360
361 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
362 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
363
364 if(reference.format() != Format::UNKNOWN)
365 {
366 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
367 }
368
369 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
370 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
371
372 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
373 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
374
375 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
376 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
377 {
378 const Coordinates id = index2coord(reference.shape(), element_idx);
379
380 if(is_in_valid_region(valid_region, id))
381 {
382 // Iterate over all channels within one element
383 for(int c = 0; c < min_channels; ++c)
384 {
385 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
386 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
387
Michalis Spyrou23fe7c22018-02-26 10:42:01 +0000388 if(!compare<U>(target_value, reference_value, tolerance_value))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100389 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000390 if(absolute_tolerance_value != 0.f)
391 {
392 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
393 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
394 {
395 continue;
396 }
397 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100398 ARM_COMPUTE_TEST_INFO("id = " << id);
399 ARM_COMPUTE_TEST_INFO("channel = " << c);
400 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
401 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
402 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 +0100403 framework::ARM_COMPUTE_PRINT_INFO();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100404
405 ++num_mismatches;
406 }
407
408 ++num_elements;
409 }
410 }
411 }
412
413 if(num_elements > 0)
414 {
415 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
416 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
417
418 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
419 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
420 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100421 }
422}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100423
John Richardson9c450cc2017-11-22 12:00:41 +0000424template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100425void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
426{
427 int64_t num_mismatches = 0;
428 int64_t num_elements = 0;
429
430 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
431 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
432
433 if(reference.format() != Format::UNKNOWN)
434 {
435 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
436 }
437
438 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
439 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
440
441 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
442 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
443
444 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
445 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
446 {
447 const Coordinates id = index2coord(reference.shape(), element_idx);
448
449 if(is_in_valid_region(valid_region, id))
450 {
451 // Iterate over all channels within one element
452 for(int c = 0; c < min_channels; ++c)
453 {
454 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
455 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
456
457 bool equal = compare<U>(target_value, reference_value, tolerance_value);
458
John Richardson9c450cc2017-11-22 12:00:41 +0000459 // check for wrapping
John Richardsondd715f22017-09-18 16:10:48 +0100460 if(!equal)
461 {
John Richardson9c450cc2017-11-22 12:00:41 +0000462 if(!support::cpp11::isfinite(target_value) || !support::cpp11::isfinite(reference_value))
463 {
464 equal = false;
465 }
466 else
467 {
468 using limits_type = typename std::make_unsigned<T>::type;
469
470 uint64_t max = std::numeric_limits<limits_type>::max();
471 uint64_t abs_sum = std::abs(static_cast<int64_t>(target_value)) + std::abs(static_cast<int64_t>(reference_value));
472 uint64_t wrap_difference = max - abs_sum;
473
474 equal = wrap_difference < static_cast<uint64_t>(tolerance_value);
475 }
John Richardsondd715f22017-09-18 16:10:48 +0100476 }
477
478 if(!equal)
479 {
480 ARM_COMPUTE_TEST_INFO("id = " << id);
481 ARM_COMPUTE_TEST_INFO("channel = " << c);
482 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
483 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
John Richardson9c450cc2017-11-22 12:00:41 +0000484 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 +0100485 framework::ARM_COMPUTE_PRINT_INFO();
486
487 ++num_mismatches;
488 }
489
490 ++num_elements;
491 }
492 }
493 }
494
495 if(num_elements > 0)
496 {
497 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
498 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
499
500 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
501 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
502 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
503 }
504}
505
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100506template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000507void 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 +0100508{
509 int64_t num_mismatches = 0;
510 int64_t num_elements = 0;
511
512 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
513 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
514
515 if(reference.format() != Format::UNKNOWN)
516 {
517 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
518 }
519
520 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
521 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
522
523 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
524 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
525
526 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
527 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
528 {
529 const Coordinates id = index2coord(reference.shape(), element_idx);
530
531 if(valid_mask[element_idx] == 1)
532 {
533 // Iterate over all channels within one element
534 for(int c = 0; c < min_channels; ++c)
535 {
536 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
537 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
538
539 if(!compare<U>(target_value, reference_value, tolerance_value))
540 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000541 if(absolute_tolerance_value != 0.f)
542 {
543 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
544 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
545 {
546 continue;
547 }
548 }
Isabella Gottardi83be7452017-08-29 13:47:03 +0100549 ARM_COMPUTE_TEST_INFO("id = " << id);
550 ARM_COMPUTE_TEST_INFO("channel = " << c);
551 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
552 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
553 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
554 framework::ARM_COMPUTE_PRINT_INFO();
555
556 ++num_mismatches;
557 }
558
559 ++num_elements;
560 }
561 }
562 else
563 {
564 ++num_elements;
565 }
566 }
567
568 if(num_elements > 0)
569 {
570 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
571 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
572
573 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
574 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
575 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
576 }
577}
578
579template <typename T, typename U>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100580bool validate(T target, T reference, U tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100581{
582 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
583 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
584 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 +0100585
586 const bool equal = compare<U>(target, reference, tolerance);
587
588 ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS);
589
590 return equal;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100591}
John Richardsonf89a49f2017-09-05 11:21:56 +0100592
593template <typename T, typename U>
594void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference)
595{
596 ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS);
597 ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS);
598
599 ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS);
600 ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS);
601
602 for(uint32_t i = 0; i < target.min_loc.size(); ++i)
603 {
604 const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord)
605 {
606 return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y;
607 });
608
609 ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS);
610 }
611
612 for(uint32_t i = 0; i < target.max_loc.size(); ++i)
613 {
614 const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord)
615 {
616 return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y;
617 });
618
619 ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS);
620 }
621}
622
Abe Mbise562fe0f2018-02-09 14:13:02 +0000623/** Check which keypoints from [first1, last1) are missing in [first2, last2) */
624template <typename T, typename U, typename V>
625std::pair<int64_t, int64_t> compare_keypoints(T first1, T last1, U first2, U last2, V tolerance, bool check_mismatches = true)
626{
627 /* Keypoint (x,y) should have similar strength (within tolerance) and other properties in both reference and target */
628 const auto compare_props_eq = [&](const KeyPoint & lhs, const KeyPoint & rhs)
629 {
630 return compare<V>(lhs.strength, rhs.strength, tolerance)
631 && lhs.tracking_status == rhs.tracking_status
632 && lhs.scale == rhs.scale
633 && lhs.orientation == rhs.orientation
634 && lhs.error == rhs.error;
635 };
636
637 /* Used to sort KeyPoints by coordinates (x, y) */
638 const auto compare_coords_lt = [](const KeyPoint & lhs, const KeyPoint & rhs)
639 {
640 return std::tie(lhs.x, lhs.y) < std::tie(rhs.x, rhs.y);
641 };
642
643 std::sort(first1, last1, compare_coords_lt);
644 std::sort(first2, last2, compare_coords_lt);
645
646 if(check_mismatches)
647 {
648 ARM_COMPUTE_TEST_INFO("Checking for mismatches: ref count = " << std::distance(first1, last1) << " \ttarget count = " << std::distance(first2, last2));
649 }
650
651 int64_t num_missing = 0;
652 int64_t num_mismatches = 0;
653 bool rest_missing = false;
654
655 while(first1 != last1)
656 {
657 if(first2 == last2)
658 {
659 rest_missing = true;
660 break;
661 }
662
663 if(compare_coords_lt(*first1, *first2))
664 {
665 ++num_missing;
666 ARM_COMPUTE_TEST_INFO("Key point not found");
667 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++);
668 }
669 else
670 {
671 if(!compare_coords_lt(*first2, *first1)) // Equal coordinates
672 {
673 if(check_mismatches && !compare_props_eq(*first1, *first2)) // Check other properties
674 {
675 ++num_mismatches;
676 ARM_COMPUTE_TEST_INFO("Mismatching keypoint");
677 ARM_COMPUTE_TEST_INFO("keypoint1 [ref] = " << *first1);
678 ARM_COMPUTE_TEST_INFO("keypoint2 [tgt] = " << *first2);
679 }
680 ++first1;
681 }
682 ++first2;
683 }
684 }
685
686 if(rest_missing)
687 {
688 while(first1 != last1)
689 {
690 ++num_missing;
691 ARM_COMPUTE_TEST_INFO("Key point not found");
692 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++);
693 }
694 }
695
696 return std::make_pair(num_missing, num_mismatches);
697}
698
699template <typename T, typename U, typename V>
700void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance, float allowed_missing_percentage, float allowed_mismatch_percentage)
701{
702 const int64_t num_elements_target = std::distance(target_first, target_last);
703 const int64_t num_elements_reference = std::distance(reference_first, reference_last);
704
705 int64_t num_missing = 0;
706 int64_t num_mismatches = 0;
707
708 if(num_elements_reference > 0)
709 {
710 std::tie(num_missing, num_mismatches) = compare_keypoints(reference_first, reference_last, target_first, target_last, tolerance);
711
712 const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f;
713 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f;
714
715 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in ref are missing from target");
716 ARM_COMPUTE_TEST_INFO("Missing (not in tgt): " << num_missing << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_missing
717 << "% \tMax allowed: " << allowed_missing_percentage << "%");
718 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
719
720 ARM_COMPUTE_TEST_INFO(num_mismatches << " keypoints (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched");
721 ARM_COMPUTE_TEST_INFO("Mismatched keypoints: " << num_mismatches << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_mismatches
722 << "% \tMax allowed: " << allowed_mismatch_percentage << "%");
723 ARM_COMPUTE_EXPECT(percent_mismatches <= allowed_mismatch_percentage, framework::LogLevel::ERRORS);
724 }
725
726 if(num_elements_target > 0)
727 {
728 // Note: no need to check for mismatches a second time (last argument is 'false')
729 std::tie(num_missing, num_mismatches) = compare_keypoints(target_first, target_last, reference_first, reference_last, tolerance, false);
730
731 const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f;
732
733 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in target are missing from ref");
734 ARM_COMPUTE_TEST_INFO("Missing (not in ref): " << num_missing << "/" << num_elements_target << " = " << std::fixed << std::setprecision(2) << percent_missing
735 << "% \tMax allowed: " << allowed_missing_percentage << "%");
736 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
737 }
738}
739
John Richardson684cb0f2018-01-09 11:17:00 +0000740/** Check which detection windows from [first1, last1) are missing in [first2, last2) */
741template <typename T, typename U, typename V>
742std::pair<int64_t, int64_t> compare_detection_windows(T first1, T last1, U first2, U last2, V tolerance)
743{
744 int64_t num_missing = 0;
745 int64_t num_mismatches = 0;
746
747 while(first1 != last1)
748 {
749 const auto window = std::find_if(first2, last2, [&](DetectionWindow window)
750 {
751 return window.x == first1->x && window.y == first1->y && window.width == first1->width && window.height == first1->height && window.idx_class == first1->idx_class;
752 });
753
754 if(window == last2)
755 {
756 ++num_missing;
757 ARM_COMPUTE_TEST_INFO("Detection window not found " << *first1)
758 }
759 else
760 {
761 if(!compare<V>(window->score, first1->score, tolerance))
762 {
763 ++num_mismatches;
764 ARM_COMPUTE_TEST_INFO("Mismatching detection window")
765 ARM_COMPUTE_TEST_INFO("detection window 1= " << *first1)
766 ARM_COMPUTE_TEST_INFO("detection window 2= " << *window)
767 }
768 }
769
770 ++first1;
771 }
772
773 return std::make_pair(num_missing, num_mismatches);
774}
775
776template <typename T, typename U, typename V>
777void validate_detection_windows(T target_first, T target_last, U reference_first, U reference_last, V tolerance,
778 float allowed_missing_percentage, float allowed_mismatch_percentage)
779{
780 const int64_t num_elements_target = std::distance(target_first, target_last);
781 const int64_t num_elements_reference = std::distance(reference_first, reference_last);
782
783 int64_t num_missing = 0;
784 int64_t num_mismatches = 0;
785
786 if(num_elements_reference > 0)
787 {
788 std::tie(num_missing, num_mismatches) = compare_detection_windows(reference_first, reference_last, target_first, target_last, tolerance);
789
790 const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f;
791 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f;
792
793 ARM_COMPUTE_TEST_INFO(num_missing << " detection windows (" << std::fixed << std::setprecision(2) << percent_missing << "%) are missing in target");
794 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
795
796 ARM_COMPUTE_TEST_INFO(num_mismatches << " detection windows (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched");
797 ARM_COMPUTE_EXPECT(percent_mismatches <= allowed_mismatch_percentage, framework::LogLevel::ERRORS);
798 }
799
800 if(num_elements_target > 0)
801 {
802 std::tie(num_missing, num_mismatches) = compare_detection_windows(target_first, target_last, reference_first, reference_last, tolerance);
803
804 const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f;
805
806 ARM_COMPUTE_TEST_INFO(num_missing << " detection windows (" << std::fixed << std::setprecision(2) << percent_missing << "%) are not part of target");
807 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
808 }
809}
810
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100811} // namespace validation
812} // namespace test
813} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100814#endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */