alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 27 | namespace arm { |
| 28 | namespace app { |
| 29 | |
| 30 | template<typename T> |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 31 | 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) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | { |
| 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 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 98 | SetVectorResults<T>(sortedSet, vecResults, tensor, labels); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 103 | template bool Classifier::GetTopNResults<uint8_t>(TfLiteTensor* tensor, |
| 104 | std::vector<ClassificationResult>& vecResults, |
| 105 | uint32_t topNCount, const std::vector <std::string>& labels); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 106 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 107 | template bool Classifier::GetTopNResults<int8_t>(TfLiteTensor* tensor, |
| 108 | std::vector<ClassificationResult>& vecResults, |
| 109 | uint32_t topNCount, const std::vector <std::string>& labels); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 110 | |
| 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; |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 133 | } else if (topNCount == 0) { |
| 134 | printf_err("Top N results cannot be zero\n"); |
| 135 | return false; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool resultState; |
| 139 | vecResults.clear(); |
| 140 | |
| 141 | /* Get the top N results. */ |
| 142 | switch (outputTensor->type) { |
| 143 | case kTfLiteUInt8: |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 144 | resultState = GetTopNResults<uint8_t>(outputTensor, vecResults, topNCount, labels); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 145 | break; |
| 146 | case kTfLiteInt8: |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 147 | resultState = GetTopNResults<int8_t>(outputTensor, vecResults, topNCount, labels); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 148 | break; |
| 149 | case kTfLiteFloat32: |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 150 | resultState = GetTopNResults<float>(outputTensor, vecResults, topNCount, labels); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 151 | 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) { |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 158 | printf_err("Failed to get top N results set\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | } /* namespace app */ |
| 166 | } /* namespace arm */ |