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