blob: 40de49895a47e67087cc7389032af1e3d76f5bcf [file] [log] [blame]
Richard Burtone6398cd2022-04-13 11:58:28 +01001/*
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#include "KwsProcessing.hpp"
Richard Burtone6398cd2022-04-13 11:58:28 +010018#include "log_macros.h"
19#include "MicroNetKwsModel.hpp"
20
21namespace arm {
22namespace app {
23
Richard Burtonb40ecf82022-04-22 16:14:57 +010024 KwsPreProcess::KwsPreProcess(TfLiteTensor* inputTensor, size_t numFeatures, size_t numMfccFrames,
25 int mfccFrameLength, int mfccFrameStride
26 ):
27 m_inputTensor{inputTensor},
Richard Burtone6398cd2022-04-13 11:58:28 +010028 m_mfccFrameLength{mfccFrameLength},
29 m_mfccFrameStride{mfccFrameStride},
Richard Burtonb40ecf82022-04-22 16:14:57 +010030 m_numMfccFrames{numMfccFrames},
Richard Burtone6398cd2022-04-13 11:58:28 +010031 m_mfcc{audio::MicroNetKwsMFCC(numFeatures, mfccFrameLength)}
32 {
Richard Burtone6398cd2022-04-13 11:58:28 +010033 this->m_mfcc.Init();
34
Richard Burtone6398cd2022-04-13 11:58:28 +010035 /* Deduce the data length required for 1 inference from the network parameters. */
Richard Burtonb40ecf82022-04-22 16:14:57 +010036 this->m_audioDataWindowSize = this->m_numMfccFrames * this->m_mfccFrameStride +
Richard Burtone6398cd2022-04-13 11:58:28 +010037 (this->m_mfccFrameLength - this->m_mfccFrameStride);
38
39 /* Creating an MFCC feature sliding window for the data required for 1 inference. */
40 this->m_mfccSlidingWindow = audio::SlidingWindow<const int16_t>(nullptr, this->m_audioDataWindowSize,
41 this->m_mfccFrameLength, this->m_mfccFrameStride);
42
43 /* For longer audio clips we choose to move by half the audio window size
44 * => for a 1 second window size there is an overlap of 0.5 seconds. */
45 this->m_audioDataStride = this->m_audioDataWindowSize / 2;
46
47 /* To have the previously calculated features re-usable, stride must be multiple
48 * of MFCC features window stride. Reduce stride through audio if needed. */
49 if (0 != this->m_audioDataStride % this->m_mfccFrameStride) {
50 this->m_audioDataStride -= this->m_audioDataStride % this->m_mfccFrameStride;
51 }
52
53 this->m_numMfccVectorsInAudioStride = this->m_audioDataStride / this->m_mfccFrameStride;
54
55 /* Calculate number of the feature vectors in the window overlap region.
56 * These feature vectors will be reused.*/
57 this->m_numReusedMfccVectors = this->m_mfccSlidingWindow.TotalStrides() + 1
58 - this->m_numMfccVectorsInAudioStride;
59
60 /* Construct feature calculation function. */
Richard Burtonb40ecf82022-04-22 16:14:57 +010061 this->m_mfccFeatureCalculator = GetFeatureCalculator(this->m_mfcc, this->m_inputTensor,
Richard Burtone6398cd2022-04-13 11:58:28 +010062 this->m_numReusedMfccVectors);
63
64 if (!this->m_mfccFeatureCalculator) {
65 printf_err("Feature calculator not initialized.");
66 }
67 }
68
Richard Burtonb40ecf82022-04-22 16:14:57 +010069 bool KwsPreProcess::DoPreProcess(const void* data, size_t inputSize)
Richard Burtone6398cd2022-04-13 11:58:28 +010070 {
71 UNUSED(inputSize);
72 if (data == nullptr) {
73 printf_err("Data pointer is null");
74 }
75
76 /* Set the features sliding window to the new address. */
77 auto input = static_cast<const int16_t*>(data);
78 this->m_mfccSlidingWindow.Reset(input);
79
80 /* Cache is only usable if we have more than 1 inference in an audio clip. */
81 bool useCache = this->m_audioWindowIndex > 0 && this->m_numReusedMfccVectors > 0;
82
83 /* Use a sliding window to calculate MFCC features frame by frame. */
84 while (this->m_mfccSlidingWindow.HasNext()) {
85 const int16_t* mfccWindow = this->m_mfccSlidingWindow.Next();
86
87 std::vector<int16_t> mfccFrameAudioData = std::vector<int16_t>(mfccWindow,
88 mfccWindow + this->m_mfccFrameLength);
89
90 /* Compute features for this window and write them to input tensor. */
91 this->m_mfccFeatureCalculator(mfccFrameAudioData, this->m_mfccSlidingWindow.Index(),
92 useCache, this->m_numMfccVectorsInAudioStride);
93 }
94
95 debug("Input tensor populated \n");
96
97 return true;
98 }
99
100 /**
101 * @brief Generic feature calculator factory.
102 *
103 * Returns lambda function to compute features using features cache.
104 * Real features math is done by a lambda function provided as a parameter.
105 * Features are written to input tensor memory.
106 *
107 * @tparam T Feature vector type.
108 * @param[in] inputTensor Model input tensor pointer.
109 * @param[in] cacheSize Number of feature vectors to cache. Defined by the sliding window overlap.
110 * @param[in] compute Features calculator function.
111 * @return Lambda function to compute features.
112 */
113 template<class T>
114 std::function<void (std::vector<int16_t>&, size_t, bool, size_t)>
Richard Burtonb40ecf82022-04-22 16:14:57 +0100115 KwsPreProcess::FeatureCalc(TfLiteTensor* inputTensor, size_t cacheSize,
116 std::function<std::vector<T> (std::vector<int16_t>& )> compute)
Richard Burtone6398cd2022-04-13 11:58:28 +0100117 {
118 /* Feature cache to be captured by lambda function. */
119 static std::vector<std::vector<T>> featureCache = std::vector<std::vector<T>>(cacheSize);
120
121 return [=](std::vector<int16_t>& audioDataWindow,
122 size_t index,
123 bool useCache,
124 size_t featuresOverlapIndex)
125 {
126 T* tensorData = tflite::GetTensorData<T>(inputTensor);
127 std::vector<T> features;
128
129 /* Reuse features from cache if cache is ready and sliding windows overlap.
130 * Overlap is in the beginning of sliding window with a size of a feature cache. */
131 if (useCache && index < featureCache.size()) {
132 features = std::move(featureCache[index]);
133 } else {
134 features = std::move(compute(audioDataWindow));
135 }
136 auto size = features.size();
137 auto sizeBytes = sizeof(T) * size;
138 std::memcpy(tensorData + (index * size), features.data(), sizeBytes);
139
140 /* Start renewing cache as soon iteration goes out of the windows overlap. */
141 if (index >= featuresOverlapIndex) {
142 featureCache[index - featuresOverlapIndex] = std::move(features);
143 }
144 };
145 }
146
147 template std::function<void (std::vector<int16_t>&, size_t , bool, size_t)>
Richard Burtonb40ecf82022-04-22 16:14:57 +0100148 KwsPreProcess::FeatureCalc<int8_t>(TfLiteTensor* inputTensor,
149 size_t cacheSize,
150 std::function<std::vector<int8_t> (std::vector<int16_t>&)> compute);
Richard Burtone6398cd2022-04-13 11:58:28 +0100151
152 template std::function<void(std::vector<int16_t>&, size_t, bool, size_t)>
Richard Burtonb40ecf82022-04-22 16:14:57 +0100153 KwsPreProcess::FeatureCalc<float>(TfLiteTensor* inputTensor,
154 size_t cacheSize,
155 std::function<std::vector<float>(std::vector<int16_t>&)> compute);
Richard Burtone6398cd2022-04-13 11:58:28 +0100156
157
158 std::function<void (std::vector<int16_t>&, int, bool, size_t)>
Richard Burtonb40ecf82022-04-22 16:14:57 +0100159 KwsPreProcess::GetFeatureCalculator(audio::MicroNetKwsMFCC& mfcc, TfLiteTensor* inputTensor, size_t cacheSize)
Richard Burtone6398cd2022-04-13 11:58:28 +0100160 {
161 std::function<void (std::vector<int16_t>&, size_t, bool, size_t)> mfccFeatureCalc;
162
163 TfLiteQuantization quant = inputTensor->quantization;
164
165 if (kTfLiteAffineQuantization == quant.type) {
166 auto *quantParams = (TfLiteAffineQuantization *) quant.params;
167 const float quantScale = quantParams->scale->data[0];
168 const int quantOffset = quantParams->zero_point->data[0];
169
170 switch (inputTensor->type) {
171 case kTfLiteInt8: {
172 mfccFeatureCalc = this->FeatureCalc<int8_t>(inputTensor,
173 cacheSize,
174 [=, &mfcc](std::vector<int16_t>& audioDataWindow) {
175 return mfcc.MfccComputeQuant<int8_t>(audioDataWindow,
176 quantScale,
177 quantOffset);
178 }
179 );
180 break;
181 }
182 default:
183 printf_err("Tensor type %s not supported\n", TfLiteTypeGetName(inputTensor->type));
184 }
185 } else {
186 mfccFeatureCalc = this->FeatureCalc<float>(inputTensor, cacheSize,
187 [&mfcc](std::vector<int16_t>& audioDataWindow) {
188 return mfcc.MfccCompute(audioDataWindow); }
189 );
190 }
191 return mfccFeatureCalc;
192 }
193
Richard Burtonb40ecf82022-04-22 16:14:57 +0100194 KwsPostProcess::KwsPostProcess(TfLiteTensor* outputTensor, Classifier& classifier,
Richard Burtone6398cd2022-04-13 11:58:28 +0100195 const std::vector<std::string>& labels,
Richard Burtonc2911442022-04-22 09:08:21 +0100196 std::vector<ClassificationResult>& results)
Richard Burtonb40ecf82022-04-22 16:14:57 +0100197 :m_outputTensor{outputTensor},
198 m_kwsClassifier{classifier},
Richard Burtone6398cd2022-04-13 11:58:28 +0100199 m_labels{labels},
Richard Burtonc2911442022-04-22 09:08:21 +0100200 m_results{results}
Richard Burtonb40ecf82022-04-22 16:14:57 +0100201 {}
Richard Burtone6398cd2022-04-13 11:58:28 +0100202
Richard Burtonb40ecf82022-04-22 16:14:57 +0100203 bool KwsPostProcess::DoPostProcess()
Richard Burtone6398cd2022-04-13 11:58:28 +0100204 {
205 return this->m_kwsClassifier.GetClassificationResults(
Richard Burtonb40ecf82022-04-22 16:14:57 +0100206 this->m_outputTensor, this->m_results,
Richard Burtone6398cd2022-04-13 11:58:28 +0100207 this->m_labels, 1, true);
208 }
209
210} /* namespace app */
211} /* namespace arm */