blob: 87145f4716158cd62c1c6b8ff044287b413423f2 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
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
27enum 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
36static void DisplayMenu()
37{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010038 printf("\n\n");
39 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000040 printf("Enter option number from:\n\n");
41 printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT);
42 printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
43 printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL);
44 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
45 printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
46 printf(" Choice: ");
George Gekov93e59512021-08-03 11:18:41 +010047 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000048}
49
50/** @brief Verify input and output tensor are of certain min dimensions. */
51static bool VerifyTensorDimensions(const arm::app::Model& model);
52
53/** @brief Gets the number of MFCC features for a single window. */
54static uint32_t GetNumMfccFeatures(const arm::app::Model& model);
55
56/** @brief Gets the number of MFCC feature vectors to be computed. */
57static uint32_t GetNumMfccFeatureVectors(const arm::app::Model& model);
58
59/** @brief Gets the output context length (left and right) for post-processing. */
60static uint32_t GetOutputContextLen(const arm::app::Model& model,
61 uint32_t inputCtxLen);
62
63/** @brief Gets the output inner length for post-processing. */
64static uint32_t GetOutputInnerLen(const arm::app::Model& model,
65 uint32_t outputCtxLen);
66
67void main_loop(hal_platform& platform)
68{
69 arm::app::Wav2LetterModel model; /* Model wrapper object. */
70
71 /* Load the model. */
72 if (!model.Init()) {
73 printf_err("Failed to initialise model\n");
74 return;
75 } else if (!VerifyTensorDimensions(model)) {
76 printf_err("Model's input or output dimension verification failed\n");
77 return;
78 }
79
80 /* Initialise pre-processing. */
81 arm::app::audio::asr::Preprocess prep(
82 GetNumMfccFeatures(model),
83 g_FrameLength,
84 g_FrameStride,
85 GetNumMfccFeatureVectors(model));
86
87 /* Initialise post-processing. */
88 const uint32_t outputCtxLen = GetOutputContextLen(model, g_ctxLen);
89 const uint32_t blankTokenIdx = 28;
90 arm::app::audio::asr::Postprocess postp(
91 outputCtxLen,
92 GetOutputInnerLen(model, outputCtxLen),
93 blankTokenIdx);
94
95 /* Instantiate application context. */
96 arm::app::ApplicationContext caseContext;
97 std::vector <std::string> labels;
98 GetLabelsVector(labels);
99 arm::app::AsrClassifier classifier; /* Classifier wrapper object. */
100
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100101 arm::app::Profiler profiler{&platform, "asr"};
102 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +0000103 caseContext.Set<hal_platform&>("platform", platform);
104 caseContext.Set<arm::app::Model&>("model", model);
105 caseContext.Set<uint32_t>("clipIndex", 0);
106 caseContext.Set<uint32_t>("frameLength", g_FrameLength);
107 caseContext.Set<uint32_t>("frameStride", g_FrameStride);
108 caseContext.Set<float>("scoreThreshold", g_ScoreThreshold); /* Score threshold. */
109 caseContext.Set<uint32_t>("ctxLen", g_ctxLen); /* Left and right context length (MFCC feat vectors). */
110 caseContext.Set<const std::vector <std::string>&>("labels", labels);
111 caseContext.Set<arm::app::AsrClassifier&>("classifier", classifier);
112 caseContext.Set<arm::app::audio::asr::Preprocess&>("preprocess", prep);
113 caseContext.Set<arm::app::audio::asr::Postprocess&>("postprocess", postp);
114
115 bool executionSuccessful = true;
116 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
117
118 /* Loop. */
119 do {
120 int menuOption = MENU_OPT_RUN_INF_NEXT;
121 if (bUseMenu) {
122 DisplayMenu();
123 menuOption = arm::app::ReadUserInputAsInt(platform);
124 printf("\n");
125 }
126 switch (menuOption) {
127 case MENU_OPT_RUN_INF_NEXT:
128 executionSuccessful = ClassifyAudioHandler(
129 caseContext,
130 caseContext.Get<uint32_t>("clipIndex"),
131 false);
132 break;
133 case MENU_OPT_RUN_INF_CHOSEN: {
134 printf(" Enter the audio clip index [0, %d]: ",
135 NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100136 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +0000137 auto clipIndex = static_cast<uint32_t>(
138 arm::app::ReadUserInputAsInt(platform));
139 executionSuccessful = ClassifyAudioHandler(caseContext,
140 clipIndex,
141 false);
142 break;
143 }
144 case MENU_OPT_RUN_INF_ALL:
145 executionSuccessful = ClassifyAudioHandler(
146 caseContext,
147 caseContext.Get<uint32_t>("clipIndex"),
148 true);
149 break;
150 case MENU_OPT_SHOW_MODEL_INFO:
151 executionSuccessful = model.ShowModelInfoHandler();
152 break;
153 case MENU_OPT_LIST_AUDIO_CLIPS:
154 executionSuccessful = ListFilesHandler(caseContext);
155 break;
156 default:
157 printf("Incorrect choice, try again.");
158 break;
159 }
160 } while (executionSuccessful && bUseMenu);
161 info("Main loop terminated.\n");
162}
163
164static bool VerifyTensorDimensions(const arm::app::Model& model)
165{
166 /* Populate tensor related parameters. */
167 TfLiteTensor* inputTensor = model.GetInputTensor(0);
168 if (!inputTensor->dims) {
169 printf_err("Invalid input tensor dims\n");
170 return false;
171 } else if (inputTensor->dims->size < 3) {
172 printf_err("Input tensor dimension should be >= 3\n");
173 return false;
174 }
175
176 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
177 if (!outputTensor->dims) {
178 printf_err("Invalid output tensor dims\n");
179 return false;
180 } else if (outputTensor->dims->size < 3) {
181 printf_err("Output tensor dimension should be >= 3\n");
182 return false;
183 }
184
185 return true;
186}
187
188static uint32_t GetNumMfccFeatures(const arm::app::Model& model)
189{
190 TfLiteTensor* inputTensor = model.GetInputTensor(0);
191 const int inputCols = inputTensor->dims->data[arm::app::Wav2LetterModel::ms_inputColsIdx];
192 if (0 != inputCols % 3) {
193 printf_err("Number of input columns is not a multiple of 3\n");
194 }
195 return std::max(inputCols/3, 0);
196}
197
198static uint32_t GetNumMfccFeatureVectors(const arm::app::Model& model)
199{
200 TfLiteTensor* inputTensor = model.GetInputTensor(0);
201 const int inputRows = inputTensor->dims->data[arm::app::Wav2LetterModel::ms_inputRowsIdx];
202 return std::max(inputRows, 0);
203}
204
205static uint32_t GetOutputContextLen(const arm::app::Model& model, const uint32_t inputCtxLen)
206{
207 const uint32_t inputRows = GetNumMfccFeatureVectors(model);
208 const uint32_t inputInnerLen = inputRows - (2 * inputCtxLen);
209 constexpr uint32_t ms_outputRowsIdx = arm::app::Wav2LetterModel::ms_outputRowsIdx;
210
211 /* Check to make sure that the input tensor supports the above
212 * context and inner lengths. */
213 if (inputRows <= 2 * inputCtxLen || inputRows <= inputInnerLen) {
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100214 printf_err("Input rows not compatible with ctx of %" PRIu32 "\n",
alexander3c798932021-03-26 21:42:19 +0000215 inputCtxLen);
216 return 0;
217 }
218
219 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
220 const uint32_t outputRows = std::max(outputTensor->dims->data[ms_outputRowsIdx], 0);
221
222 const float tensorColRatio = static_cast<float>(inputRows)/
223 static_cast<float>(outputRows);
224
225 return std::round(static_cast<float>(inputCtxLen)/tensorColRatio);
226}
227
228static uint32_t GetOutputInnerLen(const arm::app::Model& model,
229 const uint32_t outputCtxLen)
230{
231 constexpr uint32_t ms_outputRowsIdx = arm::app::Wav2LetterModel::ms_outputRowsIdx;
232 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
233 const uint32_t outputRows = std::max(outputTensor->dims->data[ms_outputRowsIdx], 0);
234 return (outputRows - (2 * outputCtxLen));
235}