blob: 3c35a7fe26b33869216c10f88102194d6d3aef14 [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 "InputFiles.hpp" /* For input audio clips. */
18#include "Classifier.hpp" /* Classifier. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000019#include "MicroNetKwsModel.hpp" /* Model class for running inference. */
alexander3c798932021-03-26 21:42:19 +000020#include "hal.h" /* Brings in platform definitions. */
21#include "Labels.hpp" /* For label strings. */
22#include "UseCaseHandler.hpp" /* Handlers for different user options. */
23#include "UseCaseCommonUtils.hpp" /* Utils functions. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010024#include "log_macros.h" /* Logging functions */
25#include "BufAttributes.hpp" /* Buffer attributes to be applied */
26
27namespace arm {
28namespace app {
29namespace kws {
30 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
31 extern uint8_t *GetModelPointer();
32 extern size_t GetModelLen();
33} /* namespace kws */
34} /* namespace app */
35} /* namespace arm */
alexander3c798932021-03-26 21:42:19 +000036
37using KwsClassifier = arm::app::Classifier;
38
39enum opcodes
40{
41 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
42 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
43 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
44 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
45 MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clips. */
46};
47
48static void DisplayMenu()
49{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010050 printf("\n\n");
51 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000052 printf("Enter option number from:\n\n");
53 printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT);
54 printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
55 printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL);
56 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
57 printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
58 printf(" Choice: ");
George Gekov93e59512021-08-03 11:18:41 +010059 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000060}
61
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010062void main_loop()
alexander3c798932021-03-26 21:42:19 +000063{
Kshitij Sisodia76a15802021-12-24 11:05:11 +000064 arm::app::MicroNetKwsModel model; /* Model wrapper object. */
alexander3c798932021-03-26 21:42:19 +000065
66 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010067 if (!model.Init(arm::app::kws::tensorArena,
68 sizeof(arm::app::kws::tensorArena),
69 arm::app::kws::GetModelPointer(),
70 arm::app::kws::GetModelLen())) {
alexander3c798932021-03-26 21:42:19 +000071 printf_err("Failed to initialise model\n");
72 return;
73 }
74
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010075#if !defined(ARM_NPU)
76 /* If it is not a NPU build check if the model contains a NPU operator */
77 if (model.ContainsEthosUOperator()) {
78 printf_err("No driver support for Ethos-U operator found in the model.\n");
79 return;
80 }
81#endif /* ARM_NPU */
82
alexander3c798932021-03-26 21:42:19 +000083 /* Instantiate application context. */
84 arm::app::ApplicationContext caseContext;
85
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010086 arm::app::Profiler profiler{"kws"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010087 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000088 caseContext.Set<arm::app::Model&>("model", model);
89 caseContext.Set<uint32_t>("clipIndex", 0);
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010090 caseContext.Set<int>("frameLength", arm::app::kws::g_FrameLength);
91 caseContext.Set<int>("frameStride", arm::app::kws::g_FrameStride);
92 caseContext.Set<float>("scoreThreshold", arm::app::kws::g_ScoreThreshold); /* Normalised score threshold. */
alexander3c798932021-03-26 21:42:19 +000093
94 KwsClassifier classifier; /* classifier wrapper object. */
95 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
96
97 std::vector <std::string> labels;
98 GetLabelsVector(labels);
99
100 caseContext.Set<const std::vector <std::string>&>("labels", labels);
101
102 bool executionSuccessful = true;
103 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
104
105 /* Loop. */
106 do {
107 int menuOption = MENU_OPT_RUN_INF_NEXT;
108 if (bUseMenu) {
109 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100110 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +0000111 printf("\n");
112 }
113 switch (menuOption) {
114 case MENU_OPT_RUN_INF_NEXT:
115 executionSuccessful = ClassifyAudioHandler(caseContext, caseContext.Get<uint32_t>("clipIndex"), false);
116 break;
117 case MENU_OPT_RUN_INF_CHOSEN: {
118 printf(" Enter the audio clip index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100119 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100120 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +0000121 executionSuccessful = ClassifyAudioHandler(caseContext, clipIndex, false);
122 break;
123 }
124 case MENU_OPT_RUN_INF_ALL:
125 executionSuccessful = ClassifyAudioHandler(caseContext,caseContext.Get<uint32_t>("clipIndex"), true);
126 break;
127 case MENU_OPT_SHOW_MODEL_INFO:
128 executionSuccessful = model.ShowModelInfoHandler();
129 break;
130 case MENU_OPT_LIST_AUDIO_CLIPS:
131 executionSuccessful = ListFilesHandler(caseContext);
132 break;
133 default:
134 printf("Incorrect choice, try again.");
135 break;
136 }
137 } while (executionSuccessful && bUseMenu);
138 info("Main loop terminated.\n");
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100139}