blob: 4e5edc17effca3ff05c20982b7d82bf3db584349 [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 {
Liam Barry213a5432022-05-09 17:06:19 +010025namespace app {
26 namespace ad {
27 extern uint8_t* GetModelPointer();
28 extern size_t GetModelLen();
29 } /* namespace ad */
30 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
31} /* namespace app */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010032} /* namespace arm */
33
alexander3c798932021-03-26 21:42:19 +000034enum opcodes
35{
36 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector */
37 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index */
38 MENU_OPT_RUN_INF_ALL, /* Run inference on all */
39 MENU_OPT_SHOW_MODEL_INFO, /* Show model info */
40 MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio signals */
41};
42
43static void DisplayMenu()
44{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010045 printf("\n");
46 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000047 printf("Enter option number from:\n\n");
48 printf(" %u. Classify next audio signal\n", MENU_OPT_RUN_INF_NEXT);
49 printf(" %u. Classify audio signal at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
50 printf(" %u. Run classification on all audio signals\n", MENU_OPT_RUN_INF_ALL);
51 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
52 printf(" %u. List audio signals\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
53 printf(" Choice: ");
George Gekov93e59512021-08-03 11:18:41 +010054 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000055}
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),
Liam Barry213a5432022-05-09 17:06:19 +010064 arm::app::ad::GetModelPointer(),
65 arm::app::ad::GetModelLen())) {
alexander3c798932021-03-26 21:42:19 +000066 printf_err("failed to initialise model\n");
67 return;
68 }
69
70 /* Instantiate application context. */
71 arm::app::ApplicationContext caseContext;
72
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010073 arm::app::Profiler profiler{"ad"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010074 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000075 caseContext.Set<arm::app::Model&>("model", model);
76 caseContext.Set<uint32_t>("clipIndex", 0);
Liam Barry213a5432022-05-09 17:06:19 +010077 caseContext.Set<uint32_t>("frameLength", arm::app::ad::g_FrameLength);
78 caseContext.Set<uint32_t>("frameStride", arm::app::ad::g_FrameStride);
79 caseContext.Set<float>("scoreThreshold", arm::app::ad::g_ScoreThreshold);
80 caseContext.Set<float>("trainingMean", arm::app::ad::g_TrainingMean);
alexander3c798932021-03-26 21:42:19 +000081
82 /* Main program loop. */
83 bool executionSuccessful = true;
84 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
85
86 /* Loop. */
87 do {
88 int menuOption = MENU_OPT_RUN_INF_NEXT;
89 if (bUseMenu) {
90 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010091 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +000092 printf("\n");
93 }
94 switch (menuOption) {
95 case MENU_OPT_RUN_INF_NEXT:
96 executionSuccessful = ClassifyVibrationHandler(
97 caseContext,
98 caseContext.Get<uint32_t>("clipIndex"),
99 false);
100 break;
101 case MENU_OPT_RUN_INF_CHOSEN: {
102 printf(" Enter the data index [0, %d]: ",
103 NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100104 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +0000105 auto audioIndex = static_cast<uint32_t>(
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100106 arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +0000107 executionSuccessful = ClassifyVibrationHandler(caseContext,
108 audioIndex,
109 false);
110 break;
111 }
112 case MENU_OPT_RUN_INF_ALL:
113 executionSuccessful = ClassifyVibrationHandler(
114 caseContext,
115 caseContext.Get<uint32_t>("clipIndex"),
116 true);
117 break;
118 case MENU_OPT_SHOW_MODEL_INFO:
119 executionSuccessful = model.ShowModelInfoHandler();
120 break;
121 case MENU_OPT_LIST_AUDIO_CLIPS:
122 executionSuccessful = ListFilesHandler(caseContext);
123 break;
124 default:
125 printf("Incorrect choice, try again.");
126 break;
127 }
128 } while (executionSuccessful && bUseMenu);
129 info("Main loop terminated.\n");
130}