ArmNN
 20.02
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/Logging.hpp>
9 
10 #include <boost/filesystem.hpp>
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  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
ModelAccuracyChecker(const std::map< std::string, std::string > &validationLabelSet, const std::vector< LabelCategoryNames > &modelOutputLabels)
Constructor for a model top k accuracy checker.
float GetAccuracy(unsigned int k)
Get Top K accuracy.
#define ARMNN_LOG(severity)
Definition: Logging.hpp:163
std::string Strip(const std::string &originalString, const std::string &characterSet)
Remove any preceding and trailing character specified in the characterSet.
std::vector< std::string > SplitBy(const std::string &originalString, const std::string &delimiter, bool includeEmptyToken)
Split a string into tokens by a delimiter.