diff options
Diffstat (limited to 'source/use_case/kws/include')
-rw-r--r-- | source/use_case/kws/include/KwsProcessing.hpp | 138 | ||||
-rw-r--r-- | source/use_case/kws/include/KwsResult.hpp | 63 | ||||
-rw-r--r-- | source/use_case/kws/include/MicroNetKwsMfcc.hpp | 50 | ||||
-rw-r--r-- | source/use_case/kws/include/MicroNetKwsModel.hpp | 59 |
4 files changed, 0 insertions, 310 deletions
diff --git a/source/use_case/kws/include/KwsProcessing.hpp b/source/use_case/kws/include/KwsProcessing.hpp deleted file mode 100644 index d3de3b3..0000000 --- a/source/use_case/kws/include/KwsProcessing.hpp +++ /dev/null @@ -1,138 +0,0 @@ -/* - * 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_PROCESSING_HPP -#define KWS_PROCESSING_HPP - -#include <AudioUtils.hpp> -#include "BaseProcessing.hpp" -#include "Model.hpp" -#include "Classifier.hpp" -#include "MicroNetKwsMfcc.hpp" - -#include <functional> - -namespace arm { -namespace app { - - /** - * @brief Pre-processing class for Keyword Spotting use case. - * Implements methods declared by BasePreProcess and anything else needed - * to populate input tensors ready for inference. - */ - class KwsPreProcess : public BasePreProcess { - - public: - /** - * @brief Constructor - * @param[in] inputTensor Pointer to the TFLite Micro input Tensor. - * @param[in] numFeatures How many MFCC features to use. - * @param[in] numFeatureFrames Number of MFCC vectors that need to be calculated - * for an inference. - * @param[in] mfccFrameLength Number of audio samples used to calculate one set of MFCC values when - * sliding a window through the audio sample. - * @param[in] mfccFrameStride Number of audio samples between consecutive windows. - **/ - explicit KwsPreProcess(TfLiteTensor* inputTensor, size_t numFeatures, size_t numFeatureFrames, - int mfccFrameLength, int mfccFrameStride); - - /** - * @brief Should perform pre-processing of 'raw' input audio data and load it into - * TFLite Micro input tensors ready for inference. - * @param[in] input Pointer to the data that pre-processing will work on. - * @param[in] inputSize Size of the input data. - * @return true if successful, false otherwise. - **/ - bool DoPreProcess(const void* input, size_t inputSize) 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. */ - - private: - TfLiteTensor* m_inputTensor; /* Model input tensor. */ - const int m_mfccFrameLength; - const int m_mfccFrameStride; - const size_t m_numMfccFrames; /* How many sets of m_numMfccFeats. */ - - audio::MicroNetKwsMFCC m_mfcc; - audio::SlidingWindow<const int16_t> m_mfccSlidingWindow; - size_t m_numMfccVectorsInAudioStride; - size_t m_numReusedMfccVectors; - std::function<void (std::vector<int16_t>&, int, bool, size_t)> m_mfccFeatureCalculator; - - /** - * @brief Returns a function to perform feature calculation and populates input tensor data with - * MFCC data. - * - * Input tensor data type check is performed to choose correct MFCC feature data type. - * If tensor has an integer data type then original features are quantised. - * - * Warning: MFCC calculator provided as input must have the same life scope as returned function. - * - * @param[in] mfcc MFCC feature calculator. - * @param[in,out] inputTensor Input tensor pointer to store calculated features. - * @param[in] cacheSize Size of the feature vectors cache (number of feature vectors). - * @return Function to be called providing audio sample and sliding window index. - */ - std::function<void (std::vector<int16_t>&, int, bool, size_t)> - GetFeatureCalculator(audio::MicroNetKwsMFCC& mfcc, - TfLiteTensor* inputTensor, - size_t cacheSize); - - template<class T> - std::function<void (std::vector<int16_t>&, size_t, bool, size_t)> - FeatureCalc(TfLiteTensor* inputTensor, size_t cacheSize, - std::function<std::vector<T> (std::vector<int16_t>& )> compute); - }; - - /** - * @brief Post-processing class for Keyword Spotting use case. - * Implements methods declared by BasePostProcess and anything else needed - * to populate result vector. - */ - 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. */ - - public: - /** - * @brief Constructor - * @param[in] outputTensor Pointer to the TFLite Micro output Tensor. - * @param[in] classifier Classifier object used to get top N results from classification. - * @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, - const std::vector<std::string>& labels, - std::vector<ClassificationResult>& results); - - /** - * @brief Should perform post-processing of the result of inference then - * populate KWS result data for any later use. - * @return true if successful, false otherwise. - **/ - bool DoPostProcess() override; - }; - -} /* namespace app */ -} /* namespace arm */ - -#endif /* KWS_PROCESSING_HPP */
\ No newline at end of file diff --git a/source/use_case/kws/include/KwsResult.hpp b/source/use_case/kws/include/KwsResult.hpp deleted file mode 100644 index 38f32b4..0000000 --- a/source/use_case/kws/include/KwsResult.hpp +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2021 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_RESULT_HPP -#define KWS_RESULT_HPP - -#include "ClassificationResult.hpp" - -#include <vector> - -namespace arm { -namespace app { -namespace kws { - - using ResultVec = std::vector<arm::app::ClassificationResult>; - - /* Structure for holding kws result. */ - class KwsResult { - - public: - ResultVec m_resultVec; /* Container for "thresholded" classification results. */ - float m_timeStamp; /* Audio timestamp for this result. */ - uint32_t m_inferenceNumber; /* Corresponding inference number. */ - float m_threshold; /* Threshold value for `m_resultVec`. */ - - KwsResult() = delete; - KwsResult(ResultVec& resultVec, - const float timestamp, - const uint32_t inferenceIdx, - const float scoreThreshold) { - - this->m_threshold = scoreThreshold; - this->m_timeStamp = timestamp; - this->m_inferenceNumber = inferenceIdx; - - this->m_resultVec = ResultVec(); - for (auto & i : resultVec) { - if (i.m_normalisedVal >= this->m_threshold) { - this->m_resultVec.emplace_back(i); - } - } - } - ~KwsResult() = default; - }; - -} /* namespace kws */ -} /* namespace app */ -} /* namespace arm */ - -#endif /* KWS_RESULT_HPP */
\ No newline at end of file diff --git a/source/use_case/kws/include/MicroNetKwsMfcc.hpp b/source/use_case/kws/include/MicroNetKwsMfcc.hpp deleted file mode 100644 index b2565a3..0000000 --- a/source/use_case/kws/include/MicroNetKwsMfcc.hpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2021 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_MICRONET_MFCC_HPP -#define KWS_MICRONET_MFCC_HPP - -#include "Mfcc.hpp" - -namespace arm { -namespace app { -namespace audio { - - /* Class to provide MicroNet specific MFCC calculation requirements. */ - class MicroNetKwsMFCC : public MFCC { - - public: - static constexpr uint32_t ms_defaultSamplingFreq = 16000; - static constexpr uint32_t ms_defaultNumFbankBins = 40; - static constexpr uint32_t ms_defaultMelLoFreq = 20; - static constexpr uint32_t ms_defaultMelHiFreq = 4000; - static constexpr bool ms_defaultUseHtkMethod = true; - - explicit MicroNetKwsMFCC(const size_t numFeats, const size_t frameLen) - : MFCC(MfccParams( - ms_defaultSamplingFreq, ms_defaultNumFbankBins, - ms_defaultMelLoFreq, ms_defaultMelHiFreq, - numFeats, frameLen, ms_defaultUseHtkMethod)) - {} - MicroNetKwsMFCC() = delete; - ~MicroNetKwsMFCC() = default; - }; - -} /* namespace audio */ -} /* namespace app */ -} /* namespace arm */ - -#endif /* KWS_MICRONET_MFCC_HPP */
\ No newline at end of file diff --git a/source/use_case/kws/include/MicroNetKwsModel.hpp b/source/use_case/kws/include/MicroNetKwsModel.hpp deleted file mode 100644 index 3259c45..0000000 --- a/source/use_case/kws/include/MicroNetKwsModel.hpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2021 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_MICRONETMODEL_HPP -#define KWS_MICRONETMODEL_HPP - -#include "Model.hpp" - -extern const int g_FrameLength; -extern const int g_FrameStride; -extern const float g_ScoreThreshold; - -namespace arm { -namespace app { - - class MicroNetKwsModel : public Model { - public: - /* Indices for the expected model - based on input and output tensor shapes */ - static constexpr uint32_t ms_inputRowsIdx = 1; - static constexpr uint32_t ms_inputColsIdx = 2; - static constexpr uint32_t ms_outputRowsIdx = 2; - static constexpr uint32_t ms_outputColsIdx = 3; - - protected: - /** @brief Gets the reference to op resolver interface class. */ - const tflite::MicroOpResolver& GetOpResolver() override; - - /** @brief Adds operations to the op resolver instance. */ - bool EnlistOperations() override; - - const uint8_t* ModelPointer() override; - - size_t ModelSize() override; - - private: - /* Maximum number of individual operations that can be enlisted. */ - static constexpr int ms_maxOpCnt = 7; - - /* A mutable op resolver instance. */ - tflite::MicroMutableOpResolver<ms_maxOpCnt> m_opResolver; - }; - -} /* namespace app */ -} /* namespace arm */ - -#endif /* KWS_MICRONETMODEL_HPP */ |