xref: /aosp_15_r20/external/armnn/src/armnnUtils/ModelAccuracyChecker.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ModelAccuracyChecker.hpp"
7 
8 #include <armnn/Logging.hpp>
9 
10 #include <map>
11 #include <vector>
12 
13 namespace armnnUtils
14 {
15 
ModelAccuracyChecker(const std::map<std::string,std::string> & validationLabels,const std::vector<LabelCategoryNames> & modelOutputLabels)16 armnnUtils::ModelAccuracyChecker::ModelAccuracyChecker(const std::map<std::string, std::string>& validationLabels,
17                                                        const std::vector<LabelCategoryNames>& modelOutputLabels)
18     : m_GroundTruthLabelSet(validationLabels)
19     , m_ModelOutputLabels(modelOutputLabels)
20 {}
21 
GetAccuracy(unsigned int k)22 float ModelAccuracyChecker::GetAccuracy(unsigned int k)
23 {
24     if (k > 10)
25     {
26         ARMNN_LOG(warning) << "Accuracy Tool only supports a maximum of Top 10 Accuracy. "
27                               "Printing Top 10 Accuracy result!";
28         k = 10;
29     }
30     unsigned int total = 0;
31     for (unsigned int i = k; i > 0; --i)
32     {
33         total += m_TopK[i];
34     }
35     return static_cast<float>(total * 100) / static_cast<float>(m_ImagesProcessed);
36 }
37 
38 // Split a string into tokens by a delimiter
39 std::vector<std::string>
SplitBy(const std::string & originalString,const std::string & delimiter,bool includeEmptyToken)40     SplitBy(const std::string& originalString, const std::string& delimiter, bool includeEmptyToken)
41 {
42     std::vector<std::string> tokens;
43     size_t cur  = 0;
44     size_t next = 0;
45     while ((next = originalString.find(delimiter, cur)) != std::string::npos)
46     {
47         // Skip empty tokens, unless explicitly stated to include them.
48         if (next - cur > 0 || includeEmptyToken)
49         {
50             tokens.push_back(originalString.substr(cur, next - cur));
51         }
52         cur = next + delimiter.size();
53     }
54     // Get the remaining token
55     // Skip empty tokens, unless explicitly stated to include them.
56     if (originalString.size() - cur > 0 || includeEmptyToken)
57     {
58         tokens.push_back(originalString.substr(cur, originalString.size() - cur));
59     }
60     return tokens;
61 }
62 
63 // Remove any preceding and trailing character specified in the characterSet.
Strip(const std::string & originalString,const std::string & characterSet)64 std::string Strip(const std::string& originalString, const std::string& characterSet)
65 {
66     ARMNN_ASSERT(!characterSet.empty());
67     const std::size_t firstFound = originalString.find_first_not_of(characterSet);
68     const std::size_t lastFound  = originalString.find_last_not_of(characterSet);
69     // Return empty if the originalString is empty or the originalString contains only to-be-striped characters
70     if (firstFound == std::string::npos || lastFound == std::string::npos)
71     {
72         return "";
73     }
74     return originalString.substr(firstFound, lastFound + 1 - firstFound);
75 }
76 }    // namespace armnnUtils