alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 17 | #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 Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 21 | #include "log_macros.h" /* Logging functions */ |
| 22 | #include "BufAttributes.hpp" /* Buffer attributes to be applied */ |
| 23 | |
| 24 | namespace arm { |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 25 | namespace 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 Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 32 | } /* namespace arm */ |
| 33 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 34 | enum 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 | |
| 43 | static void DisplayMenu() |
| 44 | { |
Kshitij Sisodia | 3c8256d | 2021-05-24 16:12:40 +0100 | [diff] [blame] | 45 | printf("\n"); |
| 46 | printf("User input required\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 47 | 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 Gekov | 93e5951 | 2021-08-03 11:18:41 +0100 | [diff] [blame] | 54 | fflush(stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 57 | void main_loop() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 58 | { |
| 59 | arm::app::AdModel model; /* Model wrapper object. */ |
| 60 | |
| 61 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 62 | if (!model.Init(arm::app::tensorArena, |
| 63 | sizeof(arm::app::tensorArena), |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 64 | arm::app::ad::GetModelPointer(), |
| 65 | arm::app::ad::GetModelLen())) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 66 | printf_err("failed to initialise model\n"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | /* Instantiate application context. */ |
| 71 | arm::app::ApplicationContext caseContext; |
| 72 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 73 | arm::app::Profiler profiler{"ad"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 74 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 75 | caseContext.Set<arm::app::Model&>("model", model); |
| 76 | caseContext.Set<uint32_t>("clipIndex", 0); |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 77 | 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); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 81 | |
| 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 Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 91 | menuOption = arm::app::ReadUserInputAsInt(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | 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 Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 104 | fflush(stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 105 | auto audioIndex = static_cast<uint32_t>( |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 106 | arm::app::ReadUserInputAsInt()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 107 | 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 | } |