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