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