Abe Mbise | 25a340f | 2017-12-19 13:00:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef __ARM_COMPUTE_TEST_FAST_VALIDATION_H__ |
| 25 | #define __ARM_COMPUTE_TEST_FAST_VALIDATION_H__ |
| 26 | |
| 27 | #include "Validation.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | namespace validation |
| 34 | { |
| 35 | /** Check which keypoints from [first1, last1) are missing in [first2, last2) */ |
| 36 | template <typename T, typename U, typename V> |
| 37 | std::pair<int64_t, int64_t> fast_compare_keypoints(T first1, T last1, U first2, U last2, V tolerance, bool check_mismatches = true) |
| 38 | { |
| 39 | /* Keypoint (x,y) should have similar strength (within tolerance) and other properties in both reference and target */ |
| 40 | const auto compare_props_eq = [&](const KeyPoint & lhs, const KeyPoint & rhs) |
| 41 | { |
| 42 | return compare<V>(lhs.strength, rhs.strength, tolerance) |
| 43 | && lhs.tracking_status == rhs.tracking_status |
| 44 | && lhs.scale == rhs.scale |
| 45 | && lhs.orientation == rhs.orientation |
| 46 | && lhs.error == rhs.error; |
| 47 | }; |
| 48 | |
| 49 | /* Used to sort KeyPoints by coordinates (x, y) */ |
| 50 | const auto compare_coords_lt = [](const KeyPoint & lhs, const KeyPoint & rhs) |
| 51 | { |
| 52 | return std::tie(lhs.x, lhs.y) < std::tie(rhs.x, rhs.y); |
| 53 | }; |
| 54 | |
| 55 | std::sort(first1, last1, compare_coords_lt); |
| 56 | std::sort(first2, last2, compare_coords_lt); |
| 57 | |
| 58 | if(check_mismatches) |
| 59 | { |
| 60 | std::cout << "ref count = " << std::distance(first1, last1) << " \ttarget count = " << std::distance(first2, last2) << std::endl; |
| 61 | } |
| 62 | |
| 63 | int64_t num_missing = 0; |
| 64 | int64_t num_mismatches = 0; |
| 65 | bool rest_missing = false; |
| 66 | |
| 67 | while(first1 != last1) |
| 68 | { |
| 69 | if(first2 == last2) |
| 70 | { |
| 71 | // num_missing += std::distance(first1, last1); |
| 72 | rest_missing = true; |
| 73 | ARM_COMPUTE_TEST_INFO("All key points from (" << first1->x << "," << first1->y << ") onwards not found"); |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | if(compare_coords_lt(*first1, *first2)) |
| 78 | { |
| 79 | ++num_missing; |
| 80 | ARM_COMPUTE_TEST_INFO("Key point not found"); |
| 81 | ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | if(!compare_coords_lt(*first2, *first1)) // Equal coordinates |
| 86 | { |
| 87 | if(check_mismatches && !compare_props_eq(*first1, *first2)) // Check other properties |
| 88 | { |
| 89 | ++num_mismatches; |
| 90 | ARM_COMPUTE_TEST_INFO("Mismatching keypoint"); |
| 91 | ARM_COMPUTE_TEST_INFO("keypoint1 [ref] = " << *first1); |
| 92 | ARM_COMPUTE_TEST_INFO("keypoint2 [tgt] = " << *first2); |
| 93 | } |
| 94 | ++first1; |
| 95 | } |
| 96 | ++first2; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if(rest_missing) |
| 101 | { |
| 102 | while(first1 != last1) |
| 103 | { |
| 104 | ++num_missing; |
| 105 | ARM_COMPUTE_TEST_INFO("Key point not found"); |
| 106 | ARM_COMPUTE_TEST_INFO("keypoint1 = " << *first1++); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return std::make_pair(num_missing, num_mismatches); |
| 111 | } |
| 112 | |
| 113 | template <typename T, typename U, typename V> |
| 114 | void fast_validate_keypoints(T target_first, T target_last, U reference_first, U reference_last, V tolerance, |
| 115 | float allowed_missing_percentage, float allowed_mismatch_percentage) |
| 116 | { |
| 117 | const int64_t num_elements_target = std::distance(target_first, target_last); |
| 118 | const int64_t num_elements_reference = std::distance(reference_first, reference_last); |
| 119 | |
| 120 | int64_t num_missing = 0; |
| 121 | int64_t num_mismatches = 0; |
| 122 | |
| 123 | if(num_elements_reference > 0) |
| 124 | { |
| 125 | std::tie(num_missing, num_mismatches) = fast_compare_keypoints(reference_first, reference_last, target_first, target_last, tolerance); |
| 126 | |
| 127 | const float percent_missing = static_cast<float>(num_missing) / num_elements_reference * 100.f; |
| 128 | const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements_reference * 100.f; |
| 129 | |
| 130 | ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in ref are missing from target"); |
| 131 | ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS); |
| 132 | |
| 133 | ARM_COMPUTE_TEST_INFO(num_mismatches << " keypoints (" << std::fixed << std::setprecision(2) << percent_mismatches << "%) mismatched"); |
| 134 | ARM_COMPUTE_EXPECT(percent_mismatches <= allowed_mismatch_percentage, framework::LogLevel::ERRORS); |
| 135 | |
| 136 | std::cout << "Mismatched keypoints: " << num_mismatches << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_mismatches |
| 137 | << "% \tMax allowed: " << allowed_mismatch_percentage << "%" << std::endl; |
| 138 | std::cout << "Missing (not in tgt): " << num_missing << "/" << num_elements_reference << " = " << std::fixed << std::setprecision(2) << percent_missing |
| 139 | << "% \tMax allowed: " << allowed_missing_percentage << "%" << std::endl; |
| 140 | } |
| 141 | |
| 142 | if(num_elements_target > 0) |
| 143 | { |
| 144 | // Note: no need to check for mismatches a second time (last argument is 'false') |
| 145 | std::tie(num_missing, num_mismatches) = fast_compare_keypoints(target_first, target_last, reference_first, reference_last, tolerance, false); |
| 146 | |
| 147 | const float percent_missing = static_cast<float>(num_missing) / num_elements_target * 100.f; |
| 148 | |
| 149 | ARM_COMPUTE_TEST_INFO(num_missing << " keypoints (" << std::fixed << std::setprecision(2) << percent_missing << "%) in target are missing from ref"); |
| 150 | ARM_COMPUTE_EXPECT(percent_missing <= allowed_missing_percentage, framework::LogLevel::ERRORS); |
| 151 | |
| 152 | std::cout << "Missing (not in ref): " << num_missing << "/" << num_elements_target << " = " << std::fixed << std::setprecision(2) << percent_missing |
| 153 | << "% \tMax allowed: " << allowed_missing_percentage << "%\n" |
| 154 | << std::endl; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | } // namespace validation |
| 159 | } // namespace test |
| 160 | } // namespace arm_compute |
| 161 | #endif /* __ARM_COMPUTE_TEST_FAST_VALIDATION_H__ */ |