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 "InputFiles.hpp" /* For input images. */ |
| 19 | #include "Labels_dscnn.hpp" /* For DS-CNN label strings. */ |
| 20 | #include "Labels_wav2letter.hpp" /* For Wav2Letter label strings. */ |
| 21 | #include "Classifier.hpp" /* KWS classifier. */ |
| 22 | #include "AsrClassifier.hpp" /* ASR classifier. */ |
| 23 | #include "DsCnnModel.hpp" /* KWS model class for running inference. */ |
| 24 | #include "Wav2LetterModel.hpp" /* ASR model class for running inference. */ |
| 25 | #include "UseCaseCommonUtils.hpp" /* Utils functions. */ |
| 26 | #include "UseCaseHandler.hpp" /* Handlers for different user options. */ |
| 27 | #include "Wav2LetterPreprocess.hpp" /* ASR pre-processing class. */ |
| 28 | #include "Wav2LetterPostprocess.hpp"/* ASR post-processing class. */ |
| 29 | |
| 30 | using KwsClassifier = arm::app::Classifier; |
| 31 | |
| 32 | enum opcodes |
| 33 | { |
| 34 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */ |
| 35 | MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */ |
| 36 | MENU_OPT_RUN_INF_ALL, /* Run inference on all. */ |
| 37 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */ |
| 38 | MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clips. */ |
| 39 | }; |
| 40 | |
| 41 | static void DisplayMenu() |
| 42 | { |
Kshitij Sisodia | 3c8256d | 2021-05-24 16:12:40 +0100 | [diff] [blame] | 43 | printf("\n\n"); |
| 44 | printf("User input required\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | printf("Enter option number from:\n\n"); |
| 46 | printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT); |
| 47 | printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN); |
| 48 | printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL); |
| 49 | printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO); |
| 50 | printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS); |
| 51 | printf(" Choice: "); |
George Gekov | 93e5951 | 2021-08-03 11:18:41 +0100 | [diff] [blame] | 52 | fflush(stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** @brief Gets the number of MFCC features for a single window. */ |
| 56 | static uint32_t GetNumMfccFeatures(const arm::app::Model& model); |
| 57 | |
| 58 | /** @brief Gets the number of MFCC feature vectors to be computed. */ |
| 59 | static uint32_t GetNumMfccFeatureVectors(const arm::app::Model& model); |
| 60 | |
| 61 | /** @brief Gets the output context length (left and right) for post-processing. */ |
| 62 | static uint32_t GetOutputContextLen(const arm::app::Model& model, |
| 63 | uint32_t inputCtxLen); |
| 64 | |
| 65 | /** @brief Gets the output inner length for post-processing. */ |
| 66 | static uint32_t GetOutputInnerLen(const arm::app::Model& model, |
| 67 | uint32_t outputCtxLen); |
| 68 | |
| 69 | void main_loop(hal_platform& platform) |
| 70 | { |
| 71 | /* Model wrapper objects. */ |
| 72 | arm::app::DsCnnModel kwsModel; |
| 73 | arm::app::Wav2LetterModel asrModel; |
| 74 | |
| 75 | /* Load the models. */ |
| 76 | if (!kwsModel.Init()) { |
| 77 | printf_err("Failed to initialise KWS model\n"); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | /* Initialise the asr model using the same allocator from KWS |
| 82 | * to re-use the tensor arena. */ |
| 83 | if (!asrModel.Init(kwsModel.GetAllocator())) { |
| 84 | printf_err("Failed to initalise ASR model\n"); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | /* Initialise ASR pre-processing. */ |
| 89 | arm::app::audio::asr::Preprocess prep( |
| 90 | GetNumMfccFeatures(asrModel), |
| 91 | arm::app::asr::g_FrameLength, |
| 92 | arm::app::asr::g_FrameStride, |
| 93 | GetNumMfccFeatureVectors(asrModel)); |
| 94 | |
| 95 | /* Initialise ASR post-processing. */ |
| 96 | const uint32_t outputCtxLen = GetOutputContextLen(asrModel, arm::app::asr::g_ctxLen); |
| 97 | const uint32_t blankTokenIdx = 28; |
| 98 | arm::app::audio::asr::Postprocess postp( |
| 99 | outputCtxLen, |
| 100 | GetOutputInnerLen(asrModel, outputCtxLen), |
| 101 | blankTokenIdx); |
| 102 | |
| 103 | /* Instantiate application context. */ |
| 104 | arm::app::ApplicationContext caseContext; |
| 105 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 106 | arm::app::Profiler profiler{&platform, "kws_asr"}; |
| 107 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
| 108 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 109 | caseContext.Set<hal_platform&>("platform", platform); |
| 110 | caseContext.Set<arm::app::Model&>("kwsmodel", kwsModel); |
| 111 | caseContext.Set<arm::app::Model&>("asrmodel", asrModel); |
| 112 | caseContext.Set<uint32_t>("clipIndex", 0); |
| 113 | caseContext.Set<uint32_t>("ctxLen", arm::app::asr::g_ctxLen); /* Left and right context length (MFCC feat vectors). */ |
| 114 | caseContext.Set<int>("kwsframeLength", arm::app::kws::g_FrameLength); |
| 115 | caseContext.Set<int>("kwsframeStride", arm::app::kws::g_FrameStride); |
| 116 | caseContext.Set<float>("kwsscoreThreshold", arm::app::kws::g_ScoreThreshold); /* Normalised score threshold. */ |
| 117 | caseContext.Set<uint32_t >("kwsNumMfcc", arm::app::kws::g_NumMfcc); |
| 118 | caseContext.Set<uint32_t >("kwsNumAudioWins", arm::app::kws::g_NumAudioWins); |
| 119 | |
| 120 | caseContext.Set<int>("asrframeLength", arm::app::asr::g_FrameLength); |
| 121 | caseContext.Set<int>("asrframeStride", arm::app::asr::g_FrameStride); |
| 122 | caseContext.Set<float>("asrscoreThreshold", arm::app::asr::g_ScoreThreshold); /* Normalised score threshold. */ |
| 123 | |
| 124 | KwsClassifier kwsClassifier; /* Classifier wrapper object. */ |
| 125 | arm::app::AsrClassifier asrClassifier; /* Classifier wrapper object. */ |
| 126 | caseContext.Set<arm::app::Classifier&>("kwsclassifier", kwsClassifier); |
| 127 | caseContext.Set<arm::app::AsrClassifier&>("asrclassifier", asrClassifier); |
| 128 | |
| 129 | caseContext.Set<arm::app::audio::asr::Preprocess&>("preprocess", prep); |
| 130 | caseContext.Set<arm::app::audio::asr::Postprocess&>("postprocess", postp); |
| 131 | |
| 132 | std::vector<std::string> asrLabels; |
| 133 | arm::app::asr::GetLabelsVector(asrLabels); |
| 134 | std::vector<std::string> kwsLabels; |
| 135 | arm::app::kws::GetLabelsVector(kwsLabels); |
| 136 | caseContext.Set<const std::vector <std::string>&>("asrlabels", asrLabels); |
| 137 | caseContext.Set<const std::vector <std::string>&>("kwslabels", kwsLabels); |
| 138 | |
| 139 | /* Index of the kws outputs we trigger ASR on. */ |
| 140 | caseContext.Set<uint32_t>("keywordindex", 2); |
| 141 | |
| 142 | /* Loop. */ |
| 143 | bool executionSuccessful = true; |
| 144 | constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; |
| 145 | |
| 146 | /* Loop. */ |
| 147 | do { |
| 148 | int menuOption = MENU_OPT_RUN_INF_NEXT; |
| 149 | if (bUseMenu) { |
| 150 | DisplayMenu(); |
| 151 | menuOption = arm::app::ReadUserInputAsInt(platform); |
| 152 | printf("\n"); |
| 153 | } |
| 154 | switch (menuOption) { |
| 155 | case MENU_OPT_RUN_INF_NEXT: |
| 156 | executionSuccessful = ClassifyAudioHandler( |
| 157 | caseContext, |
| 158 | caseContext.Get<uint32_t>("clipIndex"), |
| 159 | false); |
| 160 | break; |
| 161 | case MENU_OPT_RUN_INF_CHOSEN: { |
| 162 | printf(" Enter the audio clip index [0, %d]: ", |
| 163 | NUMBER_OF_FILES-1); |
| 164 | auto clipIndex = static_cast<uint32_t>( |
| 165 | arm::app::ReadUserInputAsInt(platform)); |
| 166 | executionSuccessful = ClassifyAudioHandler(caseContext, |
| 167 | clipIndex, |
| 168 | false); |
| 169 | break; |
| 170 | } |
| 171 | case MENU_OPT_RUN_INF_ALL: |
| 172 | executionSuccessful = ClassifyAudioHandler( |
| 173 | caseContext, |
| 174 | caseContext.Get<uint32_t>("clipIndex"), |
| 175 | true); |
| 176 | break; |
| 177 | case MENU_OPT_SHOW_MODEL_INFO: |
| 178 | executionSuccessful = kwsModel.ShowModelInfoHandler(); |
| 179 | executionSuccessful = asrModel.ShowModelInfoHandler(); |
| 180 | break; |
| 181 | case MENU_OPT_LIST_AUDIO_CLIPS: |
| 182 | executionSuccessful = ListFilesHandler(caseContext); |
| 183 | break; |
| 184 | default: |
| 185 | printf("Incorrect choice, try again."); |
| 186 | break; |
| 187 | } |
| 188 | } while (executionSuccessful && bUseMenu); |
| 189 | info("Main loop terminated.\n"); |
| 190 | } |
| 191 | |
| 192 | static uint32_t GetNumMfccFeatures(const arm::app::Model& model) |
| 193 | { |
| 194 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 195 | const int inputCols = inputTensor->dims->data[arm::app::Wav2LetterModel::ms_inputColsIdx]; |
| 196 | if (0 != inputCols % 3) { |
| 197 | printf_err("Number of input columns is not a multiple of 3\n"); |
| 198 | } |
| 199 | return std::max(inputCols/3, 0); |
| 200 | } |
| 201 | |
| 202 | static uint32_t GetNumMfccFeatureVectors(const arm::app::Model& model) |
| 203 | { |
| 204 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 205 | const int inputRows = inputTensor->dims->data[arm::app::Wav2LetterModel::ms_inputRowsIdx]; |
| 206 | return std::max(inputRows, 0); |
| 207 | } |
| 208 | |
| 209 | static uint32_t GetOutputContextLen(const arm::app::Model& model, const uint32_t inputCtxLen) |
| 210 | { |
| 211 | const uint32_t inputRows = GetNumMfccFeatureVectors(model); |
| 212 | const uint32_t inputInnerLen = inputRows - (2 * inputCtxLen); |
| 213 | constexpr uint32_t ms_outputRowsIdx = arm::app::Wav2LetterModel::ms_outputRowsIdx; |
| 214 | |
| 215 | /* Check to make sure that the input tensor supports the above context and inner lengths. */ |
| 216 | if (inputRows <= 2 * inputCtxLen || inputRows <= inputInnerLen) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 217 | printf_err("Input rows not compatible with ctx of %" PRIu32 "\n", |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 218 | inputCtxLen); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
| 223 | const uint32_t outputRows = std::max(outputTensor->dims->data[ms_outputRowsIdx], 0); |
| 224 | |
| 225 | const float tensorColRatio = static_cast<float>(inputRows)/ |
| 226 | static_cast<float>(outputRows); |
| 227 | |
| 228 | return std::round(static_cast<float>(inputCtxLen)/tensorColRatio); |
| 229 | } |
| 230 | |
| 231 | static uint32_t GetOutputInnerLen(const arm::app::Model& model, |
| 232 | const uint32_t outputCtxLen) |
| 233 | { |
| 234 | constexpr uint32_t ms_outputRowsIdx = arm::app::Wav2LetterModel::ms_outputRowsIdx; |
| 235 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
| 236 | const uint32_t outputRows = std::max(outputTensor->dims->data[ms_outputRowsIdx], 0); |
| 237 | return (outputRows - (2 * outputCtxLen)); |
| 238 | } |