alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 "InputFiles.hpp" |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 20 | #include "KwsClassifier.hpp" |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 21 | #include "MicroNetKwsModel.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 22 | #include "hal.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 23 | #include "AudioUtils.hpp" |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 24 | #include "ImageUtils.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 25 | #include "UseCaseCommonUtils.hpp" |
| 26 | #include "KwsResult.hpp" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 27 | #include "log_macros.h" |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 28 | #include "KwsProcessing.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | |
| 30 | #include <vector> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 31 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 32 | namespace arm { |
| 33 | namespace app { |
| 34 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 35 | /** |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 36 | * @brief Presents KWS inference results. |
| 37 | * @param[in] results Vector of KWS classification results to be displayed. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 38 | * @return true if successful, false otherwise. |
| 39 | **/ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 40 | static bool PresentInferenceResult(const std::vector<kws::KwsResult>& results); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 41 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 42 | /* KWS inference handler. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 43 | bool ClassifyAudioHandler(ApplicationContext& ctx, uint32_t clipIndex, bool runAll) |
| 44 | { |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 45 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 46 | auto& model = ctx.Get<Model&>("model"); |
| 47 | const auto mfccFrameLength = ctx.Get<int>("frameLength"); |
| 48 | const auto mfccFrameStride = ctx.Get<int>("frameStride"); |
| 49 | const auto scoreThreshold = ctx.Get<float>("scoreThreshold"); |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 50 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 51 | /* If the request has a valid size, set the audio index. */ |
| 52 | if (clipIndex < NUMBER_OF_FILES) { |
| 53 | if (!SetAppCtxIfmIdx(ctx, clipIndex,"clipIndex")) { |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | auto initialClipIdx = ctx.Get<uint32_t>("clipIndex"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 58 | |
| 59 | constexpr uint32_t dataPsnTxtInfStartX = 20; |
| 60 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
| 61 | constexpr int minTensorDims = static_cast<int>( |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 62 | (MicroNetKwsModel::ms_inputRowsIdx > MicroNetKwsModel::ms_inputColsIdx)? |
| 63 | MicroNetKwsModel::ms_inputRowsIdx : MicroNetKwsModel::ms_inputColsIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 64 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 65 | if (!model.IsInited()) { |
| 66 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 67 | return false; |
| 68 | } |
| 69 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 70 | /* Get Input and Output tensors for pre/post processing. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 72 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 73 | if (!inputTensor->dims) { |
| 74 | printf_err("Invalid input tensor dims\n"); |
| 75 | return false; |
| 76 | } else if (inputTensor->dims->size < minTensorDims) { |
| 77 | printf_err("Input tensor dimension should be >= %d\n", minTensorDims); |
| 78 | return false; |
| 79 | } |
| 80 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 81 | /* Get input shape for feature extraction. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 82 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 83 | const uint32_t numMfccFeatures = inputShape->data[MicroNetKwsModel::ms_inputColsIdx]; |
| 84 | const uint32_t numMfccFrames = inputShape->data[arm::app::MicroNetKwsModel::ms_inputRowsIdx]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 85 | |
| 86 | /* We expect to be sampling 1 second worth of data at a time. |
| 87 | * NOTE: This is only used for time stamp calculation. */ |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 88 | const float secondsPerSample = 1.0 / audio::MicroNetKwsMFCC::ms_defaultSamplingFreq; |
| 89 | |
| 90 | /* Set up pre and post-processing. */ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 91 | KwsPreProcess preProcess = KwsPreProcess(inputTensor, numMfccFeatures, numMfccFrames, |
| 92 | mfccFrameLength, mfccFrameStride); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 93 | |
| 94 | std::vector<ClassificationResult> singleInfResult; |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 95 | KwsPostProcess postProcess = KwsPostProcess(outputTensor, ctx.Get<KwsClassifier &>("classifier"), |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 96 | ctx.Get<std::vector<std::string>&>("labels"), |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 97 | singleInfResult); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 98 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 99 | /* Loop to process audio clips. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 100 | do { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 101 | hal_lcd_clear(COLOR_BLACK); |
Richard Burton | 9b8d67a | 2021-12-10 12:32:51 +0000 | [diff] [blame] | 102 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 103 | auto currentIndex = ctx.Get<uint32_t>("clipIndex"); |
| 104 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 105 | /* Creating a sliding window through the whole audio clip. */ |
| 106 | auto audioDataSlider = audio::SlidingWindow<const int16_t>( |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 107 | get_audio_array(currentIndex), |
| 108 | get_audio_array_size(currentIndex), |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 109 | preProcess.m_audioDataWindowSize, preProcess.m_audioDataStride); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 110 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 111 | /* Declare a container to hold results from across the whole audio clip. */ |
| 112 | std::vector<kws::KwsResult> finalResults; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 113 | |
| 114 | /* Display message on the LCD - inference running. */ |
| 115 | std::string str_inf{"Running inference... "}; |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 116 | hal_lcd_display_text(str_inf.c_str(), str_inf.size(), |
| 117 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 118 | info("Running inference on audio clip %" PRIu32 " => %s\n", currentIndex, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 119 | get_filename(currentIndex)); |
| 120 | |
| 121 | /* Start sliding through audio clip. */ |
| 122 | while (audioDataSlider.HasNext()) { |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 123 | const int16_t* inferenceWindow = audioDataSlider.Next(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 124 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 125 | info("Inference %zu/%zu\n", audioDataSlider.Index() + 1, |
| 126 | audioDataSlider.TotalStrides() + 1); |
| 127 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 128 | /* Run the pre-processing, inference and post-processing. */ |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 129 | if (!preProcess.DoPreProcess(inferenceWindow, audioDataSlider.Index())) { |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 130 | printf_err("Pre-processing failed."); |
alexander | 27b62d9 | 2021-05-04 20:46:08 +0100 | [diff] [blame] | 131 | return false; |
| 132 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 133 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 134 | if (!RunInference(model, profiler)) { |
| 135 | printf_err("Inference failed."); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 136 | return false; |
| 137 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 138 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 139 | if (!postProcess.DoPostProcess()) { |
| 140 | printf_err("Post-processing failed."); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | |
| 144 | /* Add results from this window to our final results vector. */ |
| 145 | finalResults.emplace_back(kws::KwsResult(singleInfResult, |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 146 | audioDataSlider.Index() * secondsPerSample * preProcess.m_audioDataStride, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 147 | audioDataSlider.Index(), scoreThreshold)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 148 | |
| 149 | #if VERIFY_TEST_OUTPUT |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 150 | DumpTensor(outputTensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 151 | #endif /* VERIFY_TEST_OUTPUT */ |
| 152 | } /* while (audioDataSlider.HasNext()) */ |
| 153 | |
| 154 | /* Erase. */ |
| 155 | str_inf = std::string(str_inf.size(), ' '); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 156 | hal_lcd_display_text(str_inf.c_str(), str_inf.size(), |
| 157 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 158 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 159 | ctx.Set<std::vector<kws::KwsResult>>("results", finalResults); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 160 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 161 | if (!PresentInferenceResult(finalResults)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 165 | profiler.PrintProfilingResult(); |
| 166 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 167 | IncrementAppCtxIfmIdx(ctx,"clipIndex"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 168 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 169 | } while (runAll && ctx.Get<uint32_t>("clipIndex") != initialClipIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 174 | static bool PresentInferenceResult(const std::vector<kws::KwsResult>& results) |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 175 | { |
| 176 | constexpr uint32_t dataPsnTxtStartX1 = 20; |
| 177 | constexpr uint32_t dataPsnTxtStartY1 = 30; |
| 178 | constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment. */ |
| 179 | |
| 180 | hal_lcd_set_text_color(COLOR_GREEN); |
| 181 | info("Final results:\n"); |
| 182 | info("Total number of inferences: %zu\n", results.size()); |
| 183 | |
| 184 | /* Display each result */ |
| 185 | uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr; |
| 186 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 187 | for (const auto& result : results) { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 188 | |
| 189 | std::string topKeyword{"<none>"}; |
| 190 | float score = 0.f; |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 191 | if (!result.m_resultVec.empty()) { |
| 192 | topKeyword = result.m_resultVec[0].m_label; |
| 193 | score = result.m_resultVec[0].m_normalisedVal; |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | std::string resultStr = |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 197 | std::string{"@"} + std::to_string(result.m_timeStamp) + |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 198 | std::string{"s: "} + topKeyword + std::string{" ("} + |
| 199 | std::to_string(static_cast<int>(score * 100)) + std::string{"%)"}; |
| 200 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 201 | hal_lcd_display_text(resultStr.c_str(), resultStr.size(), |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 202 | dataPsnTxtStartX1, rowIdx1, false); |
| 203 | rowIdx1 += dataPsnTxtYIncr; |
| 204 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 205 | if (result.m_resultVec.empty()) { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 206 | info("For timestamp: %f (inference #: %" PRIu32 |
| 207 | "); label: %s; threshold: %f\n", |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 208 | result.m_timeStamp, result.m_inferenceNumber, |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 209 | topKeyword.c_str(), |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 210 | result.m_threshold); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 211 | } else { |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 212 | for (uint32_t j = 0; j < result.m_resultVec.size(); ++j) { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 213 | info("For timestamp: %f (inference #: %" PRIu32 |
| 214 | "); label: %s, score: %f; threshold: %f\n", |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 215 | result.m_timeStamp, |
| 216 | result.m_inferenceNumber, |
| 217 | result.m_resultVec[j].m_label.c_str(), |
| 218 | result.m_resultVec[j].m_normalisedVal, |
| 219 | result.m_threshold); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 227 | } /* namespace app */ |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 228 | } /* namespace arm */ |