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