blob: a6ff5322d4e0a5aca001a444d84dda886ef23bb3 [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>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010026#include <inttypes.h>
Kshitij Sisodia76a15802021-12-24 11:05:11 +000027#include "PlatformMath.hpp"
alexander3c798932021-03-26 21:42:19 +000028
29namespace arm {
30namespace app {
31
Kshitij Sisodia76a15802021-12-24 11:05:11 +000032 void Classifier::SetVectorResults(std::set<std::pair<float, uint32_t>>& topNSet,
alexanderc350cdc2021-04-29 20:36:09 +010033 std::vector<ClassificationResult>& vecResults,
Kshitij Sisodia76a15802021-12-24 11:05:11 +000034 const std::vector <std::string>& labels)
35 {
alexanderc350cdc2021-04-29 20:36:09 +010036
37 /* Reset the iterator to the largest element - use reverse iterator. */
alexanderc350cdc2021-04-29 20:36:09 +010038
alexanderc350cdc2021-04-29 20:36:09 +010039 auto topNIter = topNSet.rbegin();
40 for (size_t i = 0; i < vecResults.size() && topNIter != topNSet.rend(); ++i, ++topNIter) {
41 vecResults[i].m_normalisedVal = topNIter->first;
42 vecResults[i].m_label = labels[topNIter->second];
43 vecResults[i].m_labelIdx = topNIter->second;
44 }
alexanderc350cdc2021-04-29 20:36:09 +010045 }
46
Kshitij Sisodia76a15802021-12-24 11:05:11 +000047 bool Classifier::GetTopNResults(const std::vector<float>& tensor,
alexanderc350cdc2021-04-29 20:36:09 +010048 std::vector<ClassificationResult>& vecResults,
49 uint32_t topNCount,
50 const std::vector <std::string>& labels)
alexander3c798932021-03-26 21:42:19 +000051 {
Kshitij Sisodia76a15802021-12-24 11:05:11 +000052
53 std::set<std::pair<float , uint32_t>> sortedSet;
alexander3c798932021-03-26 21:42:19 +000054
55 /* NOTE: inputVec's size verification against labels should be
56 * checked by the calling/public function. */
alexander3c798932021-03-26 21:42:19 +000057
58 /* Set initial elements. */
59 for (uint32_t i = 0; i < topNCount; ++i) {
Kshitij Sisodia76a15802021-12-24 11:05:11 +000060 sortedSet.insert({tensor[i], i});
alexander3c798932021-03-26 21:42:19 +000061 }
62
63 /* Initialise iterator. */
64 auto setFwdIter = sortedSet.begin();
65
66 /* Scan through the rest of elements with compare operations. */
67 for (uint32_t i = topNCount; i < labels.size(); ++i) {
Kshitij Sisodia76a15802021-12-24 11:05:11 +000068 if (setFwdIter->first < tensor[i]) {
alexander3c798932021-03-26 21:42:19 +000069 sortedSet.erase(*setFwdIter);
Kshitij Sisodia76a15802021-12-24 11:05:11 +000070 sortedSet.insert({tensor[i], i});
alexander3c798932021-03-26 21:42:19 +000071 setFwdIter = sortedSet.begin();
72 }
73 }
74
75 /* Final results' container. */
76 vecResults = std::vector<ClassificationResult>(topNCount);
Kshitij Sisodia76a15802021-12-24 11:05:11 +000077 SetVectorResults(sortedSet, vecResults, labels);
alexander3c798932021-03-26 21:42:19 +000078
79 return true;
80 }
81
alexander3c798932021-03-26 21:42:19 +000082 bool Classifier::GetClassificationResults(
83 TfLiteTensor* outputTensor,
84 std::vector<ClassificationResult>& vecResults,
Kshitij Sisodia76a15802021-12-24 11:05:11 +000085 const std::vector <std::string>& labels,
86 uint32_t topNCount,
87 bool useSoftmax)
alexander3c798932021-03-26 21:42:19 +000088 {
89 if (outputTensor == nullptr) {
90 printf_err("Output vector is null pointer.\n");
91 return false;
92 }
93
94 uint32_t totalOutputSize = 1;
Kshitij Sisodia76a15802021-12-24 11:05:11 +000095 for (int inputDim = 0; inputDim < outputTensor->dims->size; inputDim++) {
alexander3c798932021-03-26 21:42:19 +000096 totalOutputSize *= outputTensor->dims->data[inputDim];
97 }
98
99 /* Sanity checks. */
100 if (totalOutputSize < topNCount) {
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100101 printf_err("Output vector is smaller than %" PRIu32 "\n", topNCount);
alexander3c798932021-03-26 21:42:19 +0000102 return false;
103 } else if (totalOutputSize != labels.size()) {
104 printf_err("Output size doesn't match the labels' size\n");
105 return false;
alexanderc350cdc2021-04-29 20:36:09 +0100106 } else if (topNCount == 0) {
107 printf_err("Top N results cannot be zero\n");
108 return false;
alexander3c798932021-03-26 21:42:19 +0000109 }
110
111 bool resultState;
112 vecResults.clear();
113
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000114 /* De-Quantize Output Tensor */
115 QuantParams quantParams = GetTensorQuantParams(outputTensor);
116
117 /* Floating point tensor data to be populated
118 * NOTE: The assumption here is that the output tensor size isn't too
119 * big and therefore, there's neglibible impact on heap usage. */
120 std::vector<float> tensorData(totalOutputSize);
121
122 /* Populate the floating point buffer */
alexander3c798932021-03-26 21:42:19 +0000123 switch (outputTensor->type) {
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000124 case kTfLiteUInt8: {
125 uint8_t *tensor_buffer = tflite::GetTensorData<uint8_t>(outputTensor);
126 for (size_t i = 0; i < totalOutputSize; ++i) {
127 tensorData[i] = quantParams.scale *
128 (static_cast<float>(tensor_buffer[i]) - quantParams.offset);
129 }
alexander3c798932021-03-26 21:42:19 +0000130 break;
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000131 }
132 case kTfLiteInt8: {
133 int8_t *tensor_buffer = tflite::GetTensorData<int8_t>(outputTensor);
134 for (size_t i = 0; i < totalOutputSize; ++i) {
135 tensorData[i] = quantParams.scale *
136 (static_cast<float>(tensor_buffer[i]) - quantParams.offset);
137 }
alexander3c798932021-03-26 21:42:19 +0000138 break;
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000139 }
140 case kTfLiteFloat32: {
141 float *tensor_buffer = tflite::GetTensorData<float>(outputTensor);
142 for (size_t i = 0; i < totalOutputSize; ++i) {
143 tensorData[i] = tensor_buffer[i];
144 }
alexander3c798932021-03-26 21:42:19 +0000145 break;
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000146 }
alexander3c798932021-03-26 21:42:19 +0000147 default:
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000148 printf_err("Tensor type %s not supported by classifier\n",
149 TfLiteTypeGetName(outputTensor->type));
alexander3c798932021-03-26 21:42:19 +0000150 return false;
151 }
152
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000153 if (useSoftmax) {
154 math::MathUtils::SoftmaxF32(tensorData);
155 }
156
157 /* Get the top N results. */
158 resultState = GetTopNResults(tensorData, vecResults, topNCount, labels);
159
alexander3c798932021-03-26 21:42:19 +0000160 if (!resultState) {
alexanderc350cdc2021-04-29 20:36:09 +0100161 printf_err("Failed to get top N results set\n");
alexander3c798932021-03-26 21:42:19 +0000162 return false;
163 }
164
165 return true;
166 }
alexander3c798932021-03-26 21:42:19 +0000167} /* namespace app */
168} /* namespace arm */