blob: 26271c8bf3819884aaeaa705f8fdff0fb77aabdf [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
Alex Gildayc357c472018-03-21 13:54:09 +0000250/** Compare values with a tolerance. */
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100251template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100252struct compare_base
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100253{
Alex Gildayc357c472018-03-21 13:54:09 +0000254 /** Construct a comparison object.
255 *
256 * @param[in] target Target value.
257 * @param[in] reference Reference value.
258 * @param[in] tolerance Allowed tolerance.
259 */
John Richardson9c450cc2017-11-22 12:00:41 +0000260 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0))
261 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100262 {
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100263 }
264
Alex Gildayc357c472018-03-21 13:54:09 +0000265 typename T::value_type _target{}; /**< Target value */
266 typename T::value_type _reference{}; /**< Reference value */
267 T _tolerance{}; /**< Tolerance value */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100268};
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100269
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100270template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100271struct compare;
272
Alex Gildayc357c472018-03-21 13:54:09 +0000273/** Compare values with an absolute tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100274template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100275struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100276{
277 using compare_base<AbsoluteTolerance<U>>::compare_base;
278
Alex Gildayc357c472018-03-21 13:54:09 +0000279 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100280 operator bool() const
281 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100282 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100283 {
284 return false;
285 }
286 else if(this->_target == this->_reference)
287 {
288 return true;
289 }
290
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100291 using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type;
292
293 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference)));
294
295 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100296 }
297};
298
Alex Gildayc357c472018-03-21 13:54:09 +0000299/** Compare values with a relative tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100300template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100301struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100302{
steniu013e05e4e2017-08-25 17:18:01 +0100303 using compare_base<RelativeTolerance<U>>::compare_base;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100304
Alex Gildayc357c472018-03-21 13:54:09 +0000305 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100306 operator bool() const
307 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100308 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100309 {
310 return false;
311 }
steniu013e05e4e2017-08-25 17:18:01 +0100312 else if(this->_target == this->_reference)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100313 {
314 return true;
315 }
316
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100317 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 +0100318
steniu013e05e4e2017-08-25 17:18:01 +0100319 if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon)
320 {
321 return true;
322 }
323 else
324 {
325 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
326 {
327 return false;
328 }
329
Isabella Gottardi8df6c452018-03-12 13:26:28 +0000330 const double relative_change = std::abs((static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference);
steniu013e05e4e2017-08-25 17:18:01 +0100331
332 return relative_change <= static_cast<U>(this->_tolerance);
333 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100334 }
335};
336
337template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000338void 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 +0100339{
340 // Validate with valid region covering the entire shape
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000341 validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number, absolute_tolerance_value);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100342}
343
John Richardson9c450cc2017-11-22 12:00:41 +0000344template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100345void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
346{
347 // Validate with valid region covering the entire shape
348 validate_wrap(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number);
349}
350
351template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000352void 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 +0100353{
354 int64_t num_mismatches = 0;
355 int64_t num_elements = 0;
356
357 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
358 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
359
360 if(reference.format() != Format::UNKNOWN)
361 {
362 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
363 }
364
365 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
366 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
367
368 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
369 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
370
371 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
372 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
373 {
374 const Coordinates id = index2coord(reference.shape(), element_idx);
375
376 if(is_in_valid_region(valid_region, id))
377 {
378 // Iterate over all channels within one element
379 for(int c = 0; c < min_channels; ++c)
380 {
381 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
382 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
383
Michalis Spyrou23fe7c22018-02-26 10:42:01 +0000384 if(!compare<U>(target_value, reference_value, tolerance_value))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100385 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000386 if(absolute_tolerance_value != 0.f)
387 {
388 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
389 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
390 {
391 continue;
392 }
393 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100394 ARM_COMPUTE_TEST_INFO("id = " << id);
395 ARM_COMPUTE_TEST_INFO("channel = " << c);
396 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
397 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
398 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 +0100399 framework::ARM_COMPUTE_PRINT_INFO();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100400
401 ++num_mismatches;
402 }
403
404 ++num_elements;
405 }
406 }
407 }
408
409 if(num_elements > 0)
410 {
411 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
412 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
413
414 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
415 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
416 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100417 }
418}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100419
John Richardson9c450cc2017-11-22 12:00:41 +0000420template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100421void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
422{
423 int64_t num_mismatches = 0;
424 int64_t num_elements = 0;
425
426 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
427 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
428
429 if(reference.format() != Format::UNKNOWN)
430 {
431 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
432 }
433
434 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
435 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
436
437 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
438 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
439
440 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
441 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
442 {
443 const Coordinates id = index2coord(reference.shape(), element_idx);
444
445 if(is_in_valid_region(valid_region, id))
446 {
447 // Iterate over all channels within one element
448 for(int c = 0; c < min_channels; ++c)
449 {
450 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
451 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
452
453 bool equal = compare<U>(target_value, reference_value, tolerance_value);
454
John Richardson9c450cc2017-11-22 12:00:41 +0000455 // check for wrapping
John Richardsondd715f22017-09-18 16:10:48 +0100456 if(!equal)
457 {
John Richardson9c450cc2017-11-22 12:00:41 +0000458 if(!support::cpp11::isfinite(target_value) || !support::cpp11::isfinite(reference_value))
459 {
460 equal = false;
461 }
462 else
463 {
464 using limits_type = typename std::make_unsigned<T>::type;
465
466 uint64_t max = std::numeric_limits<limits_type>::max();
467 uint64_t abs_sum = std::abs(static_cast<int64_t>(target_value)) + std::abs(static_cast<int64_t>(reference_value));
468 uint64_t wrap_difference = max - abs_sum;
469
470 equal = wrap_difference < static_cast<uint64_t>(tolerance_value);
471 }
John Richardsondd715f22017-09-18 16:10:48 +0100472 }
473
474 if(!equal)
475 {
476 ARM_COMPUTE_TEST_INFO("id = " << id);
477 ARM_COMPUTE_TEST_INFO("channel = " << c);
478 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
479 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
John Richardson9c450cc2017-11-22 12:00:41 +0000480 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 +0100481 framework::ARM_COMPUTE_PRINT_INFO();
482
483 ++num_mismatches;
484 }
485
486 ++num_elements;
487 }
488 }
489 }
490
491 if(num_elements > 0)
492 {
493 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
494 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
495
496 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
497 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
498 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
499 }
500}
501
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100502template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000503void 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 +0100504{
505 int64_t num_mismatches = 0;
506 int64_t num_elements = 0;
507
508 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
509 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
510
511 if(reference.format() != Format::UNKNOWN)
512 {
513 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
514 }
515
516 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
517 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
518
519 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
520 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
521
522 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
523 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
524 {
525 const Coordinates id = index2coord(reference.shape(), element_idx);
526
527 if(valid_mask[element_idx] == 1)
528 {
529 // Iterate over all channels within one element
530 for(int c = 0; c < min_channels; ++c)
531 {
532 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
533 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
534
535 if(!compare<U>(target_value, reference_value, tolerance_value))
536 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000537 if(absolute_tolerance_value != 0.f)
538 {
539 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
540 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
541 {
542 continue;
543 }
544 }
Isabella Gottardi83be7452017-08-29 13:47:03 +0100545 ARM_COMPUTE_TEST_INFO("id = " << id);
546 ARM_COMPUTE_TEST_INFO("channel = " << c);
547 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
548 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
549 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
550 framework::ARM_COMPUTE_PRINT_INFO();
551
552 ++num_mismatches;
553 }
554
555 ++num_elements;
556 }
557 }
558 else
559 {
560 ++num_elements;
561 }
562 }
563
564 if(num_elements > 0)
565 {
566 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
567 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
568
569 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
570 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
571 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
572 }
573}
574
575template <typename T, typename U>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100576bool validate(T target, T reference, U tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100577{
578 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
579 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
580 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 +0100581
582 const bool equal = compare<U>(target, reference, tolerance);
583
584 ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS);
585
586 return equal;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100587}
John Richardsonf89a49f2017-09-05 11:21:56 +0100588
589template <typename T, typename U>
590void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference)
591{
592 ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS);
593 ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS);
594
595 ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS);
596 ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS);
597
598 for(uint32_t i = 0; i < target.min_loc.size(); ++i)
599 {
600 const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord)
601 {
602 return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y;
603 });
604
605 ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS);
606 }
607
608 for(uint32_t i = 0; i < target.max_loc.size(); ++i)
609 {
610 const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord)
611 {
612 return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y;
613 });
614
615 ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS);
616 }
617}
618
Abe Mbise562fe0f2018-02-09 14:13:02 +0000619/** Check which keypoints from [first1, last1) are missing in [first2, last2) */
620template <typename T, typename U, typename V>
621std::pair<int64_t, int64_t> compare_keypoints(T first1, T last1, U first2, U last2, V tolerance, bool check_mismatches = true)
622{
623 /* Keypoint (x,y) should have similar strength (within tolerance) and other properties in both reference and target */
624 const auto compare_props_eq = [&](const KeyPoint & lhs, const KeyPoint & rhs)
625 {
626 return compare<V>(lhs.strength, rhs.strength, tolerance)
627 && lhs.tracking_status == rhs.tracking_status
628 && lhs.scale == rhs.scale
629 && lhs.orientation == rhs.orientation
630 && lhs.error == rhs.error;
631 };
632
633 /* Used to sort KeyPoints by coordinates (x, y) */
634 const auto compare_coords_lt = [](const KeyPoint & lhs, const KeyPoint & rhs)
635 {
636 return std::tie(lhs.x, lhs.y) < std::tie(rhs.x, rhs.y);
637 };
638
639 std::sort(first1, last1, compare_coords_lt);
640 std::sort(first2, last2, compare_coords_lt);
641
642 if(check_mismatches)
643 {
644 ARM_COMPUTE_TEST_INFO("Checking for mismatches: ref count = " << std::distance(first1, last1) << " \ttarget count = " << std::distance(first2, last2));
645 }
646
647 int64_t num_missing = 0;
648 int64_t num_mismatches = 0;
649 bool rest_missing = false;
650
651 while(first1 != last1)
652 {
653 if(first2 == last2)
654 {
655 rest_missing = true;
656 break;
657 }
658
659 if(compare_coords_lt(*first1, *first2))
660 {
661 ++num_missing;
662 ARM_COMPUTE_TEST_INFO("Key point not found");
663 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++);
664 }
665 else
666 {
667 if(!compare_coords_lt(*first2, *first1)) // Equal coordinates
668 {
669 if(check_mismatches && !compare_props_eq(*first1, *first2)) // Check other properties
670 {
671 ++num_mismatches;
672 ARM_COMPUTE_TEST_INFO("Mismatching keypoint");
673 ARM_COMPUTE_TEST_INFO("keypoint1 [ref] = " << *first1);
674 ARM_COMPUTE_TEST_INFO("keypoint2 [tgt] = " << *first2);
675 }
676 ++first1;
677 }
678 ++first2;
679 }
680 }
681
682 if(rest_missing)
683 {
684 while(first1 != last1)
685 {
686 ++num_missing;
687 ARM_COMPUTE_TEST_INFO("Key point not found");
688 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++);
689 }
690 }
691
692 return std::make_pair(num_missing, num_mismatches);
693}
694
695template <typename T, typename U, typename V>
696void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance, float allowed_missing_percentage, float allowed_mismatch_percentage)
697{
698 const int64_t num_elements_target = std::distance(target_first, target_last);
699 const int64_t num_elements_reference = std::distance(reference_first, reference_last);
700
701 int64_t num_missing = 0;
702 int64_t num_mismatches = 0;
703
704 if(num_elements_reference > 0)
705 {
706 std::tie(num_missing, num_mismatches) = compare_keypoints(reference_first, reference_last, target_first, target_last, tolerance);
707
708 const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f;
709 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f;
710
711 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in ref are missing from target");
712 ARM_COMPUTE_TEST_INFO("Missing (not in tgt): " << num_missing << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_missing
713 << "% \tMax allowed: " << allowed_missing_percentage << "%");
714 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
715
716 ARM_COMPUTE_TEST_INFO(num_mismatches << " keypoints (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched");
717 ARM_COMPUTE_TEST_INFO("Mismatched keypoints: " << num_mismatches << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_mismatches
718 << "% \tMax allowed: " << allowed_mismatch_percentage << "%");
719 ARM_COMPUTE_EXPECT(percent_mismatches <= allowed_mismatch_percentage, framework::LogLevel::ERRORS);
720 }
721
722 if(num_elements_target > 0)
723 {
724 // Note: no need to check for mismatches a second time (last argument is 'false')
725 std::tie(num_missing, num_mismatches) = compare_keypoints(target_first, target_last, reference_first, reference_last, tolerance, false);
726
727 const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f;
728
729 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in target are missing from ref");
730 ARM_COMPUTE_TEST_INFO("Missing (not in ref): " << num_missing << "/" << num_elements_target << " = " << std::fixed << std::setprecision(2) << percent_missing
731 << "% \tMax allowed: " << allowed_missing_percentage << "%");
732 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
733 }
734}
735
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100736} // namespace validation
737} // namespace test
738} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100739#endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */