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 "AsrClassifier.hpp" |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 20 | #include "AsrResult.hpp" |
| 21 | #include "AudioUtils.hpp" |
| 22 | #include "Classifier.hpp" |
| 23 | #include "ImageUtils.hpp" |
| 24 | #include "InputFiles.hpp" |
| 25 | #include "KwsProcessing.hpp" |
| 26 | #include "KwsResult.hpp" |
| 27 | #include "MicroNetKwsMfcc.hpp" |
| 28 | #include "MicroNetKwsModel.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | #include "OutputDecode.hpp" |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 30 | #include "UseCaseCommonUtils.hpp" |
| 31 | #include "Wav2LetterMfcc.hpp" |
| 32 | #include "Wav2LetterModel.hpp" |
| 33 | #include "Wav2LetterPostprocess.hpp" |
| 34 | #include "Wav2LetterPreprocess.hpp" |
| 35 | #include "hal.h" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 36 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 37 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 38 | using KwsClassifier = arm::app::Classifier; |
| 39 | |
| 40 | namespace arm { |
| 41 | namespace app { |
| 42 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 43 | struct KWSOutput { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 44 | bool executionSuccess = false; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | const int16_t* asrAudioStart = nullptr; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 46 | int32_t asrAudioSamples = 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 50 | * @brief Presents KWS inference results. |
| 51 | * @param[in] results Vector of KWS classification results to be displayed. |
| 52 | * @return true if successful, false otherwise. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 53 | **/ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 54 | static bool PresentInferenceResult(std::vector<kws::KwsResult>& results); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 55 | |
| 56 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 57 | * @brief Presents ASR inference results. |
| 58 | * @param[in] results Vector of ASR classification results to be displayed. |
| 59 | * @return true if successful, false otherwise. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 60 | **/ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 61 | static bool PresentInferenceResult(std::vector<asr::AsrResult>& results); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 64 | * @brief Performs the KWS pipeline. |
| 65 | * @param[in,out] ctx pointer to the application context object |
| 66 | * @return struct containing pointer to audio data where ASR should begin |
| 67 | * and how much data to process. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 68 | **/ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 69 | static KWSOutput doKws(ApplicationContext& ctx) |
| 70 | { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 71 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
| 72 | auto& kwsModel = ctx.Get<Model&>("kwsModel"); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 73 | const auto kwsMfccFrameLength = ctx.Get<int>("kwsFrameLength"); |
| 74 | const auto kwsMfccFrameStride = ctx.Get<int>("kwsFrameStride"); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 75 | const auto kwsScoreThreshold = ctx.Get<float>("kwsScoreThreshold"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 76 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 77 | auto currentIndex = ctx.Get<uint32_t>("clipIndex"); |
| 78 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 79 | constexpr uint32_t dataPsnTxtInfStartX = 20; |
| 80 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
| 81 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 82 | constexpr int minTensorDims = |
| 83 | static_cast<int>((MicroNetKwsModel::ms_inputRowsIdx > MicroNetKwsModel::ms_inputColsIdx) |
| 84 | ? MicroNetKwsModel::ms_inputRowsIdx |
| 85 | : MicroNetKwsModel::ms_inputColsIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 87 | /* Output struct from doing KWS. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 88 | KWSOutput output{}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 89 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 90 | if (!kwsModel.IsInited()) { |
| 91 | printf_err("KWS model has not been initialised\n"); |
| 92 | return output; |
| 93 | } |
| 94 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 95 | /* Get Input and Output tensors for pre/post processing. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 96 | TfLiteTensor* kwsInputTensor = kwsModel.GetInputTensor(0); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 97 | TfLiteTensor* kwsOutputTensor = kwsModel.GetOutputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 98 | if (!kwsInputTensor->dims) { |
| 99 | printf_err("Invalid input tensor dims\n"); |
| 100 | return output; |
| 101 | } else if (kwsInputTensor->dims->size < minTensorDims) { |
| 102 | printf_err("Input tensor dimension should be >= %d\n", minTensorDims); |
| 103 | return output; |
| 104 | } |
| 105 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 106 | /* Get input shape for feature extraction. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 107 | TfLiteIntArray* inputShape = kwsModel.GetInputShape(0); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 108 | const uint32_t numMfccFeatures = inputShape->data[MicroNetKwsModel::ms_inputColsIdx]; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 109 | const uint32_t numMfccFrames = inputShape->data[MicroNetKwsModel::ms_inputRowsIdx]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 110 | |
| 111 | /* We expect to be sampling 1 second worth of data at a time |
| 112 | * NOTE: This is only used for time stamp calculation. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 113 | const float kwsAudioParamsSecondsPerSample = |
| 114 | 1.0 / audio::MicroNetKwsMFCC::ms_defaultSamplingFreq; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 115 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 116 | /* Set up pre and post-processing. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 117 | KwsPreProcess preProcess = KwsPreProcess( |
| 118 | kwsInputTensor, numMfccFeatures, numMfccFrames, kwsMfccFrameLength, kwsMfccFrameStride); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 119 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 120 | std::vector<ClassificationResult> singleInfResult; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 121 | KwsPostProcess postProcess = KwsPostProcess(kwsOutputTensor, |
| 122 | ctx.Get<KwsClassifier&>("kwsClassifier"), |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 123 | ctx.Get<std::vector<std::string>&>("kwsLabels"), |
| 124 | singleInfResult); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 125 | |
| 126 | /* Creating a sliding window through the whole audio clip. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 127 | auto audioDataSlider = audio::SlidingWindow<const int16_t>(GetAudioArray(currentIndex), |
| 128 | GetAudioArraySize(currentIndex), |
| 129 | preProcess.m_audioDataWindowSize, |
| 130 | preProcess.m_audioDataStride); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 131 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 132 | /* Declare a container to hold kws results from across the whole audio clip. */ |
| 133 | std::vector<kws::KwsResult> finalResults; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 134 | |
| 135 | /* Display message on the LCD - inference running. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 136 | std::string str_inf{"Running KWS inference... "}; |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 137 | hal_lcd_display_text( |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 138 | str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 139 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 140 | info("Running KWS inference on audio clip %" PRIu32 " => %s\n", |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 141 | currentIndex, |
| 142 | GetFilename(currentIndex)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 143 | |
| 144 | /* Start sliding through audio clip. */ |
| 145 | while (audioDataSlider.HasNext()) { |
| 146 | const int16_t* inferenceWindow = audioDataSlider.Next(); |
| 147 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 148 | /* Run the pre-processing, inference and post-processing. */ |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 149 | if (!preProcess.DoPreProcess(inferenceWindow, audioDataSlider.Index())) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 150 | printf_err("KWS Pre-processing failed."); |
| 151 | return output; |
| 152 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 153 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 154 | if (!RunInference(kwsModel, profiler)) { |
| 155 | printf_err("KWS Inference failed."); |
| 156 | return output; |
| 157 | } |
| 158 | |
| 159 | if (!postProcess.DoPostProcess()) { |
| 160 | printf_err("KWS Post-processing failed."); |
| 161 | return output; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 164 | info("Inference %zu/%zu\n", |
| 165 | audioDataSlider.Index() + 1, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 166 | audioDataSlider.TotalStrides() + 1); |
| 167 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 168 | /* Add results from this window to our final results vector. */ |
| 169 | finalResults.emplace_back( |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 170 | kws::KwsResult(singleInfResult, |
| 171 | audioDataSlider.Index() * kwsAudioParamsSecondsPerSample * |
| 172 | preProcess.m_audioDataStride, |
| 173 | audioDataSlider.Index(), |
| 174 | kwsScoreThreshold)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 175 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 176 | /* Break out when trigger keyword is detected. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 177 | if (singleInfResult[0].m_label == ctx.Get<const std::string&>("triggerKeyword") && |
| 178 | singleInfResult[0].m_normalisedVal > kwsScoreThreshold) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 179 | output.asrAudioStart = inferenceWindow + preProcess.m_audioDataWindowSize; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 180 | output.asrAudioSamples = |
| 181 | GetAudioArraySize(currentIndex) - |
| 182 | (audioDataSlider.NextWindowStartIndex() - preProcess.m_audioDataStride + |
| 183 | preProcess.m_audioDataWindowSize); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | |
| 187 | #if VERIFY_TEST_OUTPUT |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 188 | DumpTensor(kwsOutputTensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 189 | #endif /* VERIFY_TEST_OUTPUT */ |
| 190 | |
| 191 | } /* while (audioDataSlider.HasNext()) */ |
| 192 | |
| 193 | /* Erase. */ |
| 194 | str_inf = std::string(str_inf.size(), ' '); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 195 | hal_lcd_display_text( |
| 196 | str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 197 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 198 | if (!PresentInferenceResult(finalResults)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 199 | return output; |
| 200 | } |
| 201 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 202 | profiler.PrintProfilingResult(); |
| 203 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 204 | output.executionSuccess = true; |
| 205 | return output; |
| 206 | } |
| 207 | |
| 208 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 209 | * @brief Performs the ASR pipeline. |
| 210 | * @param[in,out] ctx Pointer to the application context object. |
| 211 | * @param[in] kwsOutput Struct containing pointer to audio data where ASR should begin |
| 212 | * and how much data to process. |
| 213 | * @return true if pipeline executed without failure. |
| 214 | **/ |
| 215 | static bool doAsr(ApplicationContext& ctx, const KWSOutput& kwsOutput) |
| 216 | { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 217 | auto& asrModel = ctx.Get<Model&>("asrModel"); |
| 218 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
| 219 | auto asrMfccFrameLen = ctx.Get<uint32_t>("asrFrameLength"); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 220 | auto asrMfccFrameStride = ctx.Get<uint32_t>("asrFrameStride"); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 221 | auto asrScoreThreshold = ctx.Get<float>("asrScoreThreshold"); |
| 222 | auto asrInputCtxLen = ctx.Get<uint32_t>("ctxLen"); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 223 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 224 | constexpr uint32_t dataPsnTxtInfStartX = 20; |
| 225 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
| 226 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 227 | if (!asrModel.IsInited()) { |
| 228 | printf_err("ASR model has not been initialised\n"); |
| 229 | return false; |
| 230 | } |
| 231 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 232 | hal_lcd_clear(COLOR_BLACK); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 233 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 234 | /* Get Input and Output tensors for pre/post processing. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 235 | TfLiteTensor* asrInputTensor = asrModel.GetInputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 236 | TfLiteTensor* asrOutputTensor = asrModel.GetOutputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 237 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 238 | /* Get input shape. Dimensions of the tensor should have been verified by |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 239 | * the callee. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 240 | TfLiteIntArray* inputShape = asrModel.GetInputShape(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 241 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 242 | const uint32_t asrInputRows = asrInputTensor->dims->data[Wav2LetterModel::ms_inputRowsIdx]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 243 | const uint32_t asrInputInnerLen = asrInputRows - (2 * asrInputCtxLen); |
| 244 | |
| 245 | /* Make sure the input tensor supports the above context and inner lengths. */ |
| 246 | if (asrInputRows <= 2 * asrInputCtxLen || asrInputRows <= asrInputInnerLen) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 247 | printf_err("ASR input rows not compatible with ctx length %" PRIu32 "\n", |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 248 | asrInputCtxLen); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 249 | return false; |
| 250 | } |
| 251 | |
| 252 | /* Audio data stride corresponds to inputInnerLen feature vectors. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 253 | const uint32_t asrAudioDataWindowLen = |
| 254 | (asrInputRows - 1) * asrMfccFrameStride + (asrMfccFrameLen); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 255 | const uint32_t asrAudioDataWindowStride = asrInputInnerLen * asrMfccFrameStride; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 256 | const float asrAudioParamsSecondsPerSample = |
| 257 | 1.0 / audio::Wav2LetterMFCC::ms_defaultSamplingFreq; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 258 | |
| 259 | /* Get the remaining audio buffer and respective size from KWS results. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 260 | const int16_t* audioArr = kwsOutput.asrAudioStart; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 261 | const uint32_t audioArrSize = kwsOutput.asrAudioSamples; |
| 262 | |
| 263 | /* Audio clip must have enough samples to produce 1 MFCC feature. */ |
| 264 | std::vector<int16_t> audioBuffer = std::vector<int16_t>(audioArr, audioArr + audioArrSize); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 265 | if (audioArrSize < asrMfccFrameLen) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 266 | printf_err("Not enough audio samples, minimum needed is %" PRIu32 "\n", |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 267 | asrMfccFrameLen); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 268 | return false; |
| 269 | } |
| 270 | |
| 271 | /* Initialise an audio slider. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 272 | auto audioDataSlider = |
| 273 | audio::FractionalSlidingWindow<const int16_t>(audioBuffer.data(), |
| 274 | audioBuffer.size(), |
| 275 | asrAudioDataWindowLen, |
| 276 | asrAudioDataWindowStride); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 277 | |
| 278 | /* Declare a container for results. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 279 | std::vector<asr::AsrResult> asrResults; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 280 | |
| 281 | /* Display message on the LCD - inference running. */ |
| 282 | std::string str_inf{"Running ASR inference... "}; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 283 | hal_lcd_display_text( |
| 284 | str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 285 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 286 | size_t asrInferenceWindowLen = asrAudioDataWindowLen; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 287 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 288 | /* Set up pre and post-processing objects. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 289 | AsrPreProcess asrPreProcess = |
| 290 | AsrPreProcess(asrInputTensor, |
| 291 | arm::app::Wav2LetterModel::ms_numMfccFeatures, |
| 292 | inputShape->data[Wav2LetterModel::ms_inputRowsIdx], |
| 293 | asrMfccFrameLen, |
| 294 | asrMfccFrameStride); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 295 | |
| 296 | std::vector<ClassificationResult> singleInfResult; |
| 297 | const uint32_t outputCtxLen = AsrPostProcess::GetOutputContextLen(asrModel, asrInputCtxLen); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 298 | AsrPostProcess asrPostProcess = |
| 299 | AsrPostProcess(asrOutputTensor, |
| 300 | ctx.Get<AsrClassifier&>("asrClassifier"), |
| 301 | ctx.Get<std::vector<std::string>&>("asrLabels"), |
| 302 | singleInfResult, |
| 303 | outputCtxLen, |
| 304 | Wav2LetterModel::ms_blankTokenIdx, |
| 305 | Wav2LetterModel::ms_outputRowsIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 306 | /* Start sliding through audio clip. */ |
| 307 | while (audioDataSlider.HasNext()) { |
| 308 | |
| 309 | /* If not enough audio see how much can be sent for processing. */ |
| 310 | size_t nextStartIndex = audioDataSlider.NextWindowStartIndex(); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 311 | if (nextStartIndex + asrAudioDataWindowLen > audioBuffer.size()) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 312 | asrInferenceWindowLen = audioBuffer.size() - nextStartIndex; |
| 313 | } |
| 314 | |
| 315 | const int16_t* asrInferenceWindow = audioDataSlider.Next(); |
| 316 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 317 | info("Inference %zu/%zu\n", |
| 318 | audioDataSlider.Index() + 1, |
| 319 | static_cast<size_t>(ceilf(audioDataSlider.FractionalTotalStrides() + 1))); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 320 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 321 | /* Run the pre-processing, inference and post-processing. */ |
| 322 | if (!asrPreProcess.DoPreProcess(asrInferenceWindow, asrInferenceWindowLen)) { |
| 323 | printf_err("ASR pre-processing failed."); |
| 324 | return false; |
| 325 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 326 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 327 | /* Run inference over this audio clip sliding window. */ |
alexander | 27b62d9 | 2021-05-04 20:46:08 +0100 | [diff] [blame] | 328 | if (!RunInference(asrModel, profiler)) { |
| 329 | printf_err("ASR inference failed\n"); |
| 330 | return false; |
| 331 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 332 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 333 | /* Post processing needs to know if we are on the last audio window. */ |
| 334 | asrPostProcess.m_lastIteration = !audioDataSlider.HasNext(); |
| 335 | if (!asrPostProcess.DoPostProcess()) { |
| 336 | printf_err("ASR post-processing failed."); |
| 337 | return false; |
| 338 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 339 | |
| 340 | /* Get results. */ |
| 341 | std::vector<ClassificationResult> asrClassificationResult; |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 342 | auto& asrClassifier = ctx.Get<AsrClassifier&>("asrClassifier"); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 343 | asrClassifier.GetClassificationResults(asrOutputTensor, |
| 344 | asrClassificationResult, |
| 345 | ctx.Get<std::vector<std::string>&>("asrLabels"), |
| 346 | 1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 347 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 348 | asrResults.emplace_back( |
| 349 | asr::AsrResult(asrClassificationResult, |
| 350 | (audioDataSlider.Index() * asrAudioParamsSecondsPerSample * |
| 351 | asrAudioDataWindowStride), |
| 352 | audioDataSlider.Index(), |
| 353 | asrScoreThreshold)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 354 | |
| 355 | #if VERIFY_TEST_OUTPUT |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 356 | armDumpTensor(asrOutputTensor, |
| 357 | asrOutputTensor->dims->data[Wav2LetterModel::ms_outputColsIdx]); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 358 | #endif /* VERIFY_TEST_OUTPUT */ |
| 359 | |
| 360 | /* Erase */ |
| 361 | str_inf = std::string(str_inf.size(), ' '); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 362 | hal_lcd_display_text( |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 363 | str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 364 | } |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 365 | if (!PresentInferenceResult(asrResults)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 369 | profiler.PrintProfilingResult(); |
| 370 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 371 | return true; |
| 372 | } |
| 373 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 374 | /* KWS and ASR inference handler. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 375 | bool ClassifyAudioHandler(ApplicationContext& ctx, uint32_t clipIndex, bool runAll) |
| 376 | { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 377 | hal_lcd_clear(COLOR_BLACK); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 378 | |
| 379 | /* If the request has a valid size, set the audio index. */ |
| 380 | if (clipIndex < NUMBER_OF_FILES) { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 381 | if (!SetAppCtxIfmIdx(ctx, clipIndex, "kws_asr")) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 382 | return false; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | auto startClipIdx = ctx.Get<uint32_t>("clipIndex"); |
| 387 | |
| 388 | do { |
| 389 | KWSOutput kwsOutput = doKws(ctx); |
| 390 | if (!kwsOutput.executionSuccess) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 391 | printf_err("KWS failed\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 392 | return false; |
| 393 | } |
| 394 | |
| 395 | if (kwsOutput.asrAudioStart != nullptr && kwsOutput.asrAudioSamples > 0) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 396 | info("Trigger keyword spotted\n"); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 397 | if (!doAsr(ctx, kwsOutput)) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 398 | printf_err("ASR failed\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | } |
| 402 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 403 | IncrementAppCtxIfmIdx(ctx, "kws_asr"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 404 | |
| 405 | } while (runAll && ctx.Get<uint32_t>("clipIndex") != startClipIdx); |
| 406 | |
| 407 | return true; |
| 408 | } |
| 409 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 410 | static bool PresentInferenceResult(std::vector<arm::app::kws::KwsResult>& results) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 411 | { |
| 412 | constexpr uint32_t dataPsnTxtStartX1 = 20; |
| 413 | constexpr uint32_t dataPsnTxtStartY1 = 30; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 414 | constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 415 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 416 | hal_lcd_set_text_color(COLOR_GREEN); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 417 | |
| 418 | /* Display each result. */ |
| 419 | uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr; |
| 420 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 421 | for (auto& result : results) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 422 | std::string topKeyword{"<none>"}; |
| 423 | float score = 0.f; |
| 424 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 425 | if (!result.m_resultVec.empty()) { |
| 426 | topKeyword = result.m_resultVec[0].m_label; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 427 | score = result.m_resultVec[0].m_normalisedVal; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 430 | std::string resultStr = std::string{"@"} + std::to_string(result.m_timeStamp) + |
| 431 | std::string{"s: "} + topKeyword + std::string{" ("} + |
| 432 | std::to_string(static_cast<int>(score * 100)) + |
| 433 | std::string{"%)"}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 434 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 435 | hal_lcd_display_text( |
| 436 | resultStr.c_str(), resultStr.size(), dataPsnTxtStartX1, rowIdx1, 0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 437 | rowIdx1 += dataPsnTxtYIncr; |
| 438 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 439 | info("For timestamp: %f (inference #: %" PRIu32 "); threshold: %f\n", |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 440 | result.m_timeStamp, |
| 441 | result.m_inferenceNumber, |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 442 | result.m_threshold); |
| 443 | for (uint32_t j = 0; j < result.m_resultVec.size(); ++j) { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 444 | info("\t\tlabel @ %" PRIu32 ": %s, score: %f\n", |
| 445 | j, |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 446 | result.m_resultVec[j].m_label.c_str(), |
| 447 | result.m_resultVec[j].m_normalisedVal); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | return true; |
| 452 | } |
| 453 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 454 | static bool PresentInferenceResult(std::vector<arm::app::asr::AsrResult>& results) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 455 | { |
| 456 | constexpr uint32_t dataPsnTxtStartX1 = 20; |
| 457 | constexpr uint32_t dataPsnTxtStartY1 = 80; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 458 | constexpr bool allow_multiple_lines = true; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 459 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 460 | hal_lcd_set_text_color(COLOR_GREEN); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 461 | |
| 462 | /* Results from multiple inferences should be combined before processing. */ |
| 463 | std::vector<arm::app::ClassificationResult> combinedResults; |
| 464 | for (auto& result : results) { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 465 | combinedResults.insert( |
| 466 | combinedResults.end(), result.m_resultVec.begin(), result.m_resultVec.end()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | for (auto& result : results) { |
| 470 | /* Get the final result string using the decoder. */ |
| 471 | std::string infResultStr = audio::asr::DecodeOutput(result.m_resultVec); |
| 472 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 473 | info( |
| 474 | "Result for inf %" PRIu32 ": %s\n", result.m_inferenceNumber, infResultStr.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | std::string finalResultStr = audio::asr::DecodeOutput(combinedResults); |
| 478 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 479 | hal_lcd_display_text(finalResultStr.c_str(), |
| 480 | finalResultStr.size(), |
| 481 | dataPsnTxtStartX1, |
| 482 | dataPsnTxtStartY1, |
| 483 | allow_multiple_lines); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 484 | |
| 485 | info("Final result: %s\n", finalResultStr.c_str()); |
| 486 | return true; |
| 487 | } |
| 488 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 489 | } /* namespace app */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 490 | } /* namespace arm */ |