blob: 290c41c987522f79cc168f2aff6210f11b9a2eb7 [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 {
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010028 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
Liam Barry213a5432022-05-09 17:06:19 +010029 namespace asr {
30 extern uint8_t* GetModelPointer();
31 extern size_t GetModelLen();
32 } /* namespace asr */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010033} /* 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. */
Liam Barry213a5432022-05-09 17:06:19 +010067 if (!model.Init(arm::app::tensorArena,
68 sizeof(arm::app::tensorArena),
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010069 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
alexander3c798932021-03-26 21:42:19 +000078 /* Instantiate application context. */
79 arm::app::ApplicationContext caseContext;
80 std::vector <std::string> labels;
81 GetLabelsVector(labels);
82 arm::app::AsrClassifier classifier; /* Classifier wrapper object. */
83
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010084 arm::app::Profiler profiler{"asr"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010085 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000086 caseContext.Set<arm::app::Model&>("model", model);
87 caseContext.Set<uint32_t>("clipIndex", 0);
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010088 caseContext.Set<uint32_t>("frameLength", arm::app::asr::g_FrameLength);
89 caseContext.Set<uint32_t>("frameStride", arm::app::asr::g_FrameStride);
90 caseContext.Set<float>("scoreThreshold", arm::app::asr::g_ScoreThreshold); /* Score threshold. */
91 caseContext.Set<uint32_t>("ctxLen", arm::app::asr::g_ctxLen); /* Left and right context length (MFCC feat vectors). */
alexander3c798932021-03-26 21:42:19 +000092 caseContext.Set<const std::vector <std::string>&>("labels", labels);
93 caseContext.Set<arm::app::AsrClassifier&>("classifier", classifier);
alexander3c798932021-03-26 21:42:19 +000094
95 bool executionSuccessful = true;
96 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
97
98 /* Loop. */
99 do {
100 int menuOption = MENU_OPT_RUN_INF_NEXT;
101 if (bUseMenu) {
102 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100103 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +0000104 printf("\n");
105 }
106 switch (menuOption) {
107 case MENU_OPT_RUN_INF_NEXT:
108 executionSuccessful = ClassifyAudioHandler(
109 caseContext,
110 caseContext.Get<uint32_t>("clipIndex"),
111 false);
112 break;
113 case MENU_OPT_RUN_INF_CHOSEN: {
114 printf(" Enter the audio clip index [0, %d]: ",
115 NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100116 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +0000117 auto clipIndex = static_cast<uint32_t>(
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100118 arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +0000119 executionSuccessful = ClassifyAudioHandler(caseContext,
120 clipIndex,
121 false);
122 break;
123 }
124 case MENU_OPT_RUN_INF_ALL:
125 executionSuccessful = ClassifyAudioHandler(
126 caseContext,
127 caseContext.Get<uint32_t>("clipIndex"),
128 true);
129 break;
130 case MENU_OPT_SHOW_MODEL_INFO:
131 executionSuccessful = model.ShowModelInfoHandler();
132 break;
133 case MENU_OPT_LIST_AUDIO_CLIPS:
134 executionSuccessful = ListFilesHandler(caseContext);
135 break;
136 default:
137 printf("Incorrect choice, try again.");
138 break;
139 }
140 } while (executionSuccessful && bUseMenu);
141 info("Main loop terminated.\n");
142}
143
144static bool VerifyTensorDimensions(const arm::app::Model& model)
145{
146 /* Populate tensor related parameters. */
147 TfLiteTensor* inputTensor = model.GetInputTensor(0);
148 if (!inputTensor->dims) {
149 printf_err("Invalid input tensor dims\n");
150 return false;
151 } else if (inputTensor->dims->size < 3) {
152 printf_err("Input tensor dimension should be >= 3\n");
153 return false;
154 }
155
156 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
157 if (!outputTensor->dims) {
158 printf_err("Invalid output tensor dims\n");
159 return false;
160 } else if (outputTensor->dims->size < 3) {
161 printf_err("Output tensor dimension should be >= 3\n");
162 return false;
163 }
164
165 return true;
166}