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" |
| 20 | #include "Classifier.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 | |
| 32 | using KwsClassifier = arm::app::Classifier; |
| 33 | |
| 34 | namespace arm { |
| 35 | namespace app { |
| 36 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 37 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 38 | /** |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 39 | * @brief Presents KWS inference results. |
| 40 | * @param[in] results Vector of KWS classification results to be displayed. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 41 | * @return true if successful, false otherwise. |
| 42 | **/ |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 43 | static bool PresentInferenceResult(const std::vector<arm::app::kws::KwsResult>& results); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 44 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 45 | /* KWS inference handler. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 46 | bool ClassifyAudioHandler(ApplicationContext& ctx, uint32_t clipIndex, bool runAll) |
| 47 | { |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 48 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 49 | auto& model = ctx.Get<Model&>("model"); |
| 50 | const auto mfccFrameLength = ctx.Get<int>("frameLength"); |
| 51 | const auto mfccFrameStride = ctx.Get<int>("frameStride"); |
| 52 | const auto scoreThreshold = ctx.Get<float>("scoreThreshold"); |
| 53 | /* If the request has a valid size, set the audio index. */ |
| 54 | if (clipIndex < NUMBER_OF_FILES) { |
| 55 | if (!SetAppCtxIfmIdx(ctx, clipIndex,"clipIndex")) { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | auto initialClipIdx = ctx.Get<uint32_t>("clipIndex"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 60 | |
| 61 | constexpr uint32_t dataPsnTxtInfStartX = 20; |
| 62 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
| 63 | constexpr int minTensorDims = static_cast<int>( |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 64 | (arm::app::MicroNetKwsModel::ms_inputRowsIdx > arm::app::MicroNetKwsModel::ms_inputColsIdx)? |
| 65 | arm::app::MicroNetKwsModel::ms_inputRowsIdx : arm::app::MicroNetKwsModel::ms_inputColsIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 66 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 68 | if (!model.IsInited()) { |
| 69 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 70 | return false; |
| 71 | } |
| 72 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 73 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 74 | if (!inputTensor->dims) { |
| 75 | printf_err("Invalid input tensor dims\n"); |
| 76 | return false; |
| 77 | } else if (inputTensor->dims->size < minTensorDims) { |
| 78 | printf_err("Input tensor dimension should be >= %d\n", minTensorDims); |
| 79 | return false; |
| 80 | } |
| 81 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 82 | /* Get input shape for feature extraction. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 83 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 84 | const uint32_t numMfccFeatures = inputShape->data[arm::app::MicroNetKwsModel::ms_inputColsIdx]; |
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. */ |
| 91 | KWSPreProcess preprocess = KWSPreProcess(&model, numMfccFeatures, mfccFrameLength, mfccFrameStride); |
| 92 | |
| 93 | std::vector<ClassificationResult> singleInfResult; |
| 94 | KWSPostProcess postprocess = KWSPostProcess(ctx.Get<KwsClassifier &>("classifier"), &model, |
| 95 | ctx.Get<std::vector<std::string>&>("labels"), |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame^] | 96 | singleInfResult); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 97 | |
| 98 | UseCaseRunner runner = UseCaseRunner(&preprocess, &postprocess, &model); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 99 | |
| 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), |
| 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 | |
| 125 | /* The first window does not have cache ready. */ |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 126 | preprocess.m_audioWindowIndex = audioDataSlider.Index(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 127 | |
| 128 | info("Inference %zu/%zu\n", audioDataSlider.Index() + 1, |
| 129 | audioDataSlider.TotalStrides() + 1); |
| 130 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 131 | /* Run the pre-processing, inference and post-processing. */ |
| 132 | if (!runner.PreProcess(inferenceWindow, audio::MicroNetKwsMFCC::ms_defaultSamplingFreq)) { |
alexander | 27b62d9 | 2021-05-04 20:46:08 +0100 | [diff] [blame] | 133 | return false; |
| 134 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 135 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 136 | profiler.StartProfiling("Inference"); |
| 137 | if (!runner.RunInference()) { |
| 138 | return false; |
| 139 | } |
| 140 | profiler.StopProfiling(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 141 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 142 | if (!runner.PostProcess()) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | /* Add results from this window to our final results vector. */ |
| 147 | finalResults.emplace_back(kws::KwsResult(singleInfResult, |
| 148 | audioDataSlider.Index() * secondsPerSample * preprocess.m_audioDataStride, |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame^] | 149 | audioDataSlider.Index(), scoreThreshold)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 150 | |
| 151 | #if VERIFY_TEST_OUTPUT |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 152 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 153 | arm::app::DumpTensor(outputTensor); |
| 154 | #endif /* VERIFY_TEST_OUTPUT */ |
| 155 | } /* while (audioDataSlider.HasNext()) */ |
| 156 | |
| 157 | /* Erase. */ |
| 158 | str_inf = std::string(str_inf.size(), ' '); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 159 | hal_lcd_display_text(str_inf.c_str(), str_inf.size(), |
| 160 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 161 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 162 | ctx.Set<std::vector<kws::KwsResult>>("results", finalResults); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 163 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 164 | if (!PresentInferenceResult(finalResults)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 165 | return false; |
| 166 | } |
| 167 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 168 | profiler.PrintProfilingResult(); |
| 169 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 170 | IncrementAppCtxIfmIdx(ctx,"clipIndex"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 171 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 172 | } while (runAll && ctx.Get<uint32_t>("clipIndex") != initialClipIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 177 | static bool PresentInferenceResult(const std::vector<arm::app::kws::KwsResult>& results) |
| 178 | { |
| 179 | constexpr uint32_t dataPsnTxtStartX1 = 20; |
| 180 | constexpr uint32_t dataPsnTxtStartY1 = 30; |
| 181 | constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment. */ |
| 182 | |
| 183 | hal_lcd_set_text_color(COLOR_GREEN); |
| 184 | info("Final results:\n"); |
| 185 | info("Total number of inferences: %zu\n", results.size()); |
| 186 | |
| 187 | /* Display each result */ |
| 188 | uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr; |
| 189 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 190 | for (const auto & result : results) { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 191 | |
| 192 | std::string topKeyword{"<none>"}; |
| 193 | float score = 0.f; |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 194 | if (!result.m_resultVec.empty()) { |
| 195 | topKeyword = result.m_resultVec[0].m_label; |
| 196 | score = result.m_resultVec[0].m_normalisedVal; |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | std::string resultStr = |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 200 | std::string{"@"} + std::to_string(result.m_timeStamp) + |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 201 | std::string{"s: "} + topKeyword + std::string{" ("} + |
| 202 | std::to_string(static_cast<int>(score * 100)) + std::string{"%)"}; |
| 203 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 204 | hal_lcd_display_text(resultStr.c_str(), resultStr.size(), |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 205 | dataPsnTxtStartX1, rowIdx1, false); |
| 206 | rowIdx1 += dataPsnTxtYIncr; |
| 207 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 208 | if (result.m_resultVec.empty()) { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 209 | info("For timestamp: %f (inference #: %" PRIu32 |
| 210 | "); label: %s; threshold: %f\n", |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 211 | result.m_timeStamp, result.m_inferenceNumber, |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 212 | topKeyword.c_str(), |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 213 | result.m_threshold); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 214 | } else { |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 215 | for (uint32_t j = 0; j < result.m_resultVec.size(); ++j) { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 216 | info("For timestamp: %f (inference #: %" PRIu32 |
| 217 | "); label: %s, score: %f; threshold: %f\n", |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 218 | result.m_timeStamp, |
| 219 | result.m_inferenceNumber, |
| 220 | result.m_resultVec[j].m_label.c_str(), |
| 221 | result.m_resultVec[j].m_normalisedVal, |
| 222 | result.m_threshold); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 230 | } /* namespace app */ |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 231 | } /* namespace arm */ |