blob: 83cd0cbcc644c022d18a65ea79e982c1057803ee [file] [log] [blame]
Éanna Ó Catháina4247d52019-05-08 14:00:45 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Éanna Ó Catháina4247d52019-05-08 14:00:45 +01006#include "ModelAccuracyChecker.hpp"
Derek Lamberti08446972019-11-26 16:38:31 +00007
Jim Flynn39faea82023-09-17 09:02:23 +01008#include <armnn/Exceptions.hpp>
Derek Lamberti08446972019-11-26 16:38:31 +00009#include <armnn/Logging.hpp>
10
SiCong Li898a3242019-06-24 16:03:33 +010011#include <map>
12#include <vector>
Éanna Ó Catháina4247d52019-05-08 14:00:45 +010013
14namespace armnnUtils
15{
16
SiCong Li898a3242019-06-24 16:03:33 +010017armnnUtils::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áina4247d52019-05-08 14:00:45 +010022
23float ModelAccuracyChecker::GetAccuracy(unsigned int k)
24{
SiCong Li898a3242019-06-24 16:03:33 +010025 if (k > 10)
26 {
Derek Lamberti08446972019-11-26 16:38:31 +000027 ARMNN_LOG(warning) << "Accuracy Tool only supports a maximum of Top 10 Accuracy. "
28 "Printing Top 10 Accuracy result!";
Éanna Ó Catháina4247d52019-05-08 14:00:45 +010029 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 Li898a3242019-06-24 16:03:33 +010038
39// Split a string into tokens by a delimiter
40std::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.
65std::string Strip(const std::string& originalString, const std::string& characterSet)
66{
Jim Flynn39faea82023-09-17 09:02:23 +010067 if (characterSet.empty())
68 {
69 throw armnn::InvalidArgumentException("Strip: string of characters to strip is empty");
70 }
SiCong Li898a3242019-06-24 16:03:33 +010071 const std::size_t firstFound = originalString.find_first_not_of(characterSet);
72 const std::size_t lastFound = originalString.find_last_not_of(characterSet);
73 // Return empty if the originalString is empty or the originalString contains only to-be-striped characters
74 if (firstFound == std::string::npos || lastFound == std::string::npos)
75 {
76 return "";
77 }
78 return originalString.substr(firstFound, lastFound + 1 - firstFound);
79}
Jim Flynn39faea82023-09-17 09:02:23 +010080} // namespace armnnUtils