alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. All rights reserved. |
| 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 "hal.h" /* Brings in platform definitions. */ |
| 18 | #include "Labels.hpp" /* For label strings. */ |
| 19 | #include "UseCaseHandler.hpp" /* Handlers for different user options. */ |
| 20 | #include "Wav2LetterModel.hpp" /* Model class for running inference. */ |
| 21 | #include "UseCaseCommonUtils.hpp" /* Utils functions. */ |
| 22 | #include "AsrClassifier.hpp" /* Classifier. */ |
| 23 | #include "InputFiles.hpp" /* Generated audio clip header. */ |
| 24 | #include "Wav2LetterPreprocess.hpp" /* Pre-processing class. */ |
| 25 | #include "Wav2LetterPostprocess.hpp" /* Post-processing class. */ |
| 26 | |
| 27 | enum opcodes |
| 28 | { |
| 29 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */ |
| 30 | MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */ |
| 31 | MENU_OPT_RUN_INF_ALL, /* Run inference on all. */ |
| 32 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */ |
| 33 | MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clips. */ |
| 34 | }; |
| 35 | |
| 36 | static void DisplayMenu() |
| 37 | { |
| 38 | printf("\n\nUser input required\n"); |
| 39 | printf("Enter option number from:\n\n"); |
| 40 | printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT); |
| 41 | printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN); |
| 42 | printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL); |
| 43 | printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO); |
| 44 | printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS); |
| 45 | printf(" Choice: "); |
| 46 | } |
| 47 | |
| 48 | /** @brief Verify input and output tensor are of certain min dimensions. */ |
| 49 | static bool VerifyTensorDimensions(const arm::app::Model& model); |
| 50 | |
| 51 | /** @brief Gets the number of MFCC features for a single window. */ |
| 52 | static uint32_t GetNumMfccFeatures(const arm::app::Model& model); |
| 53 | |
| 54 | /** @brief Gets the number of MFCC feature vectors to be computed. */ |
| 55 | static uint32_t GetNumMfccFeatureVectors(const arm::app::Model& model); |
| 56 | |
| 57 | /** @brief Gets the output context length (left and right) for post-processing. */ |
| 58 | static uint32_t GetOutputContextLen(const arm::app::Model& model, |
| 59 | uint32_t inputCtxLen); |
| 60 | |
| 61 | /** @brief Gets the output inner length for post-processing. */ |
| 62 | static uint32_t GetOutputInnerLen(const arm::app::Model& model, |
| 63 | uint32_t outputCtxLen); |
| 64 | |
| 65 | void main_loop(hal_platform& platform) |
| 66 | { |
| 67 | arm::app::Wav2LetterModel model; /* Model wrapper object. */ |
| 68 | |
| 69 | /* Load the model. */ |
| 70 | if (!model.Init()) { |
| 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 | |
| 78 | /* Initialise pre-processing. */ |
| 79 | arm::app::audio::asr::Preprocess prep( |
| 80 | GetNumMfccFeatures(model), |
| 81 | g_FrameLength, |
| 82 | g_FrameStride, |
| 83 | GetNumMfccFeatureVectors(model)); |
| 84 | |
| 85 | /* Initialise post-processing. */ |
| 86 | const uint32_t outputCtxLen = GetOutputContextLen(model, g_ctxLen); |
| 87 | const uint32_t blankTokenIdx = 28; |
| 88 | arm::app::audio::asr::Postprocess postp( |
| 89 | outputCtxLen, |
| 90 | GetOutputInnerLen(model, outputCtxLen), |
| 91 | blankTokenIdx); |
| 92 | |
| 93 | /* Instantiate application context. */ |
| 94 | arm::app::ApplicationContext caseContext; |
| 95 | std::vector <std::string> labels; |
| 96 | GetLabelsVector(labels); |
| 97 | arm::app::AsrClassifier classifier; /* Classifier wrapper object. */ |
| 98 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 99 | arm::app::Profiler profiler{&platform, "asr"}; |
| 100 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 101 | caseContext.Set<hal_platform&>("platform", platform); |
| 102 | caseContext.Set<arm::app::Model&>("model", model); |
| 103 | caseContext.Set<uint32_t>("clipIndex", 0); |
| 104 | caseContext.Set<uint32_t>("frameLength", g_FrameLength); |
| 105 | caseContext.Set<uint32_t>("frameStride", g_FrameStride); |
| 106 | caseContext.Set<float>("scoreThreshold", g_ScoreThreshold); /* Score threshold. */ |
| 107 | caseContext.Set<uint32_t>("ctxLen", g_ctxLen); /* Left and right context length (MFCC feat vectors). */ |
| 108 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 109 | caseContext.Set<arm::app::AsrClassifier&>("classifier", classifier); |
| 110 | caseContext.Set<arm::app::audio::asr::Preprocess&>("preprocess", prep); |
| 111 | caseContext.Set<arm::app::audio::asr::Postprocess&>("postprocess", postp); |
| 112 | |
| 113 | bool executionSuccessful = true; |
| 114 | constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; |
| 115 | |
| 116 | /* Loop. */ |
| 117 | do { |
| 118 | int menuOption = MENU_OPT_RUN_INF_NEXT; |
| 119 | if (bUseMenu) { |
| 120 | DisplayMenu(); |
| 121 | menuOption = arm::app::ReadUserInputAsInt(platform); |
| 122 | printf("\n"); |
| 123 | } |
| 124 | switch (menuOption) { |
| 125 | case MENU_OPT_RUN_INF_NEXT: |
| 126 | executionSuccessful = ClassifyAudioHandler( |
| 127 | caseContext, |
| 128 | caseContext.Get<uint32_t>("clipIndex"), |
| 129 | false); |
| 130 | break; |
| 131 | case MENU_OPT_RUN_INF_CHOSEN: { |
| 132 | printf(" Enter the audio clip index [0, %d]: ", |
| 133 | NUMBER_OF_FILES-1); |
| 134 | auto clipIndex = static_cast<uint32_t>( |
| 135 | arm::app::ReadUserInputAsInt(platform)); |
| 136 | executionSuccessful = ClassifyAudioHandler(caseContext, |
| 137 | clipIndex, |
| 138 | false); |
| 139 | break; |
| 140 | } |
| 141 | case MENU_OPT_RUN_INF_ALL: |
| 142 | executionSuccessful = ClassifyAudioHandler( |
| 143 | caseContext, |
| 144 | caseContext.Get<uint32_t>("clipIndex"), |
| 145 | true); |
| 146 | break; |
| 147 | case MENU_OPT_SHOW_MODEL_INFO: |
| 148 | executionSuccessful = model.ShowModelInfoHandler(); |
| 149 | break; |
| 150 | case MENU_OPT_LIST_AUDIO_CLIPS: |
| 151 | executionSuccessful = ListFilesHandler(caseContext); |
| 152 | break; |
| 153 | default: |
| 154 | printf("Incorrect choice, try again."); |
| 155 | break; |
| 156 | } |
| 157 | } while (executionSuccessful && bUseMenu); |
| 158 | info("Main loop terminated.\n"); |
| 159 | } |
| 160 | |
| 161 | static bool VerifyTensorDimensions(const arm::app::Model& model) |
| 162 | { |
| 163 | /* Populate tensor related parameters. */ |
| 164 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 165 | if (!inputTensor->dims) { |
| 166 | printf_err("Invalid input tensor dims\n"); |
| 167 | return false; |
| 168 | } else if (inputTensor->dims->size < 3) { |
| 169 | printf_err("Input tensor dimension should be >= 3\n"); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
| 174 | if (!outputTensor->dims) { |
| 175 | printf_err("Invalid output tensor dims\n"); |
| 176 | return false; |
| 177 | } else if (outputTensor->dims->size < 3) { |
| 178 | printf_err("Output tensor dimension should be >= 3\n"); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | static uint32_t GetNumMfccFeatures(const arm::app::Model& model) |
| 186 | { |
| 187 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 188 | const int inputCols = inputTensor->dims->data[arm::app::Wav2LetterModel::ms_inputColsIdx]; |
| 189 | if (0 != inputCols % 3) { |
| 190 | printf_err("Number of input columns is not a multiple of 3\n"); |
| 191 | } |
| 192 | return std::max(inputCols/3, 0); |
| 193 | } |
| 194 | |
| 195 | static uint32_t GetNumMfccFeatureVectors(const arm::app::Model& model) |
| 196 | { |
| 197 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 198 | const int inputRows = inputTensor->dims->data[arm::app::Wav2LetterModel::ms_inputRowsIdx]; |
| 199 | return std::max(inputRows, 0); |
| 200 | } |
| 201 | |
| 202 | static uint32_t GetOutputContextLen(const arm::app::Model& model, const uint32_t inputCtxLen) |
| 203 | { |
| 204 | const uint32_t inputRows = GetNumMfccFeatureVectors(model); |
| 205 | const uint32_t inputInnerLen = inputRows - (2 * inputCtxLen); |
| 206 | constexpr uint32_t ms_outputRowsIdx = arm::app::Wav2LetterModel::ms_outputRowsIdx; |
| 207 | |
| 208 | /* Check to make sure that the input tensor supports the above |
| 209 | * context and inner lengths. */ |
| 210 | if (inputRows <= 2 * inputCtxLen || inputRows <= inputInnerLen) { |
| 211 | printf_err("Input rows not compatible with ctx of %u\n", |
| 212 | inputCtxLen); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
| 217 | const uint32_t outputRows = std::max(outputTensor->dims->data[ms_outputRowsIdx], 0); |
| 218 | |
| 219 | const float tensorColRatio = static_cast<float>(inputRows)/ |
| 220 | static_cast<float>(outputRows); |
| 221 | |
| 222 | return std::round(static_cast<float>(inputCtxLen)/tensorColRatio); |
| 223 | } |
| 224 | |
| 225 | static uint32_t GetOutputInnerLen(const arm::app::Model& model, |
| 226 | const uint32_t outputCtxLen) |
| 227 | { |
| 228 | constexpr uint32_t ms_outputRowsIdx = arm::app::Wav2LetterModel::ms_outputRowsIdx; |
| 229 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
| 230 | const uint32_t outputRows = std::max(outputTensor->dims->data[ms_outputRowsIdx], 0); |
| 231 | return (outputRows - (2 * outputCtxLen)); |
| 232 | } |