blob: c18a0a48559b59c80cdeb1498add8d38f83329fa [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 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 "UseCaseHandler.hpp"
18
19#include "AdModel.hpp"
20#include "InputFiles.hpp"
21#include "Classifier.hpp"
22#include "hal.h"
23#include "AdMelSpectrogram.hpp"
24#include "AudioUtils.hpp"
25#include "UseCaseCommonUtils.hpp"
26#include "AdPostProcessing.hpp"
27
28namespace arm {
29namespace app {
30
31 /**
32 * @brief Helper function to increment current audio clip index
33 * @param[in/out] ctx pointer to the application context object
34 **/
35 static void _IncrementAppCtxClipIdx(ApplicationContext& ctx);
36
37 /**
38 * @brief Helper function to set the audio clip index
39 * @param[in/out] ctx pointer to the application context object
40 * @param[in] idx value to be set
41 * @return true if index is set, false otherwise
42 **/
43 static bool _SetAppCtxClipIdx(ApplicationContext& ctx, uint32_t idx);
44
45 /**
46 * @brief Presents inference results using the data presentation
47 * object.
48 * @param[in] platform reference to the hal platform object
49 * @param[in] result average sum of classification results
50 * @param[in] threhsold if larger than this value we have an anomaly
51 * @return true if successful, false otherwise
52 **/
53 static bool _PresentInferenceResult(hal_platform& platform, float result, float threshold);
54
55 /**
56 * @brief Returns a function to perform feature calculation and populates input tensor data with
57 * MelSpe data.
58 *
59 * Input tensor data type check is performed to choose correct MFCC feature data type.
60 * If tensor has an integer data type then original features are quantised.
61 *
62 * Warning: mfcc calculator provided as input must have the same life scope as returned function.
63 *
64 * @param[in] mfcc MFCC feature calculator.
65 * @param[in/out] inputTensor Input tensor pointer to store calculated features.
66 * @param[i] cacheSize Size of the feture vectors cache (number of feature vectors).
67 * @return function function to be called providing audio sample and sliding window index.
68 */
69 static std::function<void (std::vector<int16_t>&, int, bool, size_t, size_t)>
70 GetFeatureCalculator(audio::AdMelSpectrogram& melSpec,
71 TfLiteTensor* inputTensor,
72 size_t cacheSize,
73 float trainingMean);
74
75 /* Vibration classification handler */
76 bool ClassifyVibrationHandler(ApplicationContext& ctx, uint32_t clipIndex, bool runAll)
77 {
78 auto& platform = ctx.Get<hal_platform&>("platform");
79
80 constexpr uint32_t dataPsnTxtInfStartX = 20;
81 constexpr uint32_t dataPsnTxtInfStartY = 40;
82
83 platform.data_psn->clear(COLOR_BLACK);
84
85 auto& model = ctx.Get<Model&>("model");
86
87 /* If the request has a valid size, set the audio index */
88 if (clipIndex < NUMBER_OF_FILES) {
89 if (!_SetAppCtxClipIdx(ctx, clipIndex)) {
90 return false;
91 }
92 }
93 if (!model.IsInited()) {
94 printf_err("Model is not initialised! Terminating processing.\n");
95 return false;
96 }
97
98 const auto frameLength = ctx.Get<int>("frameLength");
99 const auto frameStride = ctx.Get<int>("frameStride");
100 const auto scoreThreshold = ctx.Get<float>("scoreThreshold");
101 const float trainingMean = ctx.Get<float>("trainingMean");
102 auto startClipIdx = ctx.Get<uint32_t>("clipIndex");
103
104 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
105 TfLiteTensor* inputTensor = model.GetInputTensor(0);
106
107 if (!inputTensor->dims) {
108 printf_err("Invalid input tensor dims\n");
109 return false;
110 }
111
112 TfLiteIntArray* inputShape = model.GetInputShape(0);
113 const uint32_t kNumRows = inputShape->data[1];
114 const uint32_t kNumCols = inputShape->data[2];
115
116 audio::AdMelSpectrogram melSpec = audio::AdMelSpectrogram(frameLength);
117 melSpec.Init();
118
119 /* Deduce the data length required for 1 inference from the network parameters. */
120 const uint8_t inputResizeScale = 2;
121 const uint32_t audioDataWindowSize = (((inputResizeScale * kNumCols) - 1) * frameStride) + frameLength;
122
123 /* We are choosing to move by 20 frames across the audio for each inference. */
124 const uint8_t nMelSpecVectorsInAudioStride = 20;
125
126 auto audioDataStride = nMelSpecVectorsInAudioStride * frameStride;
127
128 do {
129 auto currentIndex = ctx.Get<uint32_t>("clipIndex");
130
131 /* Get the output index to look at based on id in the filename. */
132 int8_t machineOutputIndex = OutputIndexFromFileName(get_filename(currentIndex));
133 if (machineOutputIndex == -1) {
134 return false;
135 }
136
137 /* Creating a Mel Spectrogram sliding window for the data required for 1 inference.
138 * "resizing" done here by multiplying stride by resize scale. */
139 auto audioMelSpecWindowSlider = audio::SlidingWindow<const int16_t>(
140 get_audio_array(currentIndex),
141 audioDataWindowSize, frameLength,
142 frameStride * inputResizeScale);
143
144 /* Creating a sliding window through the whole audio clip. */
145 auto audioDataSlider = audio::SlidingWindow<const int16_t>(
146 get_audio_array(currentIndex),
147 get_audio_array_size(currentIndex),
148 audioDataWindowSize, audioDataStride);
149
150 /* Calculate number of the feature vectors in the window overlap region taking into account resizing.
151 * These feature vectors will be reused.*/
152 auto numberOfReusedFeatureVectors = kNumRows - (nMelSpecVectorsInAudioStride / inputResizeScale);
153
154 /* Construct feature calculation function. */
155 auto melSpecFeatureCalc = GetFeatureCalculator(melSpec, inputTensor,
156 numberOfReusedFeatureVectors, trainingMean);
157 if (!melSpecFeatureCalc){
158 return false;
159 }
160
161 /* Result is an averaged sum over inferences. */
162 float result = 0;
163
164 /* Display message on the LCD - inference running. */
165 std::string str_inf{"Running inference... "};
166 platform.data_psn->present_data_text(
167 str_inf.c_str(), str_inf.size(),
168 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
169 info("Running inference on audio clip %u => %s\n", currentIndex, get_filename(currentIndex));
170
171 /* Start sliding through audio clip. */
172 while (audioDataSlider.HasNext()) {
173 const int16_t *inferenceWindow = audioDataSlider.Next();
174
175 /* We moved to the next window - set the features sliding to the new address. */
176 audioMelSpecWindowSlider.Reset(inferenceWindow);
177
178 /* The first window does not have cache ready. */
179 bool useCache = audioDataSlider.Index() > 0 && numberOfReusedFeatureVectors > 0;
180
181 /* Start calculating features inside one audio sliding window. */
182 while (audioMelSpecWindowSlider.HasNext()) {
183 const int16_t *melSpecWindow = audioMelSpecWindowSlider.Next();
184 std::vector<int16_t> melSpecAudioData = std::vector<int16_t>(melSpecWindow,
185 melSpecWindow + frameLength);
186
187 /* Compute features for this window and write them to input tensor. */
188 melSpecFeatureCalc(melSpecAudioData, audioMelSpecWindowSlider.Index(),
189 useCache, nMelSpecVectorsInAudioStride, inputResizeScale);
190 }
191
192 info("Inference %zu/%zu\n", audioDataSlider.Index() + 1,
193 audioDataSlider.TotalStrides() + 1);
194
195 /* Run inference over this audio clip sliding window */
196 arm::app::RunInference(platform, model);
197
198 /* Use the negative softmax score of the corresponding index as the outlier score */
199 std::vector<float> dequantOutput = Dequantize<int8_t>(outputTensor);
200 Softmax(dequantOutput);
201 result += -dequantOutput[machineOutputIndex];
202
203#if VERIFY_TEST_OUTPUT
204 arm::app::DumpTensor(outputTensor);
205#endif /* VERIFY_TEST_OUTPUT */
206 } /* while (audioDataSlider.HasNext()) */
207
208 /* Use average over whole clip as final score. */
209 result /= (audioDataSlider.TotalStrides() + 1);
210
211 /* Erase. */
212 str_inf = std::string(str_inf.size(), ' ');
213 platform.data_psn->present_data_text(
214 str_inf.c_str(), str_inf.size(),
215 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
216
217 ctx.Set<float>("result", result);
218 if (!_PresentInferenceResult(platform, result, scoreThreshold)) {
219 return false;
220 }
221
222 _IncrementAppCtxClipIdx(ctx);
223
224 } while (runAll && ctx.Get<uint32_t>("clipIndex") != startClipIdx);
225
226 return true;
227 }
228
229 static void _IncrementAppCtxClipIdx(ApplicationContext& ctx)
230 {
231 auto curAudioIdx = ctx.Get<uint32_t>("clipIndex");
232
233 if (curAudioIdx + 1 >= NUMBER_OF_FILES) {
234 ctx.Set<uint32_t>("clipIndex", 0);
235 return;
236 }
237 ++curAudioIdx;
238 ctx.Set<uint32_t>("clipIndex", curAudioIdx);
239 }
240
241 static bool _SetAppCtxClipIdx(ApplicationContext& ctx, const uint32_t idx)
242 {
243 if (idx >= NUMBER_OF_FILES) {
244 printf_err("Invalid idx %u (expected less than %u)\n",
245 idx, NUMBER_OF_FILES);
246 return false;
247 }
248 ctx.Set<uint32_t>("clipIndex", idx);
249 return true;
250 }
251
252 static bool _PresentInferenceResult(hal_platform& platform, float result, float threshold)
253 {
254 constexpr uint32_t dataPsnTxtStartX1 = 20;
255 constexpr uint32_t dataPsnTxtStartY1 = 30;
256 constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment */
257
258 platform.data_psn->set_text_color(COLOR_GREEN);
259
260 /* Display each result */
261 uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr;
262
263 std::string resultStr = std::string{"Average anomaly score is: "} + std::to_string(result) +
264 std::string("\n") + std::string("Anomaly threshold is: ") + std::to_string(threshold) +
265 std::string("\n");
266
267 if (result > threshold) {
268 resultStr += std::string("Anomaly detected!");
269 } else {
270 resultStr += std::string("Everything fine, no anomaly detected!");
271 }
272
273 platform.data_psn->present_data_text(
274 resultStr.c_str(), resultStr.size(),
275 dataPsnTxtStartX1, rowIdx1, 0);
276
277 info("%s\n", resultStr.c_str());
278
279 return true;
280 }
281
282 /**
283 * @brief Generic feature calculator factory.
284 *
285 * Returns lambda function to compute features using features cache.
286 * Real features math is done by a lambda function provided as a parameter.
287 * Features are written to input tensor memory.
288 *
289 * @tparam T feature vector type.
290 * @param inputTensor model input tensor pointer.
291 * @param cacheSize number of feature vectors to cache. Defined by the sliding window overlap.
292 * @param compute features calculator function.
293 * @return lambda function to compute features.
294 */
295 template<class T>
296 std::function<void (std::vector<int16_t>&, size_t, bool, size_t, size_t)>
297 _FeatureCalc(TfLiteTensor* inputTensor, size_t cacheSize,
298 std::function<std::vector<T> (std::vector<int16_t>& )> compute)
299 {
300 /* Feature cache to be captured by lambda function*/
301 static std::vector<std::vector<T>> featureCache = std::vector<std::vector<T>>(cacheSize);
302
303 return [=](std::vector<int16_t>& audioDataWindow,
304 size_t index,
305 bool useCache,
306 size_t featuresOverlapIndex,
307 size_t resizeScale)
308 {
309 T *tensorData = tflite::GetTensorData<T>(inputTensor);
310 std::vector<T> features;
311
312 /* Reuse features from cache if cache is ready and sliding windows overlap.
313 * Overlap is in the beginning of sliding window with a size of a feature cache. */
314 if (useCache && index < featureCache.size()) {
315 features = std::move(featureCache[index]);
316 } else {
317 features = std::move(compute(audioDataWindow));
318 }
319 auto size = features.size() / resizeScale;
320 auto sizeBytes = sizeof(T);
321
322 /* Input should be transposed and "resized" by skipping elements. */
323 for (size_t outIndex = 0; outIndex < size; outIndex++) {
324 std::memcpy(tensorData + (outIndex*size) + index, &features[outIndex*resizeScale], sizeBytes);
325 }
326
327 /* Start renewing cache as soon iteration goes out of the windows overlap. */
328 if (index >= featuresOverlapIndex / resizeScale) {
329 featureCache[index - featuresOverlapIndex / resizeScale] = std::move(features);
330 }
331 };
332 }
333
334 template std::function<void (std::vector<int16_t>&, size_t , bool, size_t, size_t)>
335 _FeatureCalc<int8_t>(TfLiteTensor* inputTensor,
336 size_t cacheSize,
337 std::function<std::vector<int8_t> (std::vector<int16_t>&)> compute);
338
339 template std::function<void (std::vector<int16_t>&, size_t , bool, size_t, size_t)>
340 _FeatureCalc<uint8_t>(TfLiteTensor* inputTensor,
341 size_t cacheSize,
342 std::function<std::vector<uint8_t> (std::vector<int16_t>&)> compute);
343
344 template std::function<void (std::vector<int16_t>&, size_t , bool, size_t, size_t)>
345 _FeatureCalc<int16_t>(TfLiteTensor* inputTensor,
346 size_t cacheSize,
347 std::function<std::vector<int16_t> (std::vector<int16_t>&)> compute);
348
349 template std::function<void(std::vector<int16_t>&, size_t, bool, size_t, size_t)>
350 _FeatureCalc<float>(TfLiteTensor *inputTensor,
351 size_t cacheSize,
352 std::function<std::vector<float>(std::vector<int16_t>&)> compute);
353
354
355 static std::function<void (std::vector<int16_t>&, int, bool, size_t, size_t)>
356 GetFeatureCalculator(audio::AdMelSpectrogram& melSpec, TfLiteTensor* inputTensor, size_t cacheSize, float trainingMean)
357 {
358 std::function<void (std::vector<int16_t>&, size_t, bool, size_t, size_t)> melSpecFeatureCalc;
359
360 TfLiteQuantization quant = inputTensor->quantization;
361
362 if (kTfLiteAffineQuantization == quant.type) {
363
364 auto *quantParams = (TfLiteAffineQuantization *) quant.params;
365 const float quantScale = quantParams->scale->data[0];
366 const int quantOffset = quantParams->zero_point->data[0];
367
368 switch (inputTensor->type) {
369 case kTfLiteInt8: {
370 melSpecFeatureCalc = _FeatureCalc<int8_t>(inputTensor,
371 cacheSize,
372 [=, &melSpec](std::vector<int16_t>& audioDataWindow) {
373 return melSpec.MelSpecComputeQuant<int8_t>(audioDataWindow,
374 quantScale,
375 quantOffset,
376 trainingMean);
377 }
378 );
379 break;
380 }
381 case kTfLiteUInt8: {
382 melSpecFeatureCalc = _FeatureCalc<uint8_t>(inputTensor,
383 cacheSize,
384 [=, &melSpec](std::vector<int16_t>& audioDataWindow) {
385 return melSpec.MelSpecComputeQuant<uint8_t>(audioDataWindow,
386 quantScale,
387 quantOffset,
388 trainingMean);
389 }
390 );
391 break;
392 }
393 case kTfLiteInt16: {
394 melSpecFeatureCalc = _FeatureCalc<int16_t>(inputTensor,
395 cacheSize,
396 [=, &melSpec](std::vector<int16_t>& audioDataWindow) {
397 return melSpec.MelSpecComputeQuant<int16_t>(audioDataWindow,
398 quantScale,
399 quantOffset,
400 trainingMean);
401 }
402 );
403 break;
404 }
405 default:
406 printf_err("Tensor type %s not supported\n", TfLiteTypeGetName(inputTensor->type));
407 }
408
409
410 } else {
411 melSpecFeatureCalc = melSpecFeatureCalc = _FeatureCalc<float>(inputTensor,
412 cacheSize,
413 [=, &melSpec](std::vector<int16_t>& audioDataWindow) {
414 return melSpec.ComputeMelSpec(audioDataWindow,
415 trainingMean);
416 });
417 }
418 return melSpecFeatureCalc;
419 }
420
421} /* namespace app */
422} /* namespace arm */