blob: ac3643ea4a0ab0939cfb1afc830ad1a301fd6da7 [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>
Giorgio Arena563494c2018-04-30 17:29:41 +0100140bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2, const DataLayout &data_layout = DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141{
Giorgio Arena563494c2018-04-30 17:29:41 +0100142 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143
Giorgio Arena563494c2018-04-30 17:29:41 +0100144 if(data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100146 if(dimensions1.num_dimensions() != dimensions2.num_dimensions())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100148 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 }
Giorgio Arena563494c2018-04-30 17:29:41 +0100150
151 for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i)
152 {
153 if(dimensions1[i] != dimensions2[i])
154 {
155 return false;
156 }
157 }
158 }
159 else
160 {
161 // In case a 2D shape becomes 3D after permutation, the permuted tensor will have one dimension more and the first value will be 1
162 if((dimensions1.num_dimensions() != dimensions2.num_dimensions()) && ((dimensions1.num_dimensions() != (dimensions2.num_dimensions() + 1)) || (dimensions1.x() != 1)))
163 {
164 return false;
165 }
166
167 if((dimensions1[0] != dimensions2[2]) || (dimensions1[1] != dimensions2[0]) || (dimensions1[2] != dimensions2[1]))
168 {
169 return false;
170 }
171
172 for(unsigned int i = 3; i < dimensions1.num_dimensions(); ++i)
173 {
174 if(dimensions1[i] != dimensions2[i])
175 {
176 return false;
177 }
178 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 }
180
181 return true;
182}
183
184/** Validate valid regions.
185 *
186 * - Dimensionality has to be the same.
187 * - Anchors have to match.
188 * - Shapes have to match.
189 */
190void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference);
191
192/** Validate padding.
193 *
194 * Padding on all sides has to be the same.
195 */
196void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference);
197
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000198/** Validate padding.
199 *
200 * Padding on all sides has to be the same.
201 */
202void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &width_reference, const arm_compute::PaddingSize &height_reference);
203
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204/** Validate tensors.
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 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100214template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000215void 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 +0100216
217/** Validate tensors with valid region.
218 *
219 * - Dimensionality has to be the same.
220 * - All values have to match.
221 *
222 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
223 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
224 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
225 * other test cases.
226 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100227template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000228void 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 +0100229
Isabella Gottardi83be7452017-08-29 13:47:03 +0100230/** Validate tensors with valid mask.
231 *
232 * - Dimensionality has to be the same.
233 * - All values have to match.
234 *
235 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
236 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
237 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
238 * other test cases.
239 */
240template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000241void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value = U(), float tolerance_number = 0.f,
242 float absolute_tolerance_value = 0.f);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100243
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244/** Validate tensors against constant value.
245 *
246 * - All values have to match.
247 */
248void validate(const IAccessor &tensor, const void *reference_value);
249
250/** Validate border against a constant value.
251 *
252 * - All border values have to match the specified value if mode is CONSTANT.
253 * - All border values have to be replicated if mode is REPLICATE.
254 * - Nothing is validated for mode UNDEFINED.
255 */
256void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value);
257
258/** Validate classified labels against expected ones.
259 *
260 * - All values should match
261 */
262void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
steniu01960b0842017-06-23 11:44:34 +0100263
264/** Validate float value.
265 *
266 * - All values should match
267 */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100268template <typename T, typename U = AbsoluteTolerance<T>>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100269bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
270
271/** Validate key points. */
272template <typename T, typename U, typename V = AbsoluteTolerance<float>>
Georgios Pinitas5962f132017-12-11 16:59:29 +0000273void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance = AbsoluteTolerance<float>(),
274 float allowed_missing_percentage = 5.f, float allowed_mismatch_percentage = 5.f);
steniu01960b0842017-06-23 11:44:34 +0100275
John Richardson684cb0f2018-01-09 11:17:00 +0000276/** Validate detection windows. */
277template <typename T, typename U, typename V = AbsoluteTolerance<float>>
278void validate_detection_windows(T target_first, T target_last, U reference_first, U reference_last, V tolerance = AbsoluteTolerance<float>(),
279 float allowed_missing_percentage = 5.f, float allowed_mismatch_percentage = 5.f);
280
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100281template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100282struct compare_base
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100283{
Alex Gildayc357c472018-03-21 13:54:09 +0000284 /** Construct a comparison object.
285 *
286 * @param[in] target Target value.
287 * @param[in] reference Reference value.
288 * @param[in] tolerance Allowed tolerance.
289 */
John Richardson9c450cc2017-11-22 12:00:41 +0000290 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0))
291 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100292 {
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100293 }
294
Alex Gildayc357c472018-03-21 13:54:09 +0000295 typename T::value_type _target{}; /**< Target value */
296 typename T::value_type _reference{}; /**< Reference value */
297 T _tolerance{}; /**< Tolerance value */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100298};
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100299
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100300template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100301struct compare;
302
Alex Gildayc357c472018-03-21 13:54:09 +0000303/** Compare values with an absolute tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100304template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100305struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100306{
307 using compare_base<AbsoluteTolerance<U>>::compare_base;
308
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 }
316 else if(this->_target == this->_reference)
317 {
318 return true;
319 }
320
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100321 using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type;
322
323 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference)));
324
325 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100326 }
327};
328
Alex Gildayc357c472018-03-21 13:54:09 +0000329/** Compare values with a relative tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100330template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100331struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100332{
steniu013e05e4e2017-08-25 17:18:01 +0100333 using compare_base<RelativeTolerance<U>>::compare_base;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100334
Alex Gildayc357c472018-03-21 13:54:09 +0000335 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100336 operator bool() const
337 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100338 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100339 {
340 return false;
341 }
steniu013e05e4e2017-08-25 17:18:01 +0100342 else if(this->_target == this->_reference)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100343 {
344 return true;
345 }
346
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100347 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 +0100348
steniu013e05e4e2017-08-25 17:18:01 +0100349 if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon)
350 {
351 return true;
352 }
353 else
354 {
355 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
356 {
357 return false;
358 }
359
Isabella Gottardi8df6c452018-03-12 13:26:28 +0000360 const double relative_change = std::abs((static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference);
steniu013e05e4e2017-08-25 17:18:01 +0100361
362 return relative_change <= static_cast<U>(this->_tolerance);
363 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100364 }
365};
366
367template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000368void 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 +0100369{
370 // Validate with valid region covering the entire shape
Giorgio Arena563494c2018-04-30 17:29:41 +0100371 validate(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number, absolute_tolerance_value);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100372}
373
John Richardson9c450cc2017-11-22 12:00:41 +0000374template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100375void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
376{
377 // Validate with valid region covering the entire shape
Giorgio Arena563494c2018-04-30 17:29:41 +0100378 validate_wrap(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number);
John Richardsondd715f22017-09-18 16:10:48 +0100379}
380
381template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000382void 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 +0100383{
384 int64_t num_mismatches = 0;
385 int64_t num_elements = 0;
386
387 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
388 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
389
390 if(reference.format() != Format::UNKNOWN)
391 {
392 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
393 }
394
395 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
Giorgio Arena563494c2018-04-30 17:29:41 +0100396 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100397
398 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
399 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
400
401 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
402 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
403 {
404 const Coordinates id = index2coord(reference.shape(), element_idx);
405
Giorgio Arena563494c2018-04-30 17:29:41 +0100406 Coordinates target_id(id);
407 if(tensor.data_layout() == DataLayout::NHWC)
408 {
409 permute(target_id, PermutationVector(2U, 0U, 1U));
410 }
411
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100412 if(is_in_valid_region(valid_region, id))
413 {
414 // Iterate over all channels within one element
415 for(int c = 0; c < min_channels; ++c)
416 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100417 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100418 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
419
Michalis Spyrou23fe7c22018-02-26 10:42:01 +0000420 if(!compare<U>(target_value, reference_value, tolerance_value))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100421 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000422 if(absolute_tolerance_value != 0.f)
423 {
424 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
425 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
426 {
427 continue;
428 }
429 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100430 ARM_COMPUTE_TEST_INFO("id = " << id);
431 ARM_COMPUTE_TEST_INFO("channel = " << c);
432 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
433 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
434 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 +0100435 framework::ARM_COMPUTE_PRINT_INFO();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100436
437 ++num_mismatches;
438 }
439
440 ++num_elements;
441 }
442 }
443 }
444
445 if(num_elements > 0)
446 {
447 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
448 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
449
450 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
451 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
452 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100453 }
454}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100455
John Richardson9c450cc2017-11-22 12:00:41 +0000456template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100457void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
458{
459 int64_t num_mismatches = 0;
460 int64_t num_elements = 0;
461
462 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
463 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
464
465 if(reference.format() != Format::UNKNOWN)
466 {
467 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
468 }
469
470 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
Giorgio Arena563494c2018-04-30 17:29:41 +0100471 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
John Richardsondd715f22017-09-18 16:10:48 +0100472
473 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
474 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
475
476 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
477 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
478 {
479 const Coordinates id = index2coord(reference.shape(), element_idx);
480
Giorgio Arena563494c2018-04-30 17:29:41 +0100481 Coordinates target_id(id);
482 if(tensor.data_layout() == DataLayout::NHWC)
483 {
484 permute(target_id, PermutationVector(2U, 0U, 1U));
485 }
486
John Richardsondd715f22017-09-18 16:10:48 +0100487 if(is_in_valid_region(valid_region, id))
488 {
489 // Iterate over all channels within one element
490 for(int c = 0; c < min_channels; ++c)
491 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100492 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
John Richardsondd715f22017-09-18 16:10:48 +0100493 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
494
495 bool equal = compare<U>(target_value, reference_value, tolerance_value);
496
John Richardson9c450cc2017-11-22 12:00:41 +0000497 // check for wrapping
John Richardsondd715f22017-09-18 16:10:48 +0100498 if(!equal)
499 {
John Richardson9c450cc2017-11-22 12:00:41 +0000500 if(!support::cpp11::isfinite(target_value) || !support::cpp11::isfinite(reference_value))
501 {
502 equal = false;
503 }
504 else
505 {
506 using limits_type = typename std::make_unsigned<T>::type;
507
508 uint64_t max = std::numeric_limits<limits_type>::max();
509 uint64_t abs_sum = std::abs(static_cast<int64_t>(target_value)) + std::abs(static_cast<int64_t>(reference_value));
510 uint64_t wrap_difference = max - abs_sum;
511
512 equal = wrap_difference < static_cast<uint64_t>(tolerance_value);
513 }
John Richardsondd715f22017-09-18 16:10:48 +0100514 }
515
516 if(!equal)
517 {
518 ARM_COMPUTE_TEST_INFO("id = " << id);
519 ARM_COMPUTE_TEST_INFO("channel = " << c);
520 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
521 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
John Richardson9c450cc2017-11-22 12:00:41 +0000522 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 +0100523 framework::ARM_COMPUTE_PRINT_INFO();
524
525 ++num_mismatches;
526 }
527
528 ++num_elements;
529 }
530 }
531 }
532
533 if(num_elements > 0)
534 {
535 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
536 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
537
538 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
539 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
540 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
541 }
542}
543
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100544template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000545void 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 +0100546{
547 int64_t num_mismatches = 0;
548 int64_t num_elements = 0;
549
550 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
551 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
552
553 if(reference.format() != Format::UNKNOWN)
554 {
555 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
556 }
557
558 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
Giorgio Arena563494c2018-04-30 17:29:41 +0100559 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100560
561 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
562 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
563
564 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
565 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
566 {
567 const Coordinates id = index2coord(reference.shape(), element_idx);
568
Giorgio Arena563494c2018-04-30 17:29:41 +0100569 Coordinates target_id(id);
570 if(tensor.data_layout() == DataLayout::NHWC)
571 {
572 permute(target_id, PermutationVector(2U, 0U, 1U));
573 }
574
Isabella Gottardi83be7452017-08-29 13:47:03 +0100575 if(valid_mask[element_idx] == 1)
576 {
577 // Iterate over all channels within one element
578 for(int c = 0; c < min_channels; ++c)
579 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100580 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
Isabella Gottardi83be7452017-08-29 13:47:03 +0100581 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
582
583 if(!compare<U>(target_value, reference_value, tolerance_value))
584 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000585 if(absolute_tolerance_value != 0.f)
586 {
587 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
588 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
589 {
590 continue;
591 }
592 }
Isabella Gottardi83be7452017-08-29 13:47:03 +0100593 ARM_COMPUTE_TEST_INFO("id = " << id);
594 ARM_COMPUTE_TEST_INFO("channel = " << c);
595 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
596 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
597 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
598 framework::ARM_COMPUTE_PRINT_INFO();
599
600 ++num_mismatches;
601 }
602
603 ++num_elements;
604 }
605 }
606 else
607 {
608 ++num_elements;
609 }
610 }
611
612 if(num_elements > 0)
613 {
614 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
615 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
616
617 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
618 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
619 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
620 }
621}
622
623template <typename T, typename U>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100624bool validate(T target, T reference, U tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100625{
626 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
627 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
628 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 +0100629
630 const bool equal = compare<U>(target, reference, tolerance);
631
632 ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS);
633
634 return equal;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100635}
John Richardsonf89a49f2017-09-05 11:21:56 +0100636
637template <typename T, typename U>
638void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference)
639{
640 ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS);
641 ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS);
642
643 ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS);
644 ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS);
645
646 for(uint32_t i = 0; i < target.min_loc.size(); ++i)
647 {
648 const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord)
649 {
650 return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y;
651 });
652
653 ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS);
654 }
655
656 for(uint32_t i = 0; i < target.max_loc.size(); ++i)
657 {
658 const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord)
659 {
660 return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y;
661 });
662
663 ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS);
664 }
665}
666
Abe Mbise562fe0f2018-02-09 14:13:02 +0000667/** Check which keypoints from [first1, last1) are missing in [first2, last2) */
668template <typename T, typename U, typename V>
669std::pair<int64_t, int64_t> compare_keypoints(T first1, T last1, U first2, U last2, V tolerance, bool check_mismatches = true)
670{
671 /* Keypoint (x,y) should have similar strength (within tolerance) and other properties in both reference and target */
672 const auto compare_props_eq = [&](const KeyPoint & lhs, const KeyPoint & rhs)
673 {
674 return compare<V>(lhs.strength, rhs.strength, tolerance)
675 && lhs.tracking_status == rhs.tracking_status
676 && lhs.scale == rhs.scale
677 && lhs.orientation == rhs.orientation
678 && lhs.error == rhs.error;
679 };
680
681 /* Used to sort KeyPoints by coordinates (x, y) */
682 const auto compare_coords_lt = [](const KeyPoint & lhs, const KeyPoint & rhs)
683 {
684 return std::tie(lhs.x, lhs.y) < std::tie(rhs.x, rhs.y);
685 };
686
687 std::sort(first1, last1, compare_coords_lt);
688 std::sort(first2, last2, compare_coords_lt);
689
690 if(check_mismatches)
691 {
692 ARM_COMPUTE_TEST_INFO("Checking for mismatches: ref count = " << std::distance(first1, last1) << " \ttarget count = " << std::distance(first2, last2));
693 }
694
695 int64_t num_missing = 0;
696 int64_t num_mismatches = 0;
697 bool rest_missing = false;
698
699 while(first1 != last1)
700 {
701 if(first2 == last2)
702 {
703 rest_missing = true;
704 break;
705 }
706
707 if(compare_coords_lt(*first1, *first2))
708 {
709 ++num_missing;
710 ARM_COMPUTE_TEST_INFO("Key point not found");
711 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++);
712 }
713 else
714 {
715 if(!compare_coords_lt(*first2, *first1)) // Equal coordinates
716 {
717 if(check_mismatches && !compare_props_eq(*first1, *first2)) // Check other properties
718 {
719 ++num_mismatches;
720 ARM_COMPUTE_TEST_INFO("Mismatching keypoint");
721 ARM_COMPUTE_TEST_INFO("keypoint1 [ref] = " << *first1);
722 ARM_COMPUTE_TEST_INFO("keypoint2 [tgt] = " << *first2);
723 }
724 ++first1;
725 }
726 ++first2;
727 }
728 }
729
730 if(rest_missing)
731 {
732 while(first1 != last1)
733 {
734 ++num_missing;
735 ARM_COMPUTE_TEST_INFO("Key point not found");
736 ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++);
737 }
738 }
739
740 return std::make_pair(num_missing, num_mismatches);
741}
742
743template <typename T, typename U, typename V>
744void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance, float allowed_missing_percentage, float allowed_mismatch_percentage)
745{
746 const int64_t num_elements_target = std::distance(target_first, target_last);
747 const int64_t num_elements_reference = std::distance(reference_first, reference_last);
748
749 int64_t num_missing = 0;
750 int64_t num_mismatches = 0;
751
752 if(num_elements_reference > 0)
753 {
754 std::tie(num_missing, num_mismatches) = compare_keypoints(reference_first, reference_last, target_first, target_last, tolerance);
755
756 const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f;
757 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f;
758
759 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in ref are missing from target");
760 ARM_COMPUTE_TEST_INFO("Missing (not in tgt): " << num_missing << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_missing
761 << "% \tMax allowed: " << allowed_missing_percentage << "%");
762 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
763
764 ARM_COMPUTE_TEST_INFO(num_mismatches << " keypoints (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched");
765 ARM_COMPUTE_TEST_INFO("Mismatched keypoints: " << num_mismatches << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_mismatches
766 << "% \tMax allowed: " << allowed_mismatch_percentage << "%");
767 ARM_COMPUTE_EXPECT(percent_mismatches <= allowed_mismatch_percentage, framework::LogLevel::ERRORS);
768 }
769
770 if(num_elements_target > 0)
771 {
772 // Note: no need to check for mismatches a second time (last argument is 'false')
773 std::tie(num_missing, num_mismatches) = compare_keypoints(target_first, target_last, reference_first, reference_last, tolerance, false);
774
775 const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f;
776
777 ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in target are missing from ref");
778 ARM_COMPUTE_TEST_INFO("Missing (not in ref): " << num_missing << "/" << num_elements_target << " = " << std::fixed << std::setprecision(2) << percent_missing
779 << "% \tMax allowed: " << allowed_missing_percentage << "%");
780 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
781 }
782}
783
John Richardson684cb0f2018-01-09 11:17:00 +0000784/** Check which detection windows from [first1, last1) are missing in [first2, last2) */
785template <typename T, typename U, typename V>
786std::pair<int64_t, int64_t> compare_detection_windows(T first1, T last1, U first2, U last2, V tolerance)
787{
788 int64_t num_missing = 0;
789 int64_t num_mismatches = 0;
790
791 while(first1 != last1)
792 {
793 const auto window = std::find_if(first2, last2, [&](DetectionWindow window)
794 {
795 return window.x == first1->x && window.y == first1->y && window.width == first1->width && window.height == first1->height && window.idx_class == first1->idx_class;
796 });
797
798 if(window == last2)
799 {
800 ++num_missing;
801 ARM_COMPUTE_TEST_INFO("Detection window not found " << *first1)
802 }
803 else
804 {
805 if(!compare<V>(window->score, first1->score, tolerance))
806 {
807 ++num_mismatches;
808 ARM_COMPUTE_TEST_INFO("Mismatching detection window")
809 ARM_COMPUTE_TEST_INFO("detection window 1= " << *first1)
810 ARM_COMPUTE_TEST_INFO("detection window 2= " << *window)
811 }
812 }
813
814 ++first1;
815 }
816
817 return std::make_pair(num_missing, num_mismatches);
818}
819
820template <typename T, typename U, typename V>
821void validate_detection_windows(T target_first, T target_last, U reference_first, U reference_last, V tolerance,
822 float allowed_missing_percentage, float allowed_mismatch_percentage)
823{
824 const int64_t num_elements_target = std::distance(target_first, target_last);
825 const int64_t num_elements_reference = std::distance(reference_first, reference_last);
826
827 int64_t num_missing = 0;
828 int64_t num_mismatches = 0;
829
830 if(num_elements_reference > 0)
831 {
832 std::tie(num_missing, num_mismatches) = compare_detection_windows(reference_first, reference_last, target_first, target_last, tolerance);
833
834 const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f;
835 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f;
836
837 ARM_COMPUTE_TEST_INFO(num_missing << " detection windows (" << std::fixed << std::setprecision(2) << percent_missing << "%) are missing in target");
838 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
839
840 ARM_COMPUTE_TEST_INFO(num_mismatches << " detection windows (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched");
841 ARM_COMPUTE_EXPECT(percent_mismatches <= allowed_mismatch_percentage, framework::LogLevel::ERRORS);
842 }
843
844 if(num_elements_target > 0)
845 {
846 std::tie(num_missing, num_mismatches) = compare_detection_windows(target_first, target_last, reference_first, reference_last, tolerance);
847
848 const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f;
849
850 ARM_COMPUTE_TEST_INFO(num_missing << " detection windows (" << std::fixed << std::setprecision(2) << percent_missing << "%) are not part of target");
851 ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS);
852 }
853}
854
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100855} // namespace validation
856} // namespace test
857} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100858#endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */