Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 6 | #include "ModelAccuracyChecker.hpp" |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 7 | |
| 8 | #include <armnn/Logging.hpp> |
| 9 | |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 10 | #include <map> |
| 11 | #include <vector> |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 12 | |
| 13 | namespace armnnUtils |
| 14 | { |
| 15 | |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 16 | armnnUtils::ModelAccuracyChecker::ModelAccuracyChecker(const std::map<std::string, std::string>& validationLabels, |
| 17 | const std::vector<LabelCategoryNames>& modelOutputLabels) |
| 18 | : m_GroundTruthLabelSet(validationLabels) |
| 19 | , m_ModelOutputLabels(modelOutputLabels) |
| 20 | {} |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 21 | |
| 22 | float ModelAccuracyChecker::GetAccuracy(unsigned int k) |
| 23 | { |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 24 | if (k > 10) |
| 25 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 26 | ARMNN_LOG(warning) << "Accuracy Tool only supports a maximum of Top 10 Accuracy. " |
| 27 | "Printing Top 10 Accuracy result!"; |
Éanna Ó Catháin | a4247d5 | 2019-05-08 14:00:45 +0100 | [diff] [blame] | 28 | k = 10; |
| 29 | } |
| 30 | unsigned int total = 0; |
| 31 | for (unsigned int i = k; i > 0; --i) |
| 32 | { |
| 33 | total += m_TopK[i]; |
| 34 | } |
| 35 | return static_cast<float>(total * 100) / static_cast<float>(m_ImagesProcessed); |
| 36 | } |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 37 | |
| 38 | // Split a string into tokens by a delimiter |
| 39 | std::vector<std::string> |
| 40 | SplitBy(const std::string& originalString, const std::string& delimiter, bool includeEmptyToken) |
| 41 | { |
| 42 | std::vector<std::string> tokens; |
| 43 | size_t cur = 0; |
| 44 | size_t next = 0; |
| 45 | while ((next = originalString.find(delimiter, cur)) != std::string::npos) |
| 46 | { |
| 47 | // Skip empty tokens, unless explicitly stated to include them. |
| 48 | if (next - cur > 0 || includeEmptyToken) |
| 49 | { |
| 50 | tokens.push_back(originalString.substr(cur, next - cur)); |
| 51 | } |
| 52 | cur = next + delimiter.size(); |
| 53 | } |
| 54 | // Get the remaining token |
| 55 | // Skip empty tokens, unless explicitly stated to include them. |
| 56 | if (originalString.size() - cur > 0 || includeEmptyToken) |
| 57 | { |
| 58 | tokens.push_back(originalString.substr(cur, originalString.size() - cur)); |
| 59 | } |
| 60 | return tokens; |
| 61 | } |
| 62 | |
| 63 | // Remove any preceding and trailing character specified in the characterSet. |
| 64 | std::string Strip(const std::string& originalString, const std::string& characterSet) |
| 65 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 66 | ARMNN_ASSERT(!characterSet.empty()); |
SiCong Li | 898a324 | 2019-06-24 16:03:33 +0100 | [diff] [blame] | 67 | const std::size_t firstFound = originalString.find_first_not_of(characterSet); |
| 68 | const std::size_t lastFound = originalString.find_last_not_of(characterSet); |
| 69 | // Return empty if the originalString is empty or the originalString contains only to-be-striped characters |
| 70 | if (firstFound == std::string::npos || lastFound == std::string::npos) |
| 71 | { |
| 72 | return ""; |
| 73 | } |
| 74 | return originalString.substr(firstFound, lastFound + 1 - firstFound); |
| 75 | } |
| 76 | } // namespace armnnUtils |