Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [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 | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/IArray.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Types.h" |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 29 | #include "support/ToolchainSupport.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 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 71 | /** Implicit conversion to the underlying type. |
| 72 | * |
| 73 | * @return the underlying type. |
| 74 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 75 | constexpr operator T() const |
| 76 | { |
| 77 | return _value; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | T _value{ std::numeric_limits<T>::epsilon() }; |
| 82 | }; |
| 83 | |
| 84 | /** Class reprensenting a relative tolerance value. */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 85 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 86 | class RelativeTolerance |
| 87 | { |
| 88 | public: |
| 89 | /** Underlying type. */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 90 | using value_type = T; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 91 | |
| 92 | /* Default constructor. |
| 93 | * |
| 94 | * Initialises the tolerance to 0. |
| 95 | */ |
| 96 | RelativeTolerance() = default; |
| 97 | |
| 98 | /** Constructor. |
| 99 | * |
| 100 | * @param[in] value Relative tolerance value. |
| 101 | */ |
| 102 | explicit constexpr RelativeTolerance(value_type value) |
| 103 | : _value{ value } |
| 104 | { |
| 105 | } |
| 106 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 107 | /** Implicit conversion to the underlying type. |
| 108 | * |
| 109 | * @return the underlying type. |
| 110 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 111 | constexpr operator value_type() const |
| 112 | { |
| 113 | return _value; |
| 114 | } |
| 115 | |
| 116 | private: |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 117 | value_type _value{ std::numeric_limits<T>::epsilon() }; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /** Print AbsoluteTolerance type. */ |
| 121 | template <typename T> |
| 122 | inline ::std::ostream &operator<<(::std::ostream &os, const AbsoluteTolerance<T> &tolerance) |
| 123 | { |
| 124 | os << static_cast<typename AbsoluteTolerance<T>::value_type>(tolerance); |
| 125 | |
| 126 | return os; |
| 127 | } |
| 128 | |
| 129 | /** Print RelativeTolerance type. */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 130 | template <typename T> |
| 131 | inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 132 | { |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 133 | os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 134 | |
| 135 | return os; |
| 136 | } |
| 137 | |
| 138 | template <typename T> |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 139 | bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2, const DataLayout &data_layout = DataLayout::NCHW) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 141 | ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 143 | if(data_layout == DataLayout::NCHW) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 145 | if(dimensions1.num_dimensions() != dimensions2.num_dimensions()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 147 | return false; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | } |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 149 | |
| 150 | for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i) |
| 151 | { |
| 152 | if(dimensions1[i] != dimensions2[i]) |
| 153 | { |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | // In case a 2D shape becomes 3D after permutation, the permuted tensor will have one dimension more and the first value will be 1 |
| 161 | if((dimensions1.num_dimensions() != dimensions2.num_dimensions()) && ((dimensions1.num_dimensions() != (dimensions2.num_dimensions() + 1)) || (dimensions1.x() != 1))) |
| 162 | { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | if((dimensions1[0] != dimensions2[2]) || (dimensions1[1] != dimensions2[0]) || (dimensions1[2] != dimensions2[1])) |
| 167 | { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | for(unsigned int i = 3; i < dimensions1.num_dimensions(); ++i) |
| 172 | { |
| 173 | if(dimensions1[i] != dimensions2[i]) |
| 174 | { |
| 175 | return false; |
| 176 | } |
| 177 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | /** Validate valid regions. |
| 184 | * |
| 185 | * - Dimensionality has to be the same. |
| 186 | * - Anchors have to match. |
| 187 | * - Shapes have to match. |
| 188 | */ |
| 189 | void validate(const arm_compute::ValidRegion ®ion, const arm_compute::ValidRegion &reference); |
| 190 | |
| 191 | /** Validate padding. |
| 192 | * |
| 193 | * Padding on all sides has to be the same. |
| 194 | */ |
| 195 | void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference); |
| 196 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 197 | /** Validate padding. |
| 198 | * |
| 199 | * Padding on all sides has to be the same. |
| 200 | */ |
| 201 | void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &width_reference, const arm_compute::PaddingSize &height_reference); |
| 202 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | /** Validate tensors. |
| 204 | * |
| 205 | * - Dimensionality has to be the same. |
| 206 | * - All values have to match. |
| 207 | * |
| 208 | * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to |
| 209 | * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between |
| 210 | * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by |
| 211 | * other test cases. |
| 212 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 213 | template <typename T, typename U = AbsoluteTolerance<T>> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 214 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f, float absolute_tolerance_value = 0.f); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 215 | |
| 216 | /** Validate tensors with valid region. |
| 217 | * |
| 218 | * - Dimensionality has to be the same. |
| 219 | * - All values have to match. |
| 220 | * |
| 221 | * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to |
| 222 | * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between |
| 223 | * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by |
| 224 | * other test cases. |
| 225 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 226 | template <typename T, typename U = AbsoluteTolerance<T>> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 227 | void 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 Gottardi | 6203153 | 2017-07-04 11:21:28 +0100 | [diff] [blame] | 228 | |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 229 | /** Validate tensors with valid mask. |
| 230 | * |
| 231 | * - Dimensionality has to be the same. |
| 232 | * - All values have to match. |
| 233 | * |
| 234 | * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to |
| 235 | * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between |
| 236 | * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by |
| 237 | * other test cases. |
| 238 | */ |
| 239 | template <typename T, typename U = AbsoluteTolerance<T>> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 240 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value = U(), float tolerance_number = 0.f, |
| 241 | float absolute_tolerance_value = 0.f); |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 242 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 243 | /** Validate tensors against constant value. |
| 244 | * |
| 245 | * - All values have to match. |
| 246 | */ |
| 247 | void validate(const IAccessor &tensor, const void *reference_value); |
| 248 | |
| 249 | /** Validate border against a constant value. |
| 250 | * |
| 251 | * - All border values have to match the specified value if mode is CONSTANT. |
| 252 | * - All border values have to be replicated if mode is REPLICATE. |
| 253 | * - Nothing is validated for mode UNDEFINED. |
| 254 | */ |
| 255 | void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value); |
| 256 | |
| 257 | /** Validate classified labels against expected ones. |
| 258 | * |
| 259 | * - All values should match |
| 260 | */ |
| 261 | 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] | 262 | |
| 263 | /** Validate float value. |
| 264 | * |
| 265 | * - All values should match |
| 266 | */ |
Moritz Pflanzer | 7655a67 | 2017-09-23 11:57:33 +0100 | [diff] [blame] | 267 | template <typename T, typename U = AbsoluteTolerance<T>> |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 268 | bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>()); |
| 269 | |
| 270 | /** Validate key points. */ |
| 271 | template <typename T, typename U, typename V = AbsoluteTolerance<float>> |
Georgios Pinitas | 5962f13 | 2017-12-11 16:59:29 +0000 | [diff] [blame] | 272 | void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance = AbsoluteTolerance<float>(), |
| 273 | float allowed_missing_percentage = 5.f, float allowed_mismatch_percentage = 5.f); |
steniu01 | 960b084 | 2017-06-23 11:44:34 +0100 | [diff] [blame] | 274 | |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 275 | /** Validate detection windows. */ |
| 276 | template <typename T, typename U, typename V = AbsoluteTolerance<float>> |
| 277 | void validate_detection_windows(T target_first, T target_last, U reference_first, U reference_last, V tolerance = AbsoluteTolerance<float>(), |
| 278 | float allowed_missing_percentage = 5.f, float allowed_mismatch_percentage = 5.f); |
| 279 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 280 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 281 | struct compare_base |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 282 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 283 | /** Construct a comparison object. |
| 284 | * |
| 285 | * @param[in] target Target value. |
| 286 | * @param[in] reference Reference value. |
| 287 | * @param[in] tolerance Allowed tolerance. |
| 288 | */ |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 289 | compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0)) |
| 290 | : _target{ target }, _reference{ reference }, _tolerance{ tolerance } |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 291 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 294 | typename T::value_type _target{}; /**< Target value */ |
| 295 | typename T::value_type _reference{}; /**< Reference value */ |
| 296 | T _tolerance{}; /**< Tolerance value */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 297 | }; |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 298 | |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 299 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 300 | struct compare; |
| 301 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 302 | /** Compare values with an absolute tolerance */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 303 | template <typename U> |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 304 | struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 305 | { |
| 306 | using compare_base<AbsoluteTolerance<U>>::compare_base; |
| 307 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 308 | /** Perform comparison */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 309 | operator bool() const |
| 310 | { |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 311 | if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference)) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 312 | { |
| 313 | return false; |
| 314 | } |
| 315 | else if(this->_target == this->_reference) |
| 316 | { |
| 317 | return true; |
| 318 | } |
| 319 | |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 320 | using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type; |
| 321 | |
| 322 | const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference))); |
| 323 | |
| 324 | return abs_difference <= static_cast<comparison_type>(this->_tolerance); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 325 | } |
| 326 | }; |
| 327 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 328 | /** Compare values with a relative tolerance */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 329 | template <typename U> |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 330 | struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 331 | { |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 332 | using compare_base<RelativeTolerance<U>>::compare_base; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 333 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 334 | /** Perform comparison */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 335 | operator bool() const |
| 336 | { |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 337 | if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference)) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 338 | { |
| 339 | return false; |
| 340 | } |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 341 | else if(this->_target == this->_reference) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 342 | { |
| 343 | return true; |
| 344 | } |
| 345 | |
Moritz Pflanzer | ff1c360 | 2017-09-22 12:41:25 +0100 | [diff] [blame] | 346 | 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] | 347 | |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 348 | if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon) |
| 349 | { |
| 350 | return true; |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | 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 |
| 355 | { |
| 356 | return false; |
| 357 | } |
| 358 | |
Isabella Gottardi | 8df6c45 | 2018-03-12 13:26:28 +0000 | [diff] [blame] | 359 | const double relative_change = std::abs((static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference); |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 360 | |
| 361 | return relative_change <= static_cast<U>(this->_tolerance); |
| 362 | } |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 363 | } |
| 364 | }; |
| 365 | |
| 366 | template <typename T, typename U> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 367 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number, float absolute_tolerance_value) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 368 | { |
| 369 | // Validate with valid region covering the entire shape |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 370 | validate(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number, absolute_tolerance_value); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 371 | } |
| 372 | |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 373 | 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] | 374 | void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number) |
| 375 | { |
| 376 | // Validate with valid region covering the entire shape |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 377 | validate_wrap(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number); |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | template <typename T, typename U> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 381 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number, float absolute_tolerance_value) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 382 | { |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 383 | uint64_t num_mismatches = 0; |
| 384 | uint64_t num_elements = 0; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 385 | |
| 386 | ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS); |
| 387 | ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS); |
| 388 | |
| 389 | if(reference.format() != Format::UNKNOWN) |
| 390 | { |
| 391 | ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS); |
| 392 | } |
| 393 | |
| 394 | ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS); |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 395 | ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 396 | |
| 397 | const int min_elements = std::min(tensor.num_elements(), reference.num_elements()); |
| 398 | const int min_channels = std::min(tensor.num_channels(), reference.num_channels()); |
| 399 | |
| 400 | // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ... |
| 401 | for(int element_idx = 0; element_idx < min_elements; ++element_idx) |
| 402 | { |
| 403 | const Coordinates id = index2coord(reference.shape(), element_idx); |
| 404 | |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 405 | Coordinates target_id(id); |
| 406 | if(tensor.data_layout() == DataLayout::NHWC) |
| 407 | { |
| 408 | permute(target_id, PermutationVector(2U, 0U, 1U)); |
| 409 | } |
| 410 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 411 | if(is_in_valid_region(valid_region, id)) |
| 412 | { |
| 413 | // Iterate over all channels within one element |
| 414 | for(int c = 0; c < min_channels; ++c) |
| 415 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 416 | const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c]; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 417 | const T &reference_value = reinterpret_cast<const T *>(reference(id))[c]; |
| 418 | |
Michalis Spyrou | 23fe7c2 | 2018-02-26 10:42:01 +0000 | [diff] [blame] | 419 | if(!compare<U>(target_value, reference_value, tolerance_value)) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 420 | { |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 421 | if(absolute_tolerance_value != 0.f) |
| 422 | { |
| 423 | const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value); |
| 424 | if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance)) |
| 425 | { |
| 426 | continue; |
| 427 | } |
| 428 | } |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 429 | ARM_COMPUTE_TEST_INFO("id = " << id); |
| 430 | ARM_COMPUTE_TEST_INFO("channel = " << c); |
| 431 | ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value)); |
| 432 | ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value)); |
| 433 | 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] | 434 | framework::ARM_COMPUTE_PRINT_INFO(); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 435 | |
| 436 | ++num_mismatches; |
| 437 | } |
| 438 | |
| 439 | ++num_elements; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 444 | if(num_elements != 0) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 445 | { |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 446 | const uint64_t absolute_tolerance_number = tolerance_number * num_elements; |
| 447 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 448 | |
| 449 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 450 | << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)"); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 451 | ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 452 | } |
| 453 | } |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 454 | |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 455 | 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] | 456 | void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number) |
| 457 | { |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 458 | uint64_t num_mismatches = 0; |
| 459 | uint64_t num_elements = 0; |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 460 | |
| 461 | ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS); |
| 462 | ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS); |
| 463 | |
| 464 | if(reference.format() != Format::UNKNOWN) |
| 465 | { |
| 466 | ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS); |
| 467 | } |
| 468 | |
| 469 | ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS); |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 470 | ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS); |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 471 | |
| 472 | const int min_elements = std::min(tensor.num_elements(), reference.num_elements()); |
| 473 | const int min_channels = std::min(tensor.num_channels(), reference.num_channels()); |
| 474 | |
| 475 | // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ... |
| 476 | for(int element_idx = 0; element_idx < min_elements; ++element_idx) |
| 477 | { |
| 478 | const Coordinates id = index2coord(reference.shape(), element_idx); |
| 479 | |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 480 | Coordinates target_id(id); |
| 481 | if(tensor.data_layout() == DataLayout::NHWC) |
| 482 | { |
| 483 | permute(target_id, PermutationVector(2U, 0U, 1U)); |
| 484 | } |
| 485 | |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 486 | if(is_in_valid_region(valid_region, id)) |
| 487 | { |
| 488 | // Iterate over all channels within one element |
| 489 | for(int c = 0; c < min_channels; ++c) |
| 490 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 491 | const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c]; |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 492 | const T &reference_value = reinterpret_cast<const T *>(reference(id))[c]; |
| 493 | |
| 494 | bool equal = compare<U>(target_value, reference_value, tolerance_value); |
| 495 | |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 496 | // check for wrapping |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 497 | if(!equal) |
| 498 | { |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 499 | if(!support::cpp11::isfinite(target_value) || !support::cpp11::isfinite(reference_value)) |
| 500 | { |
| 501 | equal = false; |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | using limits_type = typename std::make_unsigned<T>::type; |
| 506 | |
| 507 | uint64_t max = std::numeric_limits<limits_type>::max(); |
| 508 | uint64_t abs_sum = std::abs(static_cast<int64_t>(target_value)) + std::abs(static_cast<int64_t>(reference_value)); |
| 509 | uint64_t wrap_difference = max - abs_sum; |
| 510 | |
| 511 | equal = wrap_difference < static_cast<uint64_t>(tolerance_value); |
| 512 | } |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | if(!equal) |
| 516 | { |
| 517 | ARM_COMPUTE_TEST_INFO("id = " << id); |
| 518 | ARM_COMPUTE_TEST_INFO("channel = " << c); |
| 519 | ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value)); |
| 520 | 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] | 521 | 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] | 522 | framework::ARM_COMPUTE_PRINT_INFO(); |
| 523 | |
| 524 | ++num_mismatches; |
| 525 | } |
| 526 | |
| 527 | ++num_elements; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 532 | if(num_elements != 0) |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 533 | { |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 534 | const uint64_t absolute_tolerance_number = tolerance_number * num_elements; |
| 535 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f; |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 536 | |
| 537 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 538 | << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)"); |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 539 | ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS); |
| 540 | } |
| 541 | } |
| 542 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 543 | template <typename T, typename U> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 544 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value, float tolerance_number, float absolute_tolerance_value) |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 545 | { |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 546 | uint64_t num_mismatches = 0; |
| 547 | uint64_t num_elements = 0; |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 548 | |
| 549 | ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS); |
| 550 | ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS); |
| 551 | |
| 552 | if(reference.format() != Format::UNKNOWN) |
| 553 | { |
| 554 | ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS); |
| 555 | } |
| 556 | |
| 557 | ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS); |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 558 | ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS); |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 559 | |
| 560 | const int min_elements = std::min(tensor.num_elements(), reference.num_elements()); |
| 561 | const int min_channels = std::min(tensor.num_channels(), reference.num_channels()); |
| 562 | |
| 563 | // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ... |
| 564 | for(int element_idx = 0; element_idx < min_elements; ++element_idx) |
| 565 | { |
| 566 | const Coordinates id = index2coord(reference.shape(), element_idx); |
| 567 | |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 568 | Coordinates target_id(id); |
| 569 | if(tensor.data_layout() == DataLayout::NHWC) |
| 570 | { |
| 571 | permute(target_id, PermutationVector(2U, 0U, 1U)); |
| 572 | } |
| 573 | |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 574 | if(valid_mask[element_idx] == 1) |
| 575 | { |
| 576 | // Iterate over all channels within one element |
| 577 | for(int c = 0; c < min_channels; ++c) |
| 578 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 579 | const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c]; |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 580 | const T &reference_value = reinterpret_cast<const T *>(reference(id))[c]; |
| 581 | |
| 582 | if(!compare<U>(target_value, reference_value, tolerance_value)) |
| 583 | { |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 584 | if(absolute_tolerance_value != 0.f) |
| 585 | { |
| 586 | const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value); |
| 587 | if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance)) |
| 588 | { |
| 589 | continue; |
| 590 | } |
| 591 | } |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 592 | ARM_COMPUTE_TEST_INFO("id = " << id); |
| 593 | ARM_COMPUTE_TEST_INFO("channel = " << c); |
| 594 | ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value)); |
| 595 | ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value)); |
| 596 | ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value))); |
| 597 | framework::ARM_COMPUTE_PRINT_INFO(); |
| 598 | |
| 599 | ++num_mismatches; |
| 600 | } |
| 601 | |
| 602 | ++num_elements; |
| 603 | } |
| 604 | } |
| 605 | else |
| 606 | { |
| 607 | ++num_elements; |
| 608 | } |
| 609 | } |
| 610 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 611 | if(num_elements != 0) |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 612 | { |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 613 | const uint64_t absolute_tolerance_number = tolerance_number * num_elements; |
| 614 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f; |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 615 | |
| 616 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 617 | << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)"); |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 618 | ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | template <typename T, typename U> |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 623 | bool validate(T target, T reference, U tolerance) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 624 | { |
| 625 | ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference)); |
| 626 | ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target)); |
| 627 | 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] | 628 | |
| 629 | const bool equal = compare<U>(target, reference, tolerance); |
| 630 | |
| 631 | ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS); |
| 632 | |
| 633 | return equal; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 634 | } |
John Richardson | f89a49f | 2017-09-05 11:21:56 +0100 | [diff] [blame] | 635 | |
| 636 | template <typename T, typename U> |
| 637 | void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference) |
| 638 | { |
| 639 | ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS); |
| 640 | ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS); |
| 641 | |
| 642 | ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS); |
| 643 | ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS); |
| 644 | |
| 645 | for(uint32_t i = 0; i < target.min_loc.size(); ++i) |
| 646 | { |
| 647 | const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord) |
| 648 | { |
| 649 | return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y; |
| 650 | }); |
| 651 | |
| 652 | ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS); |
| 653 | } |
| 654 | |
| 655 | for(uint32_t i = 0; i < target.max_loc.size(); ++i) |
| 656 | { |
| 657 | const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord) |
| 658 | { |
| 659 | return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y; |
| 660 | }); |
| 661 | |
| 662 | ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS); |
| 663 | } |
| 664 | } |
| 665 | |
Abe Mbise | 562fe0f | 2018-02-09 14:13:02 +0000 | [diff] [blame] | 666 | /** Check which keypoints from [first1, last1) are missing in [first2, last2) */ |
| 667 | template <typename T, typename U, typename V> |
| 668 | std::pair<int64_t, int64_t> compare_keypoints(T first1, T last1, U first2, U last2, V tolerance, bool check_mismatches = true) |
| 669 | { |
| 670 | /* Keypoint (x,y) should have similar strength (within tolerance) and other properties in both reference and target */ |
| 671 | const auto compare_props_eq = [&](const KeyPoint & lhs, const KeyPoint & rhs) |
| 672 | { |
| 673 | return compare<V>(lhs.strength, rhs.strength, tolerance) |
| 674 | && lhs.tracking_status == rhs.tracking_status |
| 675 | && lhs.scale == rhs.scale |
| 676 | && lhs.orientation == rhs.orientation |
| 677 | && lhs.error == rhs.error; |
| 678 | }; |
| 679 | |
| 680 | /* Used to sort KeyPoints by coordinates (x, y) */ |
| 681 | const auto compare_coords_lt = [](const KeyPoint & lhs, const KeyPoint & rhs) |
| 682 | { |
| 683 | return std::tie(lhs.x, lhs.y) < std::tie(rhs.x, rhs.y); |
| 684 | }; |
| 685 | |
| 686 | std::sort(first1, last1, compare_coords_lt); |
| 687 | std::sort(first2, last2, compare_coords_lt); |
| 688 | |
| 689 | if(check_mismatches) |
| 690 | { |
Anthony Barbier | 29421bd | 2018-11-02 18:18:47 +0000 | [diff] [blame] | 691 | ARM_COMPUTE_TEST_INFO("Checking for mismatches: ref count = " << std::distance(first1, last1) << " target count = " << std::distance(first2, last2)); |
Abe Mbise | 562fe0f | 2018-02-09 14:13:02 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | int64_t num_missing = 0; |
| 695 | int64_t num_mismatches = 0; |
| 696 | bool rest_missing = false; |
| 697 | |
| 698 | while(first1 != last1) |
| 699 | { |
| 700 | if(first2 == last2) |
| 701 | { |
| 702 | rest_missing = true; |
| 703 | break; |
| 704 | } |
| 705 | |
| 706 | if(compare_coords_lt(*first1, *first2)) |
| 707 | { |
| 708 | ++num_missing; |
| 709 | ARM_COMPUTE_TEST_INFO("Key point not found"); |
| 710 | ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++); |
Georgios Pinitas | da19dab | 2018-08-30 19:55:29 +0100 | [diff] [blame] | 711 | framework::ARM_COMPUTE_PRINT_INFO(); |
Abe Mbise | 562fe0f | 2018-02-09 14:13:02 +0000 | [diff] [blame] | 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); |
Georgios Pinitas | da19dab | 2018-08-30 19:55:29 +0100 | [diff] [blame] | 723 | framework::ARM_COMPUTE_PRINT_INFO(); |
Abe Mbise | 562fe0f | 2018-02-09 14:13:02 +0000 | [diff] [blame] | 724 | } |
| 725 | ++first1; |
| 726 | } |
| 727 | ++first2; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if(rest_missing) |
| 732 | { |
| 733 | while(first1 != last1) |
| 734 | { |
| 735 | ++num_missing; |
| 736 | ARM_COMPUTE_TEST_INFO("Key point not found"); |
| 737 | ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++); |
Georgios Pinitas | da19dab | 2018-08-30 19:55:29 +0100 | [diff] [blame] | 738 | framework::ARM_COMPUTE_PRINT_INFO(); |
Abe Mbise | 562fe0f | 2018-02-09 14:13:02 +0000 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
| 742 | return std::make_pair(num_missing, num_mismatches); |
| 743 | } |
| 744 | |
| 745 | template <typename T, typename U, typename V> |
| 746 | void validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance, float allowed_missing_percentage, float allowed_mismatch_percentage) |
| 747 | { |
| 748 | const int64_t num_elements_target = std::distance(target_first, target_last); |
| 749 | const int64_t num_elements_reference = std::distance(reference_first, reference_last); |
| 750 | |
| 751 | int64_t num_missing = 0; |
| 752 | int64_t num_mismatches = 0; |
| 753 | |
| 754 | if(num_elements_reference > 0) |
| 755 | { |
| 756 | std::tie(num_missing, num_mismatches) = compare_keypoints(reference_first, reference_last, target_first, target_last, tolerance); |
| 757 | |
| 758 | const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f; |
| 759 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f; |
| 760 | |
| 761 | ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in ref are missing from target"); |
| 762 | ARM_COMPUTE_TEST_INFO("Missing (not in tgt): " << num_missing << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_missing |
| 763 | << "% \tMax allowed: " << allowed_missing_percentage << "%"); |
| 764 | ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS); |
| 765 | |
| 766 | ARM_COMPUTE_TEST_INFO(num_mismatches << " keypoints (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched"); |
| 767 | ARM_COMPUTE_TEST_INFO("Mismatched keypoints: " << num_mismatches << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_mismatches |
| 768 | << "% \tMax allowed: " << allowed_mismatch_percentage << "%"); |
| 769 | ARM_COMPUTE_EXPECT(percent_mismatches <= allowed_mismatch_percentage, framework::LogLevel::ERRORS); |
| 770 | } |
| 771 | |
| 772 | if(num_elements_target > 0) |
| 773 | { |
| 774 | // Note: no need to check for mismatches a second time (last argument is 'false') |
| 775 | std::tie(num_missing, num_mismatches) = compare_keypoints(target_first, target_last, reference_first, reference_last, tolerance, false); |
| 776 | |
| 777 | const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f; |
| 778 | |
| 779 | ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in target are missing from ref"); |
| 780 | ARM_COMPUTE_TEST_INFO("Missing (not in ref): " << num_missing << "/" << num_elements_target << " = " << std::fixed << std::setprecision(2) << percent_missing |
| 781 | << "% \tMax allowed: " << allowed_missing_percentage << "%"); |
| 782 | ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS); |
| 783 | } |
| 784 | } |
| 785 | |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 786 | /** Check which detection windows from [first1, last1) are missing in [first2, last2) */ |
| 787 | template <typename T, typename U, typename V> |
| 788 | std::pair<int64_t, int64_t> compare_detection_windows(T first1, T last1, U first2, U last2, V tolerance) |
| 789 | { |
| 790 | int64_t num_missing = 0; |
| 791 | int64_t num_mismatches = 0; |
| 792 | |
| 793 | while(first1 != last1) |
| 794 | { |
| 795 | const auto window = std::find_if(first2, last2, [&](DetectionWindow window) |
| 796 | { |
| 797 | return window.x == first1->x && window.y == first1->y && window.width == first1->width && window.height == first1->height && window.idx_class == first1->idx_class; |
| 798 | }); |
| 799 | |
| 800 | if(window == last2) |
| 801 | { |
| 802 | ++num_missing; |
| 803 | ARM_COMPUTE_TEST_INFO("Detection window not found " << *first1) |
Georgios Pinitas | da19dab | 2018-08-30 19:55:29 +0100 | [diff] [blame] | 804 | framework::ARM_COMPUTE_PRINT_INFO(); |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 805 | } |
| 806 | else |
| 807 | { |
| 808 | if(!compare<V>(window->score, first1->score, tolerance)) |
| 809 | { |
| 810 | ++num_mismatches; |
| 811 | ARM_COMPUTE_TEST_INFO("Mismatching detection window") |
| 812 | ARM_COMPUTE_TEST_INFO("detection window 1= " << *first1) |
| 813 | ARM_COMPUTE_TEST_INFO("detection window 2= " << *window) |
Georgios Pinitas | da19dab | 2018-08-30 19:55:29 +0100 | [diff] [blame] | 814 | framework::ARM_COMPUTE_PRINT_INFO(); |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | ++first1; |
| 819 | } |
| 820 | |
| 821 | return std::make_pair(num_missing, num_mismatches); |
| 822 | } |
| 823 | |
| 824 | template <typename T, typename U, typename V> |
| 825 | void validate_detection_windows(T target_first, T target_last, U reference_first, U reference_last, V tolerance, |
| 826 | float allowed_missing_percentage, float allowed_mismatch_percentage) |
| 827 | { |
| 828 | const int64_t num_elements_target = std::distance(target_first, target_last); |
| 829 | const int64_t num_elements_reference = std::distance(reference_first, reference_last); |
| 830 | |
| 831 | int64_t num_missing = 0; |
| 832 | int64_t num_mismatches = 0; |
| 833 | |
| 834 | if(num_elements_reference > 0) |
| 835 | { |
| 836 | std::tie(num_missing, num_mismatches) = compare_detection_windows(reference_first, reference_last, target_first, target_last, tolerance); |
| 837 | |
| 838 | const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f; |
| 839 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f; |
| 840 | |
| 841 | ARM_COMPUTE_TEST_INFO(num_missing << " detection windows (" << std::fixed << std::setprecision(2) << percent_missing << "%) are missing in target"); |
| 842 | ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS); |
| 843 | |
| 844 | ARM_COMPUTE_TEST_INFO(num_mismatches << " detection windows (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched"); |
| 845 | ARM_COMPUTE_EXPECT(percent_mismatches <= allowed_mismatch_percentage, framework::LogLevel::ERRORS); |
| 846 | } |
| 847 | |
| 848 | if(num_elements_target > 0) |
| 849 | { |
| 850 | std::tie(num_missing, num_mismatches) = compare_detection_windows(target_first, target_last, reference_first, reference_last, tolerance); |
| 851 | |
| 852 | const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f; |
| 853 | |
| 854 | ARM_COMPUTE_TEST_INFO(num_missing << " detection windows (" << std::fixed << std::setprecision(2) << percent_missing << "%) are not part of target"); |
| 855 | ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS); |
| 856 | } |
| 857 | } |
| 858 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 859 | } // namespace validation |
| 860 | } // namespace test |
| 861 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 862 | #endif /* ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H */ |