ArmNN  NotReleased
ModelAccuracyChecker.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <algorithm>
9 #include <armnn/Types.hpp>
10 #include <boost/assert.hpp>
11 #include <boost/variant/apply_visitor.hpp>
12 #include <cstddef>
13 #include <functional>
14 #include <iostream>
15 #include <map>
16 #include <string>
17 #include <vector>
18 
19 namespace armnnUtils
20 {
21 
22 using namespace armnn;
23 
24 // Category names associated with a label
25 using LabelCategoryNames = std::vector<std::string>;
26 
34 std::vector<std::string>
35  SplitBy(const std::string& originalString, const std::string& delimiter = " ", bool includeEmptyToken = false);
36 
43 std::string Strip(const std::string& originalString, const std::string& characterSet = " ");
44 
46 {
47 public:
55  ModelAccuracyChecker(const std::map<std::string, std::string>& validationLabelSet,
56  const std::vector<LabelCategoryNames>& modelOutputLabels);
57 
65  float GetAccuracy(unsigned int k);
66 
72  template <typename TContainer>
73  void AddImageResult(const std::string& imageName, std::vector<TContainer> outputTensor)
74  {
75  // Increment the total number of images processed
76  ++m_ImagesProcessed;
77 
78  std::map<int, float> confidenceMap;
79  auto& output = outputTensor[0];
80 
81  // Create a map of all predictions
82  boost::apply_visitor([&confidenceMap](auto && value)
83  {
84  int index = 0;
85  for (const auto & o : value)
86  {
87  if (o > 0)
88  {
89  confidenceMap.insert(std::pair<int, float>(index, static_cast<float>(o)));
90  }
91  ++index;
92  }
93  },
94  output);
95 
96  // Create a comparator for sorting the map in order of highest probability
97  typedef std::function<bool(std::pair<int, float>, std::pair<int, float>)> Comparator;
98 
99  Comparator compFunctor =
100  [](std::pair<int, float> element1, std::pair<int, float> element2)
101  {
102  return element1.second > element2.second;
103  };
104 
105  // Do the sorting and store in an ordered set
106  std::set<std::pair<int, float>, Comparator> setOfPredictions(
107  confidenceMap.begin(), confidenceMap.end(), compFunctor);
108 
109  const std::string correctLabel = m_GroundTruthLabelSet.at(imageName);
110 
111  unsigned int index = 1;
112  for (std::pair<int, float> element : setOfPredictions)
113  {
114  if (index >= m_TopK.size())
115  {
116  break;
117  }
118  // Check if the ground truth label value is included in the topi prediction.
119  // Note that a prediction can have multiple prediction labels.
120  const LabelCategoryNames predictionLabels = m_ModelOutputLabels[static_cast<size_t>(element.first)];
121  if (std::find(predictionLabels.begin(), predictionLabels.end(), correctLabel) != predictionLabels.end())
122  {
123  ++m_TopK[index];
124  break;
125  }
126  ++index;
127  }
128  }
129 
130 private:
131  const std::map<std::string, std::string> m_GroundTruthLabelSet;
132  const std::vector<LabelCategoryNames> m_ModelOutputLabels;
133  std::vector<unsigned int> m_TopK = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
134  unsigned int m_ImagesProcessed = 0;
135 };
136 } //namespace armnnUtils
137 
std::vector< std::string > SplitBy(const std::string &originalString, const std::string &delimiter, bool includeEmptyToken)
ModelAccuracyChecker(const std::map< std::string, std::string > &validationLabelSet, const std::vector< LabelCategoryNames > &modelOutputLabels)
void AddImageResult(const std::string &imageName, std::vector< TContainer > outputTensor)
std::vector< std::string > LabelCategoryNames
std::string Strip(const std::string &originalString, const std::string &characterSet)