alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com> |
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 "Labels.hpp" /* For label strings. */ |
| 18 | #include "UseCaseHandler.hpp" /* Handlers for different user options. */ |
| 19 | #include "Wav2LetterModel.hpp" /* Model class for running inference. */ |
| 20 | #include "UseCaseCommonUtils.hpp" /* Utils functions. */ |
| 21 | #include "AsrClassifier.hpp" /* Classifier. */ |
| 22 | #include "InputFiles.hpp" /* Generated audio clip header. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 23 | #include "log_macros.h" /* Logging functions */ |
| 24 | #include "BufAttributes.hpp" /* Buffer attributes to be applied */ |
| 25 | |
| 26 | namespace arm { |
| 27 | namespace app { |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 28 | static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE; |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 29 | namespace asr { |
| 30 | extern uint8_t* GetModelPointer(); |
| 31 | extern size_t GetModelLen(); |
| 32 | } /* namespace asr */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 33 | } /* namespace app */ |
| 34 | } /* namespace arm */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 35 | |
| 36 | enum opcodes |
| 37 | { |
| 38 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */ |
| 39 | MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */ |
| 40 | MENU_OPT_RUN_INF_ALL, /* Run inference on all. */ |
| 41 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */ |
| 42 | MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clips. */ |
| 43 | }; |
| 44 | |
| 45 | static void DisplayMenu() |
| 46 | { |
Kshitij Sisodia | 3c8256d | 2021-05-24 16:12:40 +0100 | [diff] [blame] | 47 | printf("\n\n"); |
| 48 | printf("User input required\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 49 | printf("Enter option number from:\n\n"); |
| 50 | printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT); |
| 51 | printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN); |
| 52 | printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL); |
| 53 | printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO); |
| 54 | printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS); |
| 55 | printf(" Choice: "); |
George Gekov | 93e5951 | 2021-08-03 11:18:41 +0100 | [diff] [blame] | 56 | fflush(stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Richard Burton | c291144 | 2022-04-22 09:08:21 +0100 | [diff] [blame] | 59 | /** @brief Verify input and output tensor are of certain min dimensions. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 60 | static bool VerifyTensorDimensions(const arm::app::Model& model); |
| 61 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 62 | void main_loop() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 63 | { |
| 64 | arm::app::Wav2LetterModel model; /* Model wrapper object. */ |
| 65 | |
| 66 | /* Load the model. */ |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 67 | if (!model.Init(arm::app::tensorArena, |
| 68 | sizeof(arm::app::tensorArena), |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 69 | arm::app::asr::GetModelPointer(), |
| 70 | arm::app::asr::GetModelLen())) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | printf_err("Failed to initialise model\n"); |
| 72 | return; |
| 73 | } else if (!VerifyTensorDimensions(model)) { |
| 74 | printf_err("Model's input or output dimension verification failed\n"); |
| 75 | return; |
| 76 | } |
| 77 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 78 | /* Instantiate application context. */ |
| 79 | arm::app::ApplicationContext caseContext; |
| 80 | std::vector <std::string> labels; |
| 81 | GetLabelsVector(labels); |
| 82 | arm::app::AsrClassifier classifier; /* Classifier wrapper object. */ |
| 83 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 84 | arm::app::Profiler profiler{"asr"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 85 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | caseContext.Set<arm::app::Model&>("model", model); |
| 87 | caseContext.Set<uint32_t>("clipIndex", 0); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 88 | caseContext.Set<uint32_t>("frameLength", arm::app::asr::g_FrameLength); |
| 89 | caseContext.Set<uint32_t>("frameStride", arm::app::asr::g_FrameStride); |
| 90 | caseContext.Set<float>("scoreThreshold", arm::app::asr::g_ScoreThreshold); /* Score threshold. */ |
| 91 | caseContext.Set<uint32_t>("ctxLen", arm::app::asr::g_ctxLen); /* Left and right context length (MFCC feat vectors). */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 93 | caseContext.Set<arm::app::AsrClassifier&>("classifier", classifier); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 94 | |
| 95 | bool executionSuccessful = true; |
| 96 | constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; |
| 97 | |
| 98 | /* Loop. */ |
| 99 | do { |
| 100 | int menuOption = MENU_OPT_RUN_INF_NEXT; |
| 101 | if (bUseMenu) { |
| 102 | DisplayMenu(); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 103 | menuOption = arm::app::ReadUserInputAsInt(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 104 | printf("\n"); |
| 105 | } |
| 106 | switch (menuOption) { |
| 107 | case MENU_OPT_RUN_INF_NEXT: |
| 108 | executionSuccessful = ClassifyAudioHandler( |
| 109 | caseContext, |
| 110 | caseContext.Get<uint32_t>("clipIndex"), |
| 111 | false); |
| 112 | break; |
| 113 | case MENU_OPT_RUN_INF_CHOSEN: { |
| 114 | printf(" Enter the audio clip index [0, %d]: ", |
| 115 | NUMBER_OF_FILES-1); |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 116 | fflush(stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 117 | auto clipIndex = static_cast<uint32_t>( |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 118 | arm::app::ReadUserInputAsInt()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 119 | executionSuccessful = ClassifyAudioHandler(caseContext, |
| 120 | clipIndex, |
| 121 | false); |
| 122 | break; |
| 123 | } |
| 124 | case MENU_OPT_RUN_INF_ALL: |
| 125 | executionSuccessful = ClassifyAudioHandler( |
| 126 | caseContext, |
| 127 | caseContext.Get<uint32_t>("clipIndex"), |
| 128 | true); |
| 129 | break; |
| 130 | case MENU_OPT_SHOW_MODEL_INFO: |
| 131 | executionSuccessful = model.ShowModelInfoHandler(); |
| 132 | break; |
| 133 | case MENU_OPT_LIST_AUDIO_CLIPS: |
| 134 | executionSuccessful = ListFilesHandler(caseContext); |
| 135 | break; |
| 136 | default: |
| 137 | printf("Incorrect choice, try again."); |
| 138 | break; |
| 139 | } |
| 140 | } while (executionSuccessful && bUseMenu); |
| 141 | info("Main loop terminated.\n"); |
| 142 | } |
| 143 | |
| 144 | static bool VerifyTensorDimensions(const arm::app::Model& model) |
| 145 | { |
| 146 | /* Populate tensor related parameters. */ |
| 147 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 148 | if (!inputTensor->dims) { |
| 149 | printf_err("Invalid input tensor dims\n"); |
| 150 | return false; |
| 151 | } else if (inputTensor->dims->size < 3) { |
| 152 | printf_err("Input tensor dimension should be >= 3\n"); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
| 157 | if (!outputTensor->dims) { |
| 158 | printf_err("Invalid output tensor dims\n"); |
| 159 | return false; |
| 160 | } else if (outputTensor->dims->size < 3) { |
| 161 | printf_err("Output tensor dimension should be >= 3\n"); |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | return true; |
| 166 | } |