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