alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
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 | #ifndef CLASSIFIER_HPP |
| 18 | #define CLASSIFIER_HPP |
| 19 | |
| 20 | #include "ClassificationResult.hpp" |
| 21 | #include "TensorFlowLiteMicro.hpp" |
| 22 | |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace arm { |
| 26 | namespace app { |
| 27 | |
| 28 | /** |
| 29 | * @brief Classifier - a helper class to get certain number of top |
| 30 | * results from the output vector from a classification NN. |
| 31 | **/ |
| 32 | class Classifier{ |
| 33 | public: |
| 34 | /** @brief Constructor. */ |
| 35 | Classifier() = default; |
| 36 | |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 37 | virtual ~Classifier() = default; |
| 38 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 39 | /** |
| 40 | * @brief Gets the top N classification results from the |
| 41 | * output vector. |
| 42 | * @param[in] outputTensor Inference output tensor from an NN model. |
| 43 | * @param[out] vecResults A vector of classification results. |
| 44 | * populated by this function. |
| 45 | * @param[in] labels Labels vector to match classified classes. |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 46 | * @param[in] topNCount Number of top classifications to pick. |
| 47 | * @param[in] useSoftmax Whether Softmax normalisation should be applied to output. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 48 | * @return true if successful, false otherwise. |
| 49 | **/ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 50 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | virtual bool GetClassificationResults( |
| 52 | TfLiteTensor* outputTensor, |
| 53 | std::vector<ClassificationResult>& vecResults, |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 54 | const std::vector <std::string>& labels, uint32_t topNCount, |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 55 | bool use_softmax); |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * @brief Populate the elements of the Classification Result object. |
| 59 | * @param[in] topNSet Ordered set of top 5 output class scores and labels. |
| 60 | * @param[out] vecResults A vector of classification results. |
| 61 | * populated by this function. |
| 62 | * @param[in] labels Labels vector to match classified classes. |
| 63 | **/ |
| 64 | |
| 65 | void SetVectorResults( |
| 66 | std::set<std::pair<float, uint32_t>>& topNSet, |
| 67 | std::vector<ClassificationResult>& vecResults, |
| 68 | const std::vector <std::string>& labels); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 70 | protected: |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | /** |
| 72 | * @brief Utility function that gets the top N classification results from the |
| 73 | * output vector. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 74 | * @param[in] tensor Inference output tensor from an NN model. |
| 75 | * @param[out] vecResults A vector of classification results |
| 76 | * populated by this function. |
| 77 | * @param[in] topNCount Number of top classifications to pick. |
| 78 | * @param[in] labels Labels vector to match classified classes. |
| 79 | * @return true if successful, false otherwise. |
| 80 | **/ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 81 | |
| 82 | bool GetTopNResults(const std::vector<float>& tensor, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 83 | std::vector<ClassificationResult>& vecResults, |
| 84 | uint32_t topNCount, |
| 85 | const std::vector <std::string>& labels); |
| 86 | }; |
| 87 | |
| 88 | } /* namespace app */ |
| 89 | } /* namespace arm */ |
| 90 | |
| 91 | #endif /* CLASSIFIER_HPP */ |