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