blob: d197dc86ba5c404dbe93a4592b7ae57dc3461a1b [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
8#include <armnn/Logging.hpp>
9
SiCong Li898a3242019-06-24 16:03:33 +010010#include <boost/filesystem.hpp>
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{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010067 ARMNN_ASSERT(!characterSet.empty());
SiCong Li898a3242019-06-24 16:03:33 +010068 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