blob: 0d8fb5418e279085a30c06f5d259fe7a23d7d5a4 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
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 */
17#include "InputFiles.hpp" /* For input audio clips. */
Richard Burtonec5e99b2022-10-05 11:00:37 +010018#include "KwsClassifier.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 {
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010029 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
Liam Barry213a5432022-05-09 17:06:19 +010030 namespace kws {
31 extern uint8_t* GetModelPointer();
32 extern size_t GetModelLen();
33 } /* namespace kws */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010034} /* namespace app */
35} /* namespace arm */
alexander3c798932021-03-26 21:42:19 +000036
alexander3c798932021-03-26 21:42:19 +000037
38enum opcodes
39{
40 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
41 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
42 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
43 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
44 MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clips. */
45};
46
47static void DisplayMenu()
48{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010049 printf("\n\n");
50 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000051 printf("Enter option number from:\n\n");
52 printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT);
53 printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
54 printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL);
55 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
56 printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS);
57 printf(" Choice: ");
George Gekov93e59512021-08-03 11:18:41 +010058 fflush(stdout);
alexander3c798932021-03-26 21:42:19 +000059}
60
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010061void main_loop()
alexander3c798932021-03-26 21:42:19 +000062{
Kshitij Sisodia76a15802021-12-24 11:05:11 +000063 arm::app::MicroNetKwsModel model; /* Model wrapper object. */
alexander3c798932021-03-26 21:42:19 +000064
65 /* Load the model. */
Liam Barry213a5432022-05-09 17:06:19 +010066 if (!model.Init(arm::app::tensorArena,
67 sizeof(arm::app::tensorArena),
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010068 arm::app::kws::GetModelPointer(),
69 arm::app::kws::GetModelLen())) {
alexander3c798932021-03-26 21:42:19 +000070 printf_err("Failed to initialise model\n");
71 return;
72 }
73
74 /* Instantiate application context. */
75 arm::app::ApplicationContext caseContext;
76
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010077 arm::app::Profiler profiler{"kws"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010078 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000079 caseContext.Set<arm::app::Model&>("model", model);
80 caseContext.Set<uint32_t>("clipIndex", 0);
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010081 caseContext.Set<int>("frameLength", arm::app::kws::g_FrameLength);
82 caseContext.Set<int>("frameStride", arm::app::kws::g_FrameStride);
83 caseContext.Set<float>("scoreThreshold", arm::app::kws::g_ScoreThreshold); /* Normalised score threshold. */
alexander3c798932021-03-26 21:42:19 +000084
Richard Burtonec5e99b2022-10-05 11:00:37 +010085 arm::app::KwsClassifier classifier; /* classifier wrapper object. */
86 caseContext.Set<arm::app::KwsClassifier&>("classifier", classifier);
alexander3c798932021-03-26 21:42:19 +000087
88 std::vector <std::string> labels;
89 GetLabelsVector(labels);
90
91 caseContext.Set<const std::vector <std::string>&>("labels", labels);
92
93 bool executionSuccessful = true;
94 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
95
96 /* Loop. */
97 do {
98 int menuOption = MENU_OPT_RUN_INF_NEXT;
99 if (bUseMenu) {
100 DisplayMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100101 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +0000102 printf("\n");
103 }
104 switch (menuOption) {
105 case MENU_OPT_RUN_INF_NEXT:
106 executionSuccessful = ClassifyAudioHandler(caseContext, caseContext.Get<uint32_t>("clipIndex"), false);
107 break;
108 case MENU_OPT_RUN_INF_CHOSEN: {
109 printf(" Enter the audio clip index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100110 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100111 auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +0000112 executionSuccessful = ClassifyAudioHandler(caseContext, clipIndex, false);
113 break;
114 }
115 case MENU_OPT_RUN_INF_ALL:
116 executionSuccessful = ClassifyAudioHandler(caseContext,caseContext.Get<uint32_t>("clipIndex"), 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");
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100130}