blob: 550e7a1f28869fb1abc45fbb194193f50f62796d [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
75 /* Instantiate application context. */
76 arm::app::ApplicationContext caseContext;
77
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010078 arm::app::Profiler profiler{"kws"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010079 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000080 caseContext.Set<arm::app::Model&>("model", model);
81 caseContext.Set<uint32_t>("clipIndex", 0);
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010082 caseContext.Set<int>("frameLength", arm::app::kws::g_FrameLength);
83 caseContext.Set<int>("frameStride", arm::app::kws::g_FrameStride);
84 caseContext.Set<float>("scoreThreshold", arm::app::kws::g_ScoreThreshold); /* Normalised score threshold. */
alexander3c798932021-03-26 21:42:19 +000085
86 KwsClassifier classifier; /* classifier wrapper object. */
87 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
88
89 std::vector <std::string> labels;
90 GetLabelsVector(labels);
91
92 caseContext.Set<const std::vector <std::string>&>("labels", labels);
93
94 bool executionSuccessful = true;
95 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
96
97 /* Loop. */
98 do {
99 int menuOption = MENU_OPT_RUN_INF_NEXT;
100 if (bUseMenu) {
101 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100102 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +0000103 printf("\n");
104 }
105 switch (menuOption) {
106 case MENU_OPT_RUN_INF_NEXT:
107 executionSuccessful = ClassifyAudioHandler(caseContext, caseContext.Get<uint32_t>("clipIndex"), false);
108 break;
109 case MENU_OPT_RUN_INF_CHOSEN: {
110 printf(" Enter the audio clip index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100111 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100112 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +0000113 executionSuccessful = ClassifyAudioHandler(caseContext, clipIndex, false);
114 break;
115 }
116 case MENU_OPT_RUN_INF_ALL:
117 executionSuccessful = ClassifyAudioHandler(caseContext,caseContext.Get<uint32_t>("clipIndex"), true);
118 break;
119 case MENU_OPT_SHOW_MODEL_INFO:
120 executionSuccessful = model.ShowModelInfoHandler();
121 break;
122 case MENU_OPT_LIST_AUDIO_CLIPS:
123 executionSuccessful = ListFilesHandler(caseContext);
124 break;
125 default:
126 printf("Incorrect choice, try again.");
127 break;
128 }
129 } while (executionSuccessful && bUseMenu);
130 info("Main loop terminated.\n");
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100131}