Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2022 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 | #ifndef KWS_PROCESSING_HPP |
| 18 | #define KWS_PROCESSING_HPP |
| 19 | |
| 20 | #include <AudioUtils.hpp> |
| 21 | #include "BaseProcessing.hpp" |
| 22 | #include "Model.hpp" |
| 23 | #include "Classifier.hpp" |
| 24 | #include "MicroNetKwsMfcc.hpp" |
| 25 | |
| 26 | #include <functional> |
| 27 | |
| 28 | namespace arm { |
| 29 | namespace app { |
| 30 | |
| 31 | /** |
| 32 | * @brief Pre-processing class for Keyword Spotting use case. |
| 33 | * Implements methods declared by BasePreProcess and anything else needed |
| 34 | * to populate input tensors ready for inference. |
| 35 | */ |
| 36 | class KwsPreProcess : public BasePreProcess { |
| 37 | |
| 38 | public: |
| 39 | /** |
| 40 | * @brief Constructor |
| 41 | * @param[in] inputTensor Pointer to the TFLite Micro input Tensor. |
| 42 | * @param[in] numFeatures How many MFCC features to use. |
| 43 | * @param[in] numFeatureFrames Number of MFCC vectors that need to be calculated |
| 44 | * for an inference. |
| 45 | * @param[in] mfccFrameLength Number of audio samples used to calculate one set of MFCC values when |
| 46 | * sliding a window through the audio sample. |
| 47 | * @param[in] mfccFrameStride Number of audio samples between consecutive windows. |
| 48 | **/ |
| 49 | explicit KwsPreProcess(TfLiteTensor* inputTensor, size_t numFeatures, size_t numFeatureFrames, |
| 50 | int mfccFrameLength, int mfccFrameStride); |
| 51 | |
| 52 | /** |
| 53 | * @brief Should perform pre-processing of 'raw' input audio data and load it into |
| 54 | * TFLite Micro input tensors ready for inference. |
| 55 | * @param[in] input Pointer to the data that pre-processing will work on. |
| 56 | * @param[in] inputSize Size of the input data. |
| 57 | * @return true if successful, false otherwise. |
| 58 | **/ |
| 59 | bool DoPreProcess(const void* input, size_t inputSize) override; |
| 60 | |
| 61 | size_t m_audioWindowIndex = 0; /* Index of audio slider, used when caching features in longer clips. */ |
| 62 | size_t m_audioDataWindowSize; /* Amount of audio needed for 1 inference. */ |
| 63 | size_t m_audioDataStride; /* Amount of audio to stride across if doing >1 inference in longer clips. */ |
| 64 | |
| 65 | private: |
| 66 | TfLiteTensor* m_inputTensor; /* Model input tensor. */ |
| 67 | const int m_mfccFrameLength; |
| 68 | const int m_mfccFrameStride; |
| 69 | const size_t m_numMfccFrames; /* How many sets of m_numMfccFeats. */ |
| 70 | |
| 71 | audio::MicroNetKwsMFCC m_mfcc; |
| 72 | audio::SlidingWindow<const int16_t> m_mfccSlidingWindow; |
| 73 | size_t m_numMfccVectorsInAudioStride; |
| 74 | size_t m_numReusedMfccVectors; |
| 75 | std::function<void (std::vector<int16_t>&, int, bool, size_t)> m_mfccFeatureCalculator; |
| 76 | |
| 77 | /** |
| 78 | * @brief Returns a function to perform feature calculation and populates input tensor data with |
| 79 | * MFCC data. |
| 80 | * |
| 81 | * Input tensor data type check is performed to choose correct MFCC feature data type. |
| 82 | * If tensor has an integer data type then original features are quantised. |
| 83 | * |
| 84 | * Warning: MFCC calculator provided as input must have the same life scope as returned function. |
| 85 | * |
| 86 | * @param[in] mfcc MFCC feature calculator. |
| 87 | * @param[in,out] inputTensor Input tensor pointer to store calculated features. |
| 88 | * @param[in] cacheSize Size of the feature vectors cache (number of feature vectors). |
| 89 | * @return Function to be called providing audio sample and sliding window index. |
| 90 | */ |
| 91 | std::function<void (std::vector<int16_t>&, int, bool, size_t)> |
| 92 | GetFeatureCalculator(audio::MicroNetKwsMFCC& mfcc, |
| 93 | TfLiteTensor* inputTensor, |
| 94 | size_t cacheSize); |
| 95 | |
| 96 | template<class T> |
| 97 | std::function<void (std::vector<int16_t>&, size_t, bool, size_t)> |
| 98 | FeatureCalc(TfLiteTensor* inputTensor, size_t cacheSize, |
| 99 | std::function<std::vector<T> (std::vector<int16_t>& )> compute); |
| 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * @brief Post-processing class for Keyword Spotting use case. |
| 104 | * Implements methods declared by BasePostProcess and anything else needed |
| 105 | * to populate result vector. |
| 106 | */ |
| 107 | class KwsPostProcess : public BasePostProcess { |
| 108 | |
| 109 | private: |
| 110 | TfLiteTensor* m_outputTensor; /* Model output tensor. */ |
| 111 | Classifier& m_kwsClassifier; /* KWS Classifier object. */ |
| 112 | const std::vector<std::string>& m_labels; /* KWS Labels. */ |
| 113 | std::vector<ClassificationResult>& m_results; /* Results vector for a single inference. */ |
| 114 | |
| 115 | public: |
| 116 | /** |
| 117 | * @brief Constructor |
| 118 | * @param[in] outputTensor Pointer to the TFLite Micro output Tensor. |
| 119 | * @param[in] classifier Classifier object used to get top N results from classification. |
| 120 | * @param[in] labels Vector of string labels to identify each output of the model. |
| 121 | * @param[in/out] results Vector of classification results to store decoded outputs. |
| 122 | **/ |
| 123 | KwsPostProcess(TfLiteTensor* outputTensor, Classifier& classifier, |
| 124 | const std::vector<std::string>& labels, |
| 125 | std::vector<ClassificationResult>& results); |
| 126 | |
| 127 | /** |
| 128 | * @brief Should perform post-processing of the result of inference then |
| 129 | * populate KWS result data for any later use. |
| 130 | * @return true if successful, false otherwise. |
| 131 | **/ |
| 132 | bool DoPostProcess() override; |
| 133 | }; |
| 134 | |
| 135 | } /* namespace app */ |
| 136 | } /* namespace arm */ |
| 137 | |
| 138 | #endif /* KWS_PROCESSING_HPP */ |