blob: 7acd3194a2661da0903b797fbd406da6012b0c52 [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 */
alexander3c798932021-03-26 21:42:19 +000017#include "Labels.hpp" /* For label strings. */
18#include "UseCaseHandler.hpp" /* Handlers for different user options. */
19#include "Wav2LetterModel.hpp" /* Model class for running inference. */
20#include "UseCaseCommonUtils.hpp" /* Utils functions. */
21#include "AsrClassifier.hpp" /* Classifier. */
22#include "InputFiles.hpp" /* Generated audio clip header. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010023#include "log_macros.h" /* Logging functions */
24#include "BufAttributes.hpp" /* Buffer attributes to be applied */
25
26namespace arm {
27namespace app {
28namespace asr {
29 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
30 extern uint8_t* GetModelPointer();
31 extern size_t GetModelLen();
32} /* namespace asr */
33} /* namespace app */
34} /* namespace arm */
alexander3c798932021-03-26 21:42:19 +000035
36enum opcodes
37{
38 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
39 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
40 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
41 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
42 MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clips. */
43};
44
45static void DisplayMenu()
46{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010047 printf("\n\n");
48 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000049 printf("Enter option number from:\n\n");
50 printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT);
51 printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
52 printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL);
53 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
54 printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
55 printf(" Choice: ");
George Gekov93e59512021-08-03 11:18:41 +010056 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000057}
58
Richard Burtonc2911442022-04-22 09:08:21 +010059/** @brief Verify input and output tensor are of certain min dimensions. */
alexander3c798932021-03-26 21:42:19 +000060static bool VerifyTensorDimensions(const arm::app::Model& model);
61
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010062void main_loop()
alexander3c798932021-03-26 21:42:19 +000063{
64 arm::app::Wav2LetterModel model; /* Model wrapper object. */
65
66 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010067 if (!model.Init(arm::app::asr::tensorArena,
68 sizeof(arm::app::asr::tensorArena),
69 arm::app::asr::GetModelPointer(),
70 arm::app::asr::GetModelLen())) {
alexander3c798932021-03-26 21:42:19 +000071 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
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010078#if !defined(ARM_NPU)
79 /* If it is not a NPU build check if the model contains a NPU operator */
80 if (model.ContainsEthosUOperator()) {
81 printf_err("No driver support for Ethos-U operator found in the model.\n");
82 return;
83 }
84#endif /* ARM_NPU */
85
alexander3c798932021-03-26 21:42:19 +000086 /* Instantiate application context. */
87 arm::app::ApplicationContext caseContext;
88 std::vector <std::string> labels;
89 GetLabelsVector(labels);
90 arm::app::AsrClassifier classifier; /* Classifier wrapper object. */
91
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010092 arm::app::Profiler profiler{"asr"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010093 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000094 caseContext.Set<arm::app::Model&>("model", model);
95 caseContext.Set<uint32_t>("clipIndex", 0);
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010096 caseContext.Set<uint32_t>("frameLength", arm::app::asr::g_FrameLength);
97 caseContext.Set<uint32_t>("frameStride", arm::app::asr::g_FrameStride);
98 caseContext.Set<float>("scoreThreshold", arm::app::asr::g_ScoreThreshold); /* Score threshold. */
99 caseContext.Set<uint32_t>("ctxLen", arm::app::asr::g_ctxLen); /* Left and right context length (MFCC feat vectors). */
alexander3c798932021-03-26 21:42:19 +0000100 caseContext.Set<const std::vector <std::string>&>("labels", labels);
101 caseContext.Set<arm::app::AsrClassifier&>("classifier", classifier);
alexander3c798932021-03-26 21:42:19 +0000102
103 bool executionSuccessful = true;
104 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
105
106 /* Loop. */
107 do {
108 int menuOption = MENU_OPT_RUN_INF_NEXT;
109 if (bUseMenu) {
110 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100111 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +0000112 printf("\n");
113 }
114 switch (menuOption) {
115 case MENU_OPT_RUN_INF_NEXT:
116 executionSuccessful = ClassifyAudioHandler(
117 caseContext,
118 caseContext.Get<uint32_t>("clipIndex"),
119 false);
120 break;
121 case MENU_OPT_RUN_INF_CHOSEN: {
122 printf(" Enter the audio clip index [0, %d]: ",
123 NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100124 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +0000125 auto clipIndex = static_cast<uint32_t>(
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100126 arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +0000127 executionSuccessful = ClassifyAudioHandler(caseContext,
128 clipIndex,
129 false);
130 break;
131 }
132 case MENU_OPT_RUN_INF_ALL:
133 executionSuccessful = ClassifyAudioHandler(
134 caseContext,
135 caseContext.Get<uint32_t>("clipIndex"),
136 true);
137 break;
138 case MENU_OPT_SHOW_MODEL_INFO:
139 executionSuccessful = model.ShowModelInfoHandler();
140 break;
141 case MENU_OPT_LIST_AUDIO_CLIPS:
142 executionSuccessful = ListFilesHandler(caseContext);
143 break;
144 default:
145 printf("Incorrect choice, try again.");
146 break;
147 }
148 } while (executionSuccessful && bUseMenu);
149 info("Main loop terminated.\n");
150}
151
152static bool VerifyTensorDimensions(const arm::app::Model& model)
153{
154 /* Populate tensor related parameters. */
155 TfLiteTensor* inputTensor = model.GetInputTensor(0);
156 if (!inputTensor->dims) {
157 printf_err("Invalid input tensor dims\n");
158 return false;
159 } else if (inputTensor->dims->size < 3) {
160 printf_err("Input tensor dimension should be >= 3\n");
161 return false;
162 }
163
164 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
165 if (!outputTensor->dims) {
166 printf_err("Invalid output tensor dims\n");
167 return false;
168 } else if (outputTensor->dims->size < 3) {
169 printf_err("Output tensor dimension should be >= 3\n");
170 return false;
171 }
172
173 return true;
174}