alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 19 | #include "TensorFlowLiteMicro.hpp" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 20 | #include "PlatformMath.hpp" |
| 21 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 22 | |
| 23 | #include <vector> |
| 24 | #include <string> |
| 25 | #include <set> |
| 26 | #include <cstdint> |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 27 | #include <cinttypes> |
| 28 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | |
| 30 | namespace arm { |
| 31 | namespace app { |
| 32 | |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 33 | void Classifier::SetVectorResults(std::set<std::pair<float, uint32_t>>& topNSet, |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 34 | std::vector<ClassificationResult>& vecResults, |
| 35 | const std::vector <std::string>& labels) |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 36 | { |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 37 | /* Reset the iterator to the largest element - use reverse iterator. */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 38 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 39 | 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 | } |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 47 | bool Classifier::GetTopNResults(const std::vector<float>& tensor, |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 48 | std::vector<ClassificationResult>& vecResults, |
| 49 | uint32_t topNCount, const std::vector <std::string>& labels) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 50 | { |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 51 | std::set<std::pair<float , uint32_t>> sortedSet; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 52 | |
| 53 | /* NOTE: inputVec's size verification against labels should be |
| 54 | * checked by the calling/public function. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 55 | |
| 56 | /* Set initial elements. */ |
| 57 | for (uint32_t i = 0; i < topNCount; ++i) { |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 58 | sortedSet.insert({tensor[i], i}); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* Initialise iterator. */ |
| 62 | auto setFwdIter = sortedSet.begin(); |
| 63 | |
| 64 | /* Scan through the rest of elements with compare operations. */ |
| 65 | for (uint32_t i = topNCount; i < labels.size(); ++i) { |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 66 | if (setFwdIter->first < tensor[i]) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | sortedSet.erase(*setFwdIter); |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 68 | sortedSet.insert({tensor[i], i}); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | setFwdIter = sortedSet.begin(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* Final results' container. */ |
| 74 | vecResults = std::vector<ClassificationResult>(topNCount); |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 75 | SetVectorResults(sortedSet, vecResults, labels); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 80 | bool Classifier::GetClassificationResults(TfLiteTensor* outputTensor, |
| 81 | std::vector<ClassificationResult>& vecResults, const std::vector <std::string>& labels, |
| 82 | uint32_t topNCount, bool useSoftmax) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 83 | { |
| 84 | if (outputTensor == nullptr) { |
| 85 | printf_err("Output vector is null pointer.\n"); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | uint32_t totalOutputSize = 1; |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 90 | for (int inputDim = 0; inputDim < outputTensor->dims->size; inputDim++) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 91 | totalOutputSize *= outputTensor->dims->data[inputDim]; |
| 92 | } |
| 93 | |
| 94 | /* Sanity checks. */ |
| 95 | if (totalOutputSize < topNCount) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 96 | printf_err("Output vector is smaller than %" PRIu32 "\n", topNCount); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 97 | return false; |
| 98 | } else if (totalOutputSize != labels.size()) { |
| 99 | printf_err("Output size doesn't match the labels' size\n"); |
| 100 | return false; |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 101 | } else if (topNCount == 0) { |
| 102 | printf_err("Top N results cannot be zero\n"); |
| 103 | return false; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool resultState; |
| 107 | vecResults.clear(); |
| 108 | |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 109 | /* De-Quantize Output Tensor */ |
| 110 | QuantParams quantParams = GetTensorQuantParams(outputTensor); |
| 111 | |
| 112 | /* Floating point tensor data to be populated |
| 113 | * NOTE: The assumption here is that the output tensor size isn't too |
| 114 | * big and therefore, there's neglibible impact on heap usage. */ |
| 115 | std::vector<float> tensorData(totalOutputSize); |
| 116 | |
| 117 | /* Populate the floating point buffer */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 118 | switch (outputTensor->type) { |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 119 | case kTfLiteUInt8: { |
| 120 | uint8_t *tensor_buffer = tflite::GetTensorData<uint8_t>(outputTensor); |
| 121 | for (size_t i = 0; i < totalOutputSize; ++i) { |
| 122 | tensorData[i] = quantParams.scale * |
| 123 | (static_cast<float>(tensor_buffer[i]) - quantParams.offset); |
| 124 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 125 | break; |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 126 | } |
| 127 | case kTfLiteInt8: { |
| 128 | int8_t *tensor_buffer = tflite::GetTensorData<int8_t>(outputTensor); |
| 129 | for (size_t i = 0; i < totalOutputSize; ++i) { |
| 130 | tensorData[i] = quantParams.scale * |
| 131 | (static_cast<float>(tensor_buffer[i]) - quantParams.offset); |
| 132 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 133 | break; |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 134 | } |
| 135 | case kTfLiteFloat32: { |
| 136 | float *tensor_buffer = tflite::GetTensorData<float>(outputTensor); |
| 137 | for (size_t i = 0; i < totalOutputSize; ++i) { |
| 138 | tensorData[i] = tensor_buffer[i]; |
| 139 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | break; |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 141 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 142 | default: |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 143 | printf_err("Tensor type %s not supported by classifier\n", |
| 144 | TfLiteTypeGetName(outputTensor->type)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 148 | if (useSoftmax) { |
| 149 | math::MathUtils::SoftmaxF32(tensorData); |
| 150 | } |
| 151 | |
| 152 | /* Get the top N results. */ |
| 153 | resultState = GetTopNResults(tensorData, vecResults, topNCount, labels); |
| 154 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 155 | if (!resultState) { |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 156 | printf_err("Failed to get top N results set\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | return true; |
| 161 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 162 | } /* namespace app */ |
| 163 | } /* namespace arm */ |