blob: 9a47f3da7d8be7d26b150deaf721f372f4bb9314 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "Classifier.hpp"
18
19#include "hal.h"
20#include "TensorFlowLiteMicro.hpp"
21
22#include <vector>
23#include <string>
24#include <set>
25#include <cstdint>
26
27namespace arm {
28namespace app {
29
30 template<typename T>
alexanderc350cdc2021-04-29 20:36:09 +010031 void SetVectorResults(std::set<std::pair<T, uint32_t>>& topNSet,
32 std::vector<ClassificationResult>& vecResults,
33 TfLiteTensor* tensor,
34 const std::vector <std::string>& labels) {
35
36 /* For getting the floating point values, we need quantization parameters. */
37 QuantParams quantParams = GetTensorQuantParams(tensor);
38
39 /* Reset the iterator to the largest element - use reverse iterator. */
40 auto topNIter = topNSet.rbegin();
41 for (size_t i = 0; i < vecResults.size() && topNIter != topNSet.rend(); ++i, ++topNIter) {
42 T score = topNIter->first;
43 vecResults[i].m_normalisedVal = quantParams.scale * (score - quantParams.offset);
44 vecResults[i].m_label = labels[topNIter->second];
45 vecResults[i].m_labelIdx = topNIter->second;
46 }
47
48 }
49
50 template<>
51 void SetVectorResults<float>(std::set<std::pair<float, uint32_t>>& topNSet,
52 std::vector<ClassificationResult>& vecResults,
53 TfLiteTensor* tensor,
54 const std::vector <std::string>& labels) {
55 UNUSED(tensor);
56 /* Reset the iterator to the largest element - use reverse iterator. */
57 auto topNIter = topNSet.rbegin();
58 for (size_t i = 0; i < vecResults.size() && topNIter != topNSet.rend(); ++i, ++topNIter) {
59 vecResults[i].m_normalisedVal = topNIter->first;
60 vecResults[i].m_label = labels[topNIter->second];
61 vecResults[i].m_labelIdx = topNIter->second;
62 }
63
64 }
65
66 template<typename T>
67 bool Classifier::GetTopNResults(TfLiteTensor* tensor,
68 std::vector<ClassificationResult>& vecResults,
69 uint32_t topNCount,
70 const std::vector <std::string>& labels)
alexander3c798932021-03-26 21:42:19 +000071 {
72 std::set<std::pair<T, uint32_t>> sortedSet;
73
74 /* NOTE: inputVec's size verification against labels should be
75 * checked by the calling/public function. */
76 T* tensorData = tflite::GetTensorData<T>(tensor);
77
78 /* Set initial elements. */
79 for (uint32_t i = 0; i < topNCount; ++i) {
80 sortedSet.insert({tensorData[i], i});
81 }
82
83 /* Initialise iterator. */
84 auto setFwdIter = sortedSet.begin();
85
86 /* Scan through the rest of elements with compare operations. */
87 for (uint32_t i = topNCount; i < labels.size(); ++i) {
88 if (setFwdIter->first < tensorData[i]) {
89 sortedSet.erase(*setFwdIter);
90 sortedSet.insert({tensorData[i], i});
91 setFwdIter = sortedSet.begin();
92 }
93 }
94
95 /* Final results' container. */
96 vecResults = std::vector<ClassificationResult>(topNCount);
97
alexanderc350cdc2021-04-29 20:36:09 +010098 SetVectorResults<T>(sortedSet, vecResults, tensor, labels);
alexander3c798932021-03-26 21:42:19 +000099
100 return true;
101 }
102
alexanderc350cdc2021-04-29 20:36:09 +0100103 template bool Classifier::GetTopNResults<uint8_t>(TfLiteTensor* tensor,
104 std::vector<ClassificationResult>& vecResults,
105 uint32_t topNCount, const std::vector <std::string>& labels);
alexander3c798932021-03-26 21:42:19 +0000106
alexanderc350cdc2021-04-29 20:36:09 +0100107 template bool Classifier::GetTopNResults<int8_t>(TfLiteTensor* tensor,
108 std::vector<ClassificationResult>& vecResults,
109 uint32_t topNCount, const std::vector <std::string>& labels);
alexander3c798932021-03-26 21:42:19 +0000110
111 bool Classifier::GetClassificationResults(
112 TfLiteTensor* outputTensor,
113 std::vector<ClassificationResult>& vecResults,
114 const std::vector <std::string>& labels, uint32_t topNCount)
115 {
116 if (outputTensor == nullptr) {
117 printf_err("Output vector is null pointer.\n");
118 return false;
119 }
120
121 uint32_t totalOutputSize = 1;
122 for (int inputDim = 0; inputDim < outputTensor->dims->size; inputDim++){
123 totalOutputSize *= outputTensor->dims->data[inputDim];
124 }
125
126 /* Sanity checks. */
127 if (totalOutputSize < topNCount) {
128 printf_err("Output vector is smaller than %u\n", topNCount);
129 return false;
130 } else if (totalOutputSize != labels.size()) {
131 printf_err("Output size doesn't match the labels' size\n");
132 return false;
alexanderc350cdc2021-04-29 20:36:09 +0100133 } else if (topNCount == 0) {
134 printf_err("Top N results cannot be zero\n");
135 return false;
alexander3c798932021-03-26 21:42:19 +0000136 }
137
138 bool resultState;
139 vecResults.clear();
140
141 /* Get the top N results. */
142 switch (outputTensor->type) {
143 case kTfLiteUInt8:
alexanderc350cdc2021-04-29 20:36:09 +0100144 resultState = GetTopNResults<uint8_t>(outputTensor, vecResults, topNCount, labels);
alexander3c798932021-03-26 21:42:19 +0000145 break;
146 case kTfLiteInt8:
alexanderc350cdc2021-04-29 20:36:09 +0100147 resultState = GetTopNResults<int8_t>(outputTensor, vecResults, topNCount, labels);
alexander3c798932021-03-26 21:42:19 +0000148 break;
149 case kTfLiteFloat32:
alexanderc350cdc2021-04-29 20:36:09 +0100150 resultState = GetTopNResults<float>(outputTensor, vecResults, topNCount, labels);
alexander3c798932021-03-26 21:42:19 +0000151 break;
152 default:
153 printf_err("Tensor type %s not supported by classifier\n", TfLiteTypeGetName(outputTensor->type));
154 return false;
155 }
156
157 if (!resultState) {
alexanderc350cdc2021-04-29 20:36:09 +0100158 printf_err("Failed to get top N results set\n");
alexander3c798932021-03-26 21:42:19 +0000159 return false;
160 }
161
162 return true;
163 }
164
165} /* namespace app */
166} /* namespace arm */