blob: 30cb084a53676421cd15173b0f6a690e267a1add [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 "InputFiles.hpp" /* For input images. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000019#include "Labels_micronetkws.hpp" /* For MicroNetKws label strings. */
alexander3c798932021-03-26 21:42:19 +000020#include "Labels_wav2letter.hpp" /* For Wav2Letter label strings. */
21#include "Classifier.hpp" /* KWS classifier. */
22#include "AsrClassifier.hpp" /* ASR classifier. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000023#include "MicroNetKwsModel.hpp" /* KWS model class for running inference. */
alexander3c798932021-03-26 21:42:19 +000024#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
30using KwsClassifier = arm::app::Classifier;
31
32enum 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
41static void DisplayMenu()
42{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010043 printf("\n\n");
44 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000045 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 Gekov93e59512021-08-03 11:18:41 +010052 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000053}
54
55/** @brief Gets the number of MFCC features for a single window. */
56static uint32_t GetNumMfccFeatures(const arm::app::Model& model);
57
58/** @brief Gets the number of MFCC feature vectors to be computed. */
59static uint32_t GetNumMfccFeatureVectors(const arm::app::Model& model);
60
61/** @brief Gets the output context length (left and right) for post-processing. */
62static uint32_t GetOutputContextLen(const arm::app::Model& model,
63 uint32_t inputCtxLen);
64
65/** @brief Gets the output inner length for post-processing. */
66static uint32_t GetOutputInnerLen(const arm::app::Model& model,
67 uint32_t outputCtxLen);
68
69void main_loop(hal_platform& platform)
70{
71 /* Model wrapper objects. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000072 arm::app::MicroNetKwsModel kwsModel;
alexander3c798932021-03-26 21:42:19 +000073 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())) {
Kshitij Sisodia76a15802021-12-24 11:05:11 +000084 printf_err("Failed to initialise ASR model\n");
alexander3c798932021-03-26 21:42:19 +000085 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 Gottardi8df12f32021-04-07 17:15:31 +0100106 arm::app::Profiler profiler{&platform, "kws_asr"};
107 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
108
alexander3c798932021-03-26 21:42:19 +0000109 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. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000140 caseContext.Set<uint32_t>("keywordindex", 9 );
alexander3c798932021-03-26 21:42:19 +0000141
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);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100164 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +0000165 auto clipIndex = static_cast<uint32_t>(
166 arm::app::ReadUserInputAsInt(platform));
167 executionSuccessful = ClassifyAudioHandler(caseContext,
168 clipIndex,
169 false);
170 break;
171 }
172 case MENU_OPT_RUN_INF_ALL:
173 executionSuccessful = ClassifyAudioHandler(
174 caseContext,
175 caseContext.Get<uint32_t>("clipIndex"),
176 true);
177 break;
178 case MENU_OPT_SHOW_MODEL_INFO:
179 executionSuccessful = kwsModel.ShowModelInfoHandler();
180 executionSuccessful = asrModel.ShowModelInfoHandler();
181 break;
182 case MENU_OPT_LIST_AUDIO_CLIPS:
183 executionSuccessful = ListFilesHandler(caseContext);
184 break;
185 default:
186 printf("Incorrect choice, try again.");
187 break;
188 }
189 } while (executionSuccessful && bUseMenu);
190 info("Main loop terminated.\n");
191}
192
193static uint32_t GetNumMfccFeatures(const arm::app::Model& model)
194{
195 TfLiteTensor* inputTensor = model.GetInputTensor(0);
196 const int inputCols = inputTensor->dims->data[arm::app::Wav2LetterModel::ms_inputColsIdx];
197 if (0 != inputCols % 3) {
198 printf_err("Number of input columns is not a multiple of 3\n");
199 }
200 return std::max(inputCols/3, 0);
201}
202
203static uint32_t GetNumMfccFeatureVectors(const arm::app::Model& model)
204{
205 TfLiteTensor* inputTensor = model.GetInputTensor(0);
206 const int inputRows = inputTensor->dims->data[arm::app::Wav2LetterModel::ms_inputRowsIdx];
207 return std::max(inputRows, 0);
208}
209
210static uint32_t GetOutputContextLen(const arm::app::Model& model, const uint32_t inputCtxLen)
211{
212 const uint32_t inputRows = GetNumMfccFeatureVectors(model);
213 const uint32_t inputInnerLen = inputRows - (2 * inputCtxLen);
214 constexpr uint32_t ms_outputRowsIdx = arm::app::Wav2LetterModel::ms_outputRowsIdx;
215
216 /* Check to make sure that the input tensor supports the above context and inner lengths. */
217 if (inputRows <= 2 * inputCtxLen || inputRows <= inputInnerLen) {
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100218 printf_err("Input rows not compatible with ctx of %" PRIu32 "\n",
alexander3c798932021-03-26 21:42:19 +0000219 inputCtxLen);
220 return 0;
221 }
222
223 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
224 const uint32_t outputRows = std::max(outputTensor->dims->data[ms_outputRowsIdx], 0);
225
226 const float tensorColRatio = static_cast<float>(inputRows)/
227 static_cast<float>(outputRows);
228
229 return std::round(static_cast<float>(inputCtxLen)/tensorColRatio);
230}
231
232static uint32_t GetOutputInnerLen(const arm::app::Model& model,
233 const uint32_t outputCtxLen)
234{
235 constexpr uint32_t ms_outputRowsIdx = arm::app::Wav2LetterModel::ms_outputRowsIdx;
236 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
237 const uint32_t outputRows = std::max(outputTensor->dims->data[ms_outputRowsIdx], 0);
238 return (outputRows - (2 * outputCtxLen));
239}