ArmNN
 24.08
ModelAccuracyChecker.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include <armnn/Exceptions.hpp>
9 #include <armnn/Logging.hpp>
10 
11 #include <map>
12 #include <vector>
13 
14 namespace armnnUtils
15 {
16 
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 {}
22 
23 float ModelAccuracyChecker::GetAccuracy(unsigned int k)
24 {
25  if (k > 10)
26  {
27  ARMNN_LOG(warning) << "Accuracy Tool only supports a maximum of Top 10 Accuracy. "
28  "Printing Top 10 Accuracy result!";
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 }
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  if (characterSet.empty())
68  {
69  throw armnn::InvalidArgumentException("Strip: string of characters to strip is empty");
70  }
71  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 }
80 } // namespace armnnUtils
ARMNN_LOG
#define ARMNN_LOG(severity)
Definition: Logging.hpp:212
Logging.hpp
armnnUtils
Definition: CompatibleTypes.hpp:10
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
ModelAccuracyChecker.hpp
armnnUtils::ModelAccuracyChecker::ModelAccuracyChecker
ModelAccuracyChecker(const std::map< std::string, std::string > &validationLabelSet, const std::vector< LabelCategoryNames > &modelOutputLabels)
Constructor for a model top k accuracy checker.
Definition: ModelAccuracyChecker.cpp:17
armnnUtils::Strip
std::string Strip(const std::string &originalString, const std::string &characterSet)
Remove any preceding and trailing character specified in the characterSet.
Definition: ModelAccuracyChecker.cpp:65
armnnUtils::ModelAccuracyChecker::GetAccuracy
float GetAccuracy(unsigned int k)
Get Top K accuracy.
Definition: ModelAccuracyChecker.cpp:23
Exceptions.hpp
armnn::BoostLogSeverityMapping::warning
@ warning
armnnUtils::SplitBy
std::vector< std::string > SplitBy(const std::string &originalString, const std::string &delimiter, bool includeEmptyToken)
Split a string into tokens by a delimiter.
Definition: ModelAccuracyChecker.cpp:41