Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017, 2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 24 | #ifndef __ARM_COMPUTE_TEST_VALIDATION_H__ |
| 25 | #define __ARM_COMPUTE_TEST_VALIDATION_H__ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/FixedPoint.h" |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/IArray.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 30 | #include "support/ToolchainSupport.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 31 | #include "tests/IAccessor.h" |
| 32 | #include "tests/SimpleTensor.h" |
John Richardson | f89a49f | 2017-09-05 11:21:56 +0100 | [diff] [blame] | 33 | #include "tests/Types.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 34 | #include "tests/Utils.h" |
| 35 | #include "tests/framework/Asserts.h" |
| 36 | #include "tests/framework/Exceptions.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 37 | #include "utils/TypePrinter.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 39 | #include <iomanip> |
| 40 | #include <ios> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | #include <vector> |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | namespace test |
| 46 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | namespace validation |
| 48 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 49 | /** Class reprensenting an absolute tolerance value. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 51 | class AbsoluteTolerance |
| 52 | { |
| 53 | public: |
| 54 | /** Underlying type. */ |
| 55 | using value_type = T; |
| 56 | |
| 57 | /* Default constructor. |
| 58 | * |
| 59 | * Initialises the tolerance to 0. |
| 60 | */ |
| 61 | AbsoluteTolerance() = default; |
| 62 | |
| 63 | /** Constructor. |
| 64 | * |
| 65 | * @param[in] value Absolute tolerance value. |
| 66 | */ |
| 67 | explicit constexpr AbsoluteTolerance(T value) |
| 68 | : _value{ value } |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | /** Implicit conversion to the underlying type. */ |
| 73 | constexpr operator T() const |
| 74 | { |
| 75 | return _value; |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | T _value{ std::numeric_limits<T>::epsilon() }; |
| 80 | }; |
| 81 | |
| 82 | /** Class reprensenting a relative tolerance value. */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 83 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 84 | class RelativeTolerance |
| 85 | { |
| 86 | public: |
| 87 | /** Underlying type. */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 88 | using value_type = T; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 89 | |
| 90 | /* Default constructor. |
| 91 | * |
| 92 | * Initialises the tolerance to 0. |
| 93 | */ |
| 94 | RelativeTolerance() = default; |
| 95 | |
| 96 | /** Constructor. |
| 97 | * |
| 98 | * @param[in] value Relative tolerance value. |
| 99 | */ |
| 100 | explicit constexpr RelativeTolerance(value_type value) |
| 101 | : _value{ value } |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | /** Implicit conversion to the underlying type. */ |
| 106 | constexpr operator value_type() const |
| 107 | { |
| 108 | return _value; |
| 109 | } |
| 110 | |
| 111 | private: |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 112 | value_type _value{ std::numeric_limits<T>::epsilon() }; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /** Print AbsoluteTolerance type. */ |
| 116 | template <typename T> |
| 117 | inline ::std::ostream &operator<<(::std::ostream &os, const AbsoluteTolerance<T> &tolerance) |
| 118 | { |
| 119 | os << static_cast<typename AbsoluteTolerance<T>::value_type>(tolerance); |
| 120 | |
| 121 | return os; |
| 122 | } |
| 123 | |
| 124 | /** Print RelativeTolerance type. */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 125 | template <typename T> |
| 126 | inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 127 | { |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 128 | os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 129 | |
| 130 | return os; |
| 131 | } |
| 132 | |
| 133 | template <typename T> |
| 134 | bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | { |
| 136 | if(dimensions1.num_dimensions() != dimensions2.num_dimensions()) |
| 137 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 138 | return false; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i) |
| 142 | { |
| 143 | if(dimensions1[i] != dimensions2[i]) |
| 144 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 145 | return false; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | /** Validate valid regions. |
| 153 | * |
| 154 | * - Dimensionality has to be the same. |
| 155 | * - Anchors have to match. |
| 156 | * - Shapes have to match. |
| 157 | */ |
| 158 | void validate(const arm_compute::ValidRegion ®ion, const arm_compute::ValidRegion &reference); |
| 159 | |
| 160 | /** Validate padding. |
| 161 | * |
| 162 | * Padding on all sides has to be the same. |
| 163 | */ |
| 164 | void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference); |
| 165 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame^] | 166 | /** Validate padding. |
| 167 | * |
| 168 | * Padding on all sides has to be the same. |
| 169 | */ |
| 170 | void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &width_reference, const arm_compute::PaddingSize &height_reference); |
| 171 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | /** Validate tensors. |
| 173 | * |
| 174 | * - Dimensionality has to be the same. |
| 175 | * - All values have to match. |
| 176 | * |
| 177 | * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to |
| 178 | * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between |
| 179 | * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by |
| 180 | * other test cases. |
| 181 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 182 | template <typename T, typename U = AbsoluteTolerance<T>> |
| 183 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 184 | |
| 185 | /** Validate tensors with valid region. |
| 186 | * |
| 187 | * - Dimensionality has to be the same. |
| 188 | * - All values have to match. |
| 189 | * |
| 190 | * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to |
| 191 | * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between |
| 192 | * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by |
| 193 | * other test cases. |
| 194 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 195 | template <typename T, typename U = AbsoluteTolerance<T>> |
| 196 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value = U(), float tolerance_number = 0.f); |
Isabella Gottardi | 6203153 | 2017-07-04 11:21:28 +0100 | [diff] [blame] | 197 | |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 198 | /** Validate tensors with valid mask. |
| 199 | * |
| 200 | * - Dimensionality has to be the same. |
| 201 | * - All values have to match. |
| 202 | * |
| 203 | * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to |
| 204 | * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between |
| 205 | * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by |
| 206 | * other test cases. |
| 207 | */ |
| 208 | template <typename T, typename U = AbsoluteTolerance<T>> |
| 209 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value = U(), float tolerance_number = 0.f); |
| 210 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 211 | /** Validate tensors against constant value. |
| 212 | * |
| 213 | * - All values have to match. |
| 214 | */ |
| 215 | void validate(const IAccessor &tensor, const void *reference_value); |
| 216 | |
| 217 | /** Validate border against a constant value. |
| 218 | * |
| 219 | * - All border values have to match the specified value if mode is CONSTANT. |
| 220 | * - All border values have to be replicated if mode is REPLICATE. |
| 221 | * - Nothing is validated for mode UNDEFINED. |
| 222 | */ |
| 223 | void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value); |
| 224 | |
| 225 | /** Validate classified labels against expected ones. |
| 226 | * |
| 227 | * - All values should match |
| 228 | */ |
| 229 | void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels); |
steniu01 | 960b084 | 2017-06-23 11:44:34 +0100 | [diff] [blame] | 230 | |
| 231 | /** Validate float value. |
| 232 | * |
| 233 | * - All values should match |
| 234 | */ |
Moritz Pflanzer | 7655a67 | 2017-09-23 11:57:33 +0100 | [diff] [blame] | 235 | template <typename T, typename U = AbsoluteTolerance<T>> |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 236 | bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>()); |
| 237 | |
| 238 | /** Validate key points. */ |
| 239 | template <typename T, typename U, typename V = AbsoluteTolerance<float>> |
Georgios Pinitas | 5962f13 | 2017-12-11 16:59:29 +0000 | [diff] [blame] | 240 | void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance = AbsoluteTolerance<float>(), |
| 241 | float allowed_missing_percentage = 5.f, float allowed_mismatch_percentage = 5.f); |
steniu01 | 960b084 | 2017-06-23 11:44:34 +0100 | [diff] [blame] | 242 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 243 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 244 | struct compare_base |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 245 | { |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 246 | compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0)) |
| 247 | : _target{ target }, _reference{ reference }, _tolerance{ tolerance } |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 248 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 251 | typename T::value_type _target{}; |
| 252 | typename T::value_type _reference{}; |
| 253 | T _tolerance{}; |
| 254 | }; |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 255 | |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 256 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 257 | struct compare; |
| 258 | |
| 259 | template <typename U> |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 260 | struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 261 | { |
| 262 | using compare_base<AbsoluteTolerance<U>>::compare_base; |
| 263 | |
| 264 | operator bool() const |
| 265 | { |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 266 | if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference)) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 267 | { |
| 268 | return false; |
| 269 | } |
| 270 | else if(this->_target == this->_reference) |
| 271 | { |
| 272 | return true; |
| 273 | } |
| 274 | |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 275 | using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type; |
| 276 | |
| 277 | const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference))); |
| 278 | |
| 279 | return abs_difference <= static_cast<comparison_type>(this->_tolerance); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 280 | } |
| 281 | }; |
| 282 | |
| 283 | template <typename U> |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 284 | struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 285 | { |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 286 | using compare_base<RelativeTolerance<U>>::compare_base; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 287 | |
| 288 | operator bool() const |
| 289 | { |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 290 | if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference)) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 291 | { |
| 292 | return false; |
| 293 | } |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 294 | else if(this->_target == this->_reference) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 295 | { |
| 296 | return true; |
| 297 | } |
| 298 | |
Moritz Pflanzer | ff1c360 | 2017-09-22 12:41:25 +0100 | [diff] [blame] | 299 | 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 Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 300 | |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 301 | if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon) |
| 302 | { |
| 303 | return true; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | if(static_cast<double>(this->_reference) == 0.0f) // We have checked whether _reference and _target is closing. If _reference is 0 but not closed to _target, it should return false |
| 308 | { |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | const double relative_change = std::abs(static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference; |
| 313 | |
| 314 | return relative_change <= static_cast<U>(this->_tolerance); |
| 315 | } |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 316 | } |
| 317 | }; |
| 318 | |
| 319 | template <typename T, typename U> |
| 320 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number) |
| 321 | { |
| 322 | // Validate with valid region covering the entire shape |
| 323 | validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number); |
| 324 | } |
| 325 | |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 326 | template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 327 | void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number) |
| 328 | { |
| 329 | // Validate with valid region covering the entire shape |
| 330 | validate_wrap(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number); |
| 331 | } |
| 332 | |
| 333 | template <typename T, typename U> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 334 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number) |
| 335 | { |
| 336 | int64_t num_mismatches = 0; |
| 337 | int64_t num_elements = 0; |
| 338 | |
| 339 | ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS); |
| 340 | ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS); |
| 341 | |
| 342 | if(reference.format() != Format::UNKNOWN) |
| 343 | { |
| 344 | ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS); |
| 345 | } |
| 346 | |
| 347 | ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS); |
| 348 | ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS); |
| 349 | |
| 350 | const int min_elements = std::min(tensor.num_elements(), reference.num_elements()); |
| 351 | const int min_channels = std::min(tensor.num_channels(), reference.num_channels()); |
| 352 | |
| 353 | // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ... |
| 354 | for(int element_idx = 0; element_idx < min_elements; ++element_idx) |
| 355 | { |
| 356 | const Coordinates id = index2coord(reference.shape(), element_idx); |
| 357 | |
| 358 | if(is_in_valid_region(valid_region, id)) |
| 359 | { |
| 360 | // Iterate over all channels within one element |
| 361 | for(int c = 0; c < min_channels; ++c) |
| 362 | { |
| 363 | const T &target_value = reinterpret_cast<const T *>(tensor(id))[c]; |
| 364 | const T &reference_value = reinterpret_cast<const T *>(reference(id))[c]; |
| 365 | |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 366 | if(!compare<U>(target_value, reference_value, tolerance_value)) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 367 | { |
| 368 | ARM_COMPUTE_TEST_INFO("id = " << id); |
| 369 | ARM_COMPUTE_TEST_INFO("channel = " << c); |
| 370 | ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value)); |
| 371 | ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value)); |
| 372 | ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value))); |
steniu01 | 172c58d | 2017-08-31 13:49:08 +0100 | [diff] [blame] | 373 | framework::ARM_COMPUTE_PRINT_INFO(); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 374 | |
| 375 | ++num_mismatches; |
| 376 | } |
| 377 | |
| 378 | ++num_elements; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if(num_elements > 0) |
| 384 | { |
| 385 | const int64_t absolute_tolerance_number = tolerance_number * num_elements; |
| 386 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f; |
| 387 | |
| 388 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches |
| 389 | << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)"); |
| 390 | ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 391 | } |
| 392 | } |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 393 | |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 394 | template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 395 | void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number) |
| 396 | { |
| 397 | int64_t num_mismatches = 0; |
| 398 | int64_t num_elements = 0; |
| 399 | |
| 400 | ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS); |
| 401 | ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS); |
| 402 | |
| 403 | if(reference.format() != Format::UNKNOWN) |
| 404 | { |
| 405 | ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS); |
| 406 | } |
| 407 | |
| 408 | ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS); |
| 409 | ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS); |
| 410 | |
| 411 | const int min_elements = std::min(tensor.num_elements(), reference.num_elements()); |
| 412 | const int min_channels = std::min(tensor.num_channels(), reference.num_channels()); |
| 413 | |
| 414 | // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ... |
| 415 | for(int element_idx = 0; element_idx < min_elements; ++element_idx) |
| 416 | { |
| 417 | const Coordinates id = index2coord(reference.shape(), element_idx); |
| 418 | |
| 419 | if(is_in_valid_region(valid_region, id)) |
| 420 | { |
| 421 | // Iterate over all channels within one element |
| 422 | for(int c = 0; c < min_channels; ++c) |
| 423 | { |
| 424 | const T &target_value = reinterpret_cast<const T *>(tensor(id))[c]; |
| 425 | const T &reference_value = reinterpret_cast<const T *>(reference(id))[c]; |
| 426 | |
| 427 | bool equal = compare<U>(target_value, reference_value, tolerance_value); |
| 428 | |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 429 | // check for wrapping |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 430 | if(!equal) |
| 431 | { |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 432 | if(!support::cpp11::isfinite(target_value) || !support::cpp11::isfinite(reference_value)) |
| 433 | { |
| 434 | equal = false; |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | using limits_type = typename std::make_unsigned<T>::type; |
| 439 | |
| 440 | uint64_t max = std::numeric_limits<limits_type>::max(); |
| 441 | uint64_t abs_sum = std::abs(static_cast<int64_t>(target_value)) + std::abs(static_cast<int64_t>(reference_value)); |
| 442 | uint64_t wrap_difference = max - abs_sum; |
| 443 | |
| 444 | equal = wrap_difference < static_cast<uint64_t>(tolerance_value); |
| 445 | } |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | if(!equal) |
| 449 | { |
| 450 | ARM_COMPUTE_TEST_INFO("id = " << id); |
| 451 | ARM_COMPUTE_TEST_INFO("channel = " << c); |
| 452 | ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value)); |
| 453 | ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value)); |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 454 | ARM_COMPUTE_TEST_INFO("wrap_tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value))); |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 455 | framework::ARM_COMPUTE_PRINT_INFO(); |
| 456 | |
| 457 | ++num_mismatches; |
| 458 | } |
| 459 | |
| 460 | ++num_elements; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if(num_elements > 0) |
| 466 | { |
| 467 | const int64_t absolute_tolerance_number = tolerance_number * num_elements; |
| 468 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f; |
| 469 | |
| 470 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches |
| 471 | << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)"); |
| 472 | ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS); |
| 473 | } |
| 474 | } |
| 475 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 476 | /** Check which keypoints from [first1, last1) are missing in [first2, last2) */ |
| 477 | template <typename T, typename U, typename V> |
| 478 | std::pair<int64_t, int64_t> compare_keypoints(T first1, T last1, U first2, U last2, V tolerance) |
| 479 | { |
| 480 | int64_t num_missing = 0; |
| 481 | int64_t num_mismatches = 0; |
| 482 | |
| 483 | while(first1 != last1) |
| 484 | { |
| 485 | const auto point = std::find_if(first2, last2, [&](KeyPoint point) |
| 486 | { |
| 487 | return point.x == first1->x && point.y == first1->y; |
| 488 | }); |
| 489 | |
| 490 | if(point == last2) |
| 491 | { |
| 492 | ++num_missing; |
Georgios Pinitas | 5962f13 | 2017-12-11 16:59:29 +0000 | [diff] [blame] | 493 | ARM_COMPUTE_TEST_INFO("Key point not found" << *first1) |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 494 | ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1) |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 495 | } |
| 496 | else if(!validate(point->tracking_status, first1->tracking_status) || !validate(point->strength, first1->strength, tolerance) || !validate(point->scale, first1->scale) |
| 497 | || !validate(point->orientation, first1->orientation) || !validate(point->error, first1->error)) |
| 498 | { |
| 499 | ++num_mismatches; |
Georgios Pinitas | 5962f13 | 2017-12-11 16:59:29 +0000 | [diff] [blame] | 500 | ARM_COMPUTE_TEST_INFO("Mismatching keypoint") |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 501 | ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1) |
| 502 | ARM_COMPUTE_TEST_INFO("keypoint2 = " << *point) |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | ++first1; |
| 506 | } |
| 507 | |
| 508 | return std::make_pair(num_missing, num_mismatches); |
| 509 | } |
| 510 | |
| 511 | template <typename T, typename U, typename V> |
Georgios Pinitas | 5962f13 | 2017-12-11 16:59:29 +0000 | [diff] [blame] | 512 | void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance, |
| 513 | float allowed_missing_percentage, float allowed_mismatch_percentage) |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 514 | { |
| 515 | const int64_t num_elements_target = std::distance(target_first, target_last); |
| 516 | const int64_t num_elements_reference = std::distance(reference_first, reference_last); |
| 517 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 518 | int64_t num_missing = 0; |
| 519 | int64_t num_mismatches = 0; |
| 520 | |
| 521 | if(num_elements_reference > 0) |
| 522 | { |
| 523 | std::tie(num_missing, num_mismatches) = compare_keypoints(reference_first, reference_last, target_first, target_last, tolerance); |
| 524 | |
| 525 | const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f; |
| 526 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f; |
| 527 | |
| 528 | ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) are missing in target"); |
Georgios Pinitas | 5962f13 | 2017-12-11 16:59:29 +0000 | [diff] [blame] | 529 | ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS); |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 530 | |
| 531 | ARM_COMPUTE_TEST_INFO(num_mismatches << " keypoints (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched"); |
Georgios Pinitas | 5962f13 | 2017-12-11 16:59:29 +0000 | [diff] [blame] | 532 | ARM_COMPUTE_EXPECT(percent_mismatches <= allowed_mismatch_percentage, framework::LogLevel::ERRORS); |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | if(num_elements_target > 0) |
| 536 | { |
| 537 | std::tie(num_missing, num_mismatches) = compare_keypoints(target_first, target_last, reference_first, reference_last, tolerance); |
| 538 | |
| 539 | const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f; |
| 540 | |
| 541 | ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) are not part of target"); |
Georgios Pinitas | 5962f13 | 2017-12-11 16:59:29 +0000 | [diff] [blame] | 542 | ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS); |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 546 | template <typename T, typename U> |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 547 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value, float tolerance_number) |
| 548 | { |
| 549 | int64_t num_mismatches = 0; |
| 550 | int64_t num_elements = 0; |
| 551 | |
| 552 | ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS); |
| 553 | ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS); |
| 554 | |
| 555 | if(reference.format() != Format::UNKNOWN) |
| 556 | { |
| 557 | ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS); |
| 558 | } |
| 559 | |
| 560 | ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS); |
| 561 | ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS); |
| 562 | |
| 563 | const int min_elements = std::min(tensor.num_elements(), reference.num_elements()); |
| 564 | const int min_channels = std::min(tensor.num_channels(), reference.num_channels()); |
| 565 | |
| 566 | // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ... |
| 567 | for(int element_idx = 0; element_idx < min_elements; ++element_idx) |
| 568 | { |
| 569 | const Coordinates id = index2coord(reference.shape(), element_idx); |
| 570 | |
| 571 | if(valid_mask[element_idx] == 1) |
| 572 | { |
| 573 | // Iterate over all channels within one element |
| 574 | for(int c = 0; c < min_channels; ++c) |
| 575 | { |
| 576 | const T &target_value = reinterpret_cast<const T *>(tensor(id))[c]; |
| 577 | const T &reference_value = reinterpret_cast<const T *>(reference(id))[c]; |
| 578 | |
| 579 | if(!compare<U>(target_value, reference_value, tolerance_value)) |
| 580 | { |
| 581 | ARM_COMPUTE_TEST_INFO("id = " << id); |
| 582 | ARM_COMPUTE_TEST_INFO("channel = " << c); |
| 583 | ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value)); |
| 584 | ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value)); |
| 585 | ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value))); |
| 586 | framework::ARM_COMPUTE_PRINT_INFO(); |
| 587 | |
| 588 | ++num_mismatches; |
| 589 | } |
| 590 | |
| 591 | ++num_elements; |
| 592 | } |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | ++num_elements; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if(num_elements > 0) |
| 601 | { |
| 602 | const int64_t absolute_tolerance_number = tolerance_number * num_elements; |
| 603 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f; |
| 604 | |
| 605 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches |
| 606 | << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)"); |
| 607 | ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | template <typename T, typename U> |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 612 | bool validate(T target, T reference, U tolerance) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 613 | { |
| 614 | ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference)); |
| 615 | ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target)); |
| 616 | ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance))); |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 617 | |
| 618 | const bool equal = compare<U>(target, reference, tolerance); |
| 619 | |
| 620 | ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS); |
| 621 | |
| 622 | return equal; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 623 | } |
John Richardson | f89a49f | 2017-09-05 11:21:56 +0100 | [diff] [blame] | 624 | |
| 625 | template <typename T, typename U> |
| 626 | void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference) |
| 627 | { |
| 628 | ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS); |
| 629 | ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS); |
| 630 | |
| 631 | ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS); |
| 632 | ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS); |
| 633 | |
| 634 | for(uint32_t i = 0; i < target.min_loc.size(); ++i) |
| 635 | { |
| 636 | const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord) |
| 637 | { |
| 638 | return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y; |
| 639 | }); |
| 640 | |
| 641 | ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS); |
| 642 | } |
| 643 | |
| 644 | for(uint32_t i = 0; i < target.max_loc.size(); ++i) |
| 645 | { |
| 646 | const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord) |
| 647 | { |
| 648 | return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y; |
| 649 | }); |
| 650 | |
| 651 | ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS); |
| 652 | } |
| 653 | } |
| 654 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 655 | } // namespace validation |
| 656 | } // namespace test |
| 657 | } // namespace arm_compute |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 658 | #endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */ |