alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [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 | */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 17 | #include "InputFiles.hpp" /* For input images. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 18 | #include "Labels_micronetkws.hpp" /* For MicroNetKws label strings. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 19 | #include "Labels_wav2letter.hpp" /* For Wav2Letter label strings. */ |
| 20 | #include "Classifier.hpp" /* KWS classifier. */ |
| 21 | #include "AsrClassifier.hpp" /* ASR classifier. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 22 | #include "MicroNetKwsModel.hpp" /* KWS model class for running inference. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 23 | #include "Wav2LetterModel.hpp" /* ASR model class for running inference. */ |
| 24 | #include "UseCaseCommonUtils.hpp" /* Utils functions. */ |
| 25 | #include "UseCaseHandler.hpp" /* Handlers for different user options. */ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 26 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 27 | |
| 28 | using KwsClassifier = arm::app::Classifier; |
| 29 | |
| 30 | enum opcodes |
| 31 | { |
| 32 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */ |
| 33 | MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */ |
| 34 | MENU_OPT_RUN_INF_ALL, /* Run inference on all. */ |
| 35 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */ |
| 36 | MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clips. */ |
| 37 | }; |
| 38 | |
| 39 | static void DisplayMenu() |
| 40 | { |
Kshitij Sisodia | 3c8256d | 2021-05-24 16:12:40 +0100 | [diff] [blame] | 41 | printf("\n\n"); |
| 42 | printf("User input required\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 43 | printf("Enter option number from:\n\n"); |
| 44 | printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT); |
| 45 | printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN); |
| 46 | printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL); |
| 47 | printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO); |
| 48 | printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS); |
| 49 | printf(" Choice: "); |
George Gekov | 93e5951 | 2021-08-03 11:18:41 +0100 | [diff] [blame] | 50 | fflush(stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 53 | /** @brief Verify input and output tensor are of certain min dimensions. */ |
| 54 | static bool VerifyTensorDimensions(const arm::app::Model& model); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 55 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 56 | void main_loop() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 57 | { |
| 58 | /* Model wrapper objects. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 59 | arm::app::MicroNetKwsModel kwsModel; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 60 | arm::app::Wav2LetterModel asrModel; |
| 61 | |
| 62 | /* Load the models. */ |
| 63 | if (!kwsModel.Init()) { |
| 64 | printf_err("Failed to initialise KWS model\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | /* Initialise the asr model using the same allocator from KWS |
| 69 | * to re-use the tensor arena. */ |
| 70 | if (!asrModel.Init(kwsModel.GetAllocator())) { |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 71 | printf_err("Failed to initialise ASR model\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 72 | return; |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 73 | } else if (!VerifyTensorDimensions(asrModel)) { |
| 74 | printf_err("Model's input or output dimension verification failed\n"); |
| 75 | return; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 76 | } |
| 77 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 78 | /* Instantiate application context. */ |
| 79 | arm::app::ApplicationContext caseContext; |
| 80 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 81 | arm::app::Profiler profiler{"kws_asr"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 82 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 83 | caseContext.Set<arm::app::Model&>("kwsModel", kwsModel); |
| 84 | caseContext.Set<arm::app::Model&>("asrModel", asrModel); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 85 | caseContext.Set<uint32_t>("clipIndex", 0); |
| 86 | caseContext.Set<uint32_t>("ctxLen", arm::app::asr::g_ctxLen); /* Left and right context length (MFCC feat vectors). */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 87 | caseContext.Set<int>("kwsFrameLength", arm::app::kws::g_FrameLength); |
| 88 | caseContext.Set<int>("kwsFrameStride", arm::app::kws::g_FrameStride); |
| 89 | caseContext.Set<float>("kwsScoreThreshold", arm::app::kws::g_ScoreThreshold); /* Normalised score threshold. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 90 | caseContext.Set<uint32_t >("kwsNumMfcc", arm::app::kws::g_NumMfcc); |
| 91 | caseContext.Set<uint32_t >("kwsNumAudioWins", arm::app::kws::g_NumAudioWins); |
| 92 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 93 | caseContext.Set<int>("asrFrameLength", arm::app::asr::g_FrameLength); |
| 94 | caseContext.Set<int>("asrFrameStride", arm::app::asr::g_FrameStride); |
| 95 | caseContext.Set<float>("asrScoreThreshold", arm::app::asr::g_ScoreThreshold); /* Normalised score threshold. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | |
| 97 | KwsClassifier kwsClassifier; /* Classifier wrapper object. */ |
| 98 | arm::app::AsrClassifier asrClassifier; /* Classifier wrapper object. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 99 | caseContext.Set<arm::app::Classifier&>("kwsClassifier", kwsClassifier); |
| 100 | caseContext.Set<arm::app::AsrClassifier&>("asrClassifier", asrClassifier); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 101 | |
| 102 | std::vector<std::string> asrLabels; |
| 103 | arm::app::asr::GetLabelsVector(asrLabels); |
| 104 | std::vector<std::string> kwsLabels; |
| 105 | arm::app::kws::GetLabelsVector(kwsLabels); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 106 | caseContext.Set<const std::vector <std::string>&>("asrLabels", asrLabels); |
| 107 | caseContext.Set<const std::vector <std::string>&>("kwsLabels", kwsLabels); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 108 | |
Liam Barry | b5b32d3 | 2021-12-30 11:35:00 +0000 | [diff] [blame] | 109 | /* KWS keyword that triggers ASR and associated checks */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 110 | std::string triggerKeyword = std::string("no"); |
Liam Barry | b5b32d3 | 2021-12-30 11:35:00 +0000 | [diff] [blame] | 111 | if (std::find(kwsLabels.begin(), kwsLabels.end(), triggerKeyword) != kwsLabels.end()) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 112 | caseContext.Set<const std::string &>("triggerKeyword", triggerKeyword); |
Liam Barry | b5b32d3 | 2021-12-30 11:35:00 +0000 | [diff] [blame] | 113 | } |
| 114 | else { |
| 115 | printf_err("Selected trigger keyword not found in labels file\n"); |
| 116 | return; |
| 117 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 118 | |
| 119 | /* Loop. */ |
| 120 | bool executionSuccessful = true; |
| 121 | constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; |
| 122 | |
| 123 | /* Loop. */ |
| 124 | do { |
| 125 | int menuOption = MENU_OPT_RUN_INF_NEXT; |
| 126 | if (bUseMenu) { |
| 127 | DisplayMenu(); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 128 | menuOption = arm::app::ReadUserInputAsInt(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 129 | printf("\n"); |
| 130 | } |
| 131 | switch (menuOption) { |
| 132 | case MENU_OPT_RUN_INF_NEXT: |
| 133 | executionSuccessful = ClassifyAudioHandler( |
| 134 | caseContext, |
| 135 | caseContext.Get<uint32_t>("clipIndex"), |
| 136 | false); |
| 137 | break; |
| 138 | case MENU_OPT_RUN_INF_CHOSEN: { |
| 139 | printf(" Enter the audio clip index [0, %d]: ", |
| 140 | NUMBER_OF_FILES-1); |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 141 | fflush(stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 142 | auto clipIndex = static_cast<uint32_t>( |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 143 | arm::app::ReadUserInputAsInt()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 144 | executionSuccessful = ClassifyAudioHandler(caseContext, |
| 145 | clipIndex, |
| 146 | false); |
| 147 | break; |
| 148 | } |
| 149 | case MENU_OPT_RUN_INF_ALL: |
| 150 | executionSuccessful = ClassifyAudioHandler( |
| 151 | caseContext, |
| 152 | caseContext.Get<uint32_t>("clipIndex"), |
| 153 | true); |
| 154 | break; |
| 155 | case MENU_OPT_SHOW_MODEL_INFO: |
| 156 | executionSuccessful = kwsModel.ShowModelInfoHandler(); |
| 157 | executionSuccessful = asrModel.ShowModelInfoHandler(); |
| 158 | break; |
| 159 | case MENU_OPT_LIST_AUDIO_CLIPS: |
| 160 | executionSuccessful = ListFilesHandler(caseContext); |
| 161 | break; |
| 162 | default: |
| 163 | printf("Incorrect choice, try again."); |
| 164 | break; |
| 165 | } |
| 166 | } while (executionSuccessful && bUseMenu); |
| 167 | info("Main loop terminated.\n"); |
| 168 | } |
| 169 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 170 | static bool VerifyTensorDimensions(const arm::app::Model& model) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 171 | { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 172 | /* Populate tensor related parameters. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 173 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 174 | if (!inputTensor->dims) { |
| 175 | printf_err("Invalid input tensor dims\n"); |
| 176 | return false; |
| 177 | } else if (inputTensor->dims->size < 3) { |
| 178 | printf_err("Input tensor dimension should be >= 3\n"); |
| 179 | return false; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 183 | if (!outputTensor->dims) { |
| 184 | printf_err("Invalid output tensor dims\n"); |
| 185 | return false; |
| 186 | } else if (outputTensor->dims->size < 3) { |
| 187 | printf_err("Output tensor dimension should be >= 3\n"); |
| 188 | return false; |
| 189 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 190 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame^] | 191 | return true; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 192 | } |