diff options
author | Richard Burton <richard.burton@arm.com> | 2022-10-05 11:00:37 +0100 |
---|---|---|
committer | Richard Burton <richard.burton@arm.com> | 2022-10-06 14:08:13 +0100 |
commit | ec5e99be3ae6dd0d3811950f155b01e144431452 (patch) | |
tree | a5d6c4dd9267db2465063b8d0e1a5cb6d19dac8d /source/application/api/use_case/kws/include | |
parent | 890b2b89cacc6f2291596a001d555d374c8c9edd (diff) | |
download | ml-embedded-evaluation-kit-ec5e99be3ae6dd0d3811950f155b01e144431452.tar.gz |
MLECO-3164: Additional refactoring of KWS API
Part 1
* Add KwsClassifier
* KwsPostProcess can now be told to average results
* Averaging is handlded by KwsClassifier
* Current sliding window index is now an argument of DoPreProcess
Change-Id: I07626da595ad1cbd982e8366f0d1bb56d1040459
Diffstat (limited to 'source/application/api/use_case/kws/include')
3 files changed, 77 insertions, 12 deletions
diff --git a/source/application/api/use_case/kws/include/KwsClassifier.hpp b/source/application/api/use_case/kws/include/KwsClassifier.hpp new file mode 100644 index 0000000..d050e85 --- /dev/null +++ b/source/application/api/use_case/kws/include/KwsClassifier.hpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef KWS_CLASSIFIER_HPP +#define KWS_CLASSIFIER_HPP + +#include "ClassificationResult.hpp" +#include "TensorFlowLiteMicro.hpp" +#include "Classifier.hpp" + +#include <vector> + +namespace arm { +namespace app { + + /** + * @brief KWS Classifier - a helper class to get certain number of top + * results from the output vector from a classification NN. + * Allows for averaging of previous results. + **/ + class KwsClassifier : public Classifier { + public: + + /** + * @brief Gets the top N classification results from the + * output vector. + * @param[in] outputTensor Inference output tensor from an NN model. + * @param[out] vecResults A vector of classification results. + * populated by this function. + * @param[in] labels Labels vector to match classified classes. + * @param[in] topNCount Number of top classifications to pick. Default is 1. + * @param[in] useSoftmax Whether Softmax normalisation should be applied to output. Default is false. + * @param[in/out] resultHistory History of previous classification results to be updated. + * @return true if successful, false otherwise. + **/ + using Classifier::GetClassificationResults; /* We are overloading not overriding. */ + bool GetClassificationResults(TfLiteTensor* outputTensor, std::vector<ClassificationResult>& vecResults, + const std::vector <std::string>& labels, uint32_t topNCount, + bool use_softmax, std::vector<std::vector<float>>& resultHistory); + + /** + * @brief Average the given history of results. + * @param[in] resultHistory The history of results to take on average of. + * @param[out] averageResult The calculated average. + **/ + static void AveragResults(const std::vector<std::vector<float>>& resultHistory, + std::vector<float>& averageResult); + }; + +} /* namespace app */ +} /* namespace arm */ + +#endif /* KWS_CLASSIFIER_HPP */ diff --git a/source/application/api/use_case/kws/include/KwsProcessing.hpp b/source/application/api/use_case/kws/include/KwsProcessing.hpp index 0ede425..e2d3ff9 100644 --- a/source/application/api/use_case/kws/include/KwsProcessing.hpp +++ b/source/application/api/use_case/kws/include/KwsProcessing.hpp @@ -19,7 +19,7 @@ #include "AudioUtils.hpp" #include "BaseProcessing.hpp" -#include "Classifier.hpp" +#include "KwsClassifier.hpp" #include "MicroNetKwsMfcc.hpp" #include <functional> @@ -55,9 +55,8 @@ namespace app { * @param[in] inputSize Size of the input data. * @return true if successful, false otherwise. **/ - bool DoPreProcess(const void* input, size_t inputSize) override; + bool DoPreProcess(const void* input, size_t inferenceIndex = 0) override; - size_t m_audioWindowIndex = 0; /* Index of audio slider, used when caching features in longer clips. */ size_t m_audioDataWindowSize; /* Amount of audio needed for 1 inference. */ size_t m_audioDataStride; /* Amount of audio to stride across if doing >1 inference in longer clips. */ @@ -106,11 +105,11 @@ namespace app { class KwsPostProcess : public BasePostProcess { private: - TfLiteTensor* m_outputTensor; /* Model output tensor. */ - Classifier& m_kwsClassifier; /* KWS Classifier object. */ - const std::vector<std::string>& m_labels; /* KWS Labels. */ - std::vector<ClassificationResult>& m_results; /* Results vector for a single inference. */ - + TfLiteTensor* m_outputTensor; /* Model output tensor. */ + KwsClassifier& m_kwsClassifier; /* KWS Classifier object. */ + const std::vector<std::string>& m_labels; /* KWS Labels. */ + std::vector<ClassificationResult>& m_results; /* Results vector for a single inference. */ + std::vector<std::vector<float>> m_resultHistory; /* Store previous results so they can be averaged. */ public: /** * @brief Constructor @@ -119,9 +118,9 @@ namespace app { * @param[in] labels Vector of string labels to identify each output of the model. * @param[in/out] results Vector of classification results to store decoded outputs. **/ - KwsPostProcess(TfLiteTensor* outputTensor, Classifier& classifier, + KwsPostProcess(TfLiteTensor* outputTensor, KwsClassifier& classifier, const std::vector<std::string>& labels, - std::vector<ClassificationResult>& results); + std::vector<ClassificationResult>& results, size_t averagingWindowLen = 1); /** * @brief Should perform post-processing of the result of inference then diff --git a/source/application/api/use_case/kws/include/KwsResult.hpp b/source/application/api/use_case/kws/include/KwsResult.hpp index 38f32b4..e0bb868 100644 --- a/source/application/api/use_case/kws/include/KwsResult.hpp +++ b/source/application/api/use_case/kws/include/KwsResult.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. All rights reserved. + * Copyright (c) 2021-2022 Arm Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -47,7 +47,7 @@ namespace kws { this->m_inferenceNumber = inferenceIdx; this->m_resultVec = ResultVec(); - for (auto & i : resultVec) { + for (auto& i : resultVec) { if (i.m_normalisedVal >= this->m_threshold) { this->m_resultVec.emplace_back(i); } |