blob: e9f7b4e87f205b3e46d4c1444cbceec8c2cf936b [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burton4e002792022-05-04 09:45:02 +01002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 "InputFiles.hpp" /* For input data */
18#include "AdModel.hpp" /* Model class for running inference */
19#include "UseCaseCommonUtils.hpp" /* Utils functions */
20#include "UseCaseHandler.hpp" /* Handlers for different user options */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010021#include "log_macros.h" /* Logging functions */
22#include "BufAttributes.hpp" /* Buffer attributes to be applied */
23
24namespace arm {
25 namespace app {
26 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
27 } /* namespace app */
28} /* namespace arm */
29
30extern uint8_t* GetModelPointer();
31extern size_t GetModelLen();
alexander3c798932021-03-26 21:42:19 +000032
33enum opcodes
34{
35 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector */
36 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index */
37 MENU_OPT_RUN_INF_ALL, /* Run inference on all */
38 MENU_OPT_SHOW_MODEL_INFO, /* Show model info */
39 MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio signals */
40};
41
42static void DisplayMenu()
43{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010044 printf("\n");
45 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000046 printf("Enter option number from:\n\n");
47 printf(" %u. Classify next audio signal\n", MENU_OPT_RUN_INF_NEXT);
48 printf(" %u. Classify audio signal at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
49 printf(" %u. Run classification on all audio signals\n", MENU_OPT_RUN_INF_ALL);
50 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
51 printf(" %u. List audio signals\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
52 printf(" Choice: ");
George Gekov93e59512021-08-03 11:18:41 +010053 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000054}
55
56
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010057void main_loop()
alexander3c798932021-03-26 21:42:19 +000058{
59 arm::app::AdModel model; /* Model wrapper object. */
60
61 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010062 if (!model.Init(arm::app::tensorArena,
63 sizeof(arm::app::tensorArena),
64 GetModelPointer(),
65 GetModelLen()))
alexander3c798932021-03-26 21:42:19 +000066 {
67 printf_err("failed to initialise model\n");
68 return;
69 }
70
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010071#if !defined(ARM_NPU)
72 /* If it is not a NPU build check if the model contains a NPU operator */
73 if (model.ContainsEthosUOperator()) {
74 printf_err("No driver support for Ethos-U operator found in the model.\n");
75 return;
76 }
77#endif /* ARM_NPU */
78
alexander3c798932021-03-26 21:42:19 +000079 /* Instantiate application context. */
80 arm::app::ApplicationContext caseContext;
81
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010082 arm::app::Profiler profiler{"ad"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010083 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000084 caseContext.Set<arm::app::Model&>("model", model);
85 caseContext.Set<uint32_t>("clipIndex", 0);
Richard Burton4e002792022-05-04 09:45:02 +010086 caseContext.Set<uint32_t>("frameLength", g_FrameLength);
87 caseContext.Set<uint32_t>("frameStride", g_FrameStride);
alexander3c798932021-03-26 21:42:19 +000088 caseContext.Set<float>("scoreThreshold", g_ScoreThreshold);
89 caseContext.Set<float>("trainingMean", g_TrainingMean);
90
91 /* Main program loop. */
92 bool executionSuccessful = true;
93 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
94
95 /* Loop. */
96 do {
97 int menuOption = MENU_OPT_RUN_INF_NEXT;
98 if (bUseMenu) {
99 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100100 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +0000101 printf("\n");
102 }
103 switch (menuOption) {
104 case MENU_OPT_RUN_INF_NEXT:
105 executionSuccessful = ClassifyVibrationHandler(
106 caseContext,
107 caseContext.Get<uint32_t>("clipIndex"),
108 false);
109 break;
110 case MENU_OPT_RUN_INF_CHOSEN: {
111 printf(" Enter the data index [0, %d]: ",
112 NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100113 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +0000114 auto audioIndex = static_cast<uint32_t>(
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100115 arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +0000116 executionSuccessful = ClassifyVibrationHandler(caseContext,
117 audioIndex,
118 false);
119 break;
120 }
121 case MENU_OPT_RUN_INF_ALL:
122 executionSuccessful = ClassifyVibrationHandler(
123 caseContext,
124 caseContext.Get<uint32_t>("clipIndex"),
125 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");
139}