Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [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 KWS_CLASSIFIER_HPP |
| 18 | #define KWS_CLASSIFIER_HPP |
| 19 | |
| 20 | #include "ClassificationResult.hpp" |
| 21 | #include "TensorFlowLiteMicro.hpp" |
| 22 | #include "Classifier.hpp" |
| 23 | |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace arm { |
| 27 | namespace app { |
| 28 | |
| 29 | /** |
| 30 | * @brief KWS Classifier - a helper class to get certain number of top |
| 31 | * results from the output vector from a classification NN. |
| 32 | * Allows for averaging of previous results. |
| 33 | **/ |
| 34 | class KwsClassifier : public Classifier { |
| 35 | public: |
| 36 | |
| 37 | /** |
| 38 | * @brief Gets the top N classification results from the |
| 39 | * output vector. |
| 40 | * @param[in] outputTensor Inference output tensor from an NN model. |
| 41 | * @param[out] vecResults A vector of classification results. |
| 42 | * populated by this function. |
| 43 | * @param[in] labels Labels vector to match classified classes. |
| 44 | * @param[in] topNCount Number of top classifications to pick. Default is 1. |
| 45 | * @param[in] useSoftmax Whether Softmax normalisation should be applied to output. Default is false. |
| 46 | * @param[in/out] resultHistory History of previous classification results to be updated. |
| 47 | * @return true if successful, false otherwise. |
| 48 | **/ |
| 49 | using Classifier::GetClassificationResults; /* We are overloading not overriding. */ |
| 50 | bool GetClassificationResults(TfLiteTensor* outputTensor, std::vector<ClassificationResult>& vecResults, |
| 51 | const std::vector <std::string>& labels, uint32_t topNCount, |
| 52 | bool use_softmax, std::vector<std::vector<float>>& resultHistory); |
| 53 | |
| 54 | /** |
| 55 | * @brief Average the given history of results. |
| 56 | * @param[in] resultHistory The history of results to take on average of. |
| 57 | * @param[out] averageResult The calculated average. |
| 58 | **/ |
| 59 | static void AveragResults(const std::vector<std::vector<float>>& resultHistory, |
| 60 | std::vector<float>& averageResult); |
| 61 | }; |
| 62 | |
| 63 | } /* namespace app */ |
| 64 | } /* namespace arm */ |
| 65 | |
| 66 | #endif /* KWS_CLASSIFIER_HPP */ |