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