alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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 "hal.h" /* Brings in platform definitions */ |
| 18 | #include "InputFiles.hpp" /* For input data */ |
| 19 | #include "AdModel.hpp" /* Model class for running inference */ |
| 20 | #include "UseCaseCommonUtils.hpp" /* Utils functions */ |
| 21 | #include "UseCaseHandler.hpp" /* Handlers for different user options */ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 22 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 23 | |
| 24 | enum opcodes |
| 25 | { |
| 26 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector */ |
| 27 | MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index */ |
| 28 | MENU_OPT_RUN_INF_ALL, /* Run inference on all */ |
| 29 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info */ |
| 30 | MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio signals */ |
| 31 | }; |
| 32 | |
| 33 | static void DisplayMenu() |
| 34 | { |
Kshitij Sisodia | 3c8256d | 2021-05-24 16:12:40 +0100 | [diff] [blame] | 35 | printf("\n"); |
| 36 | printf("User input required\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 37 | printf("Enter option number from:\n\n"); |
| 38 | printf(" %u. Classify next audio signal\n", MENU_OPT_RUN_INF_NEXT); |
| 39 | printf(" %u. Classify audio signal at chosen index\n", MENU_OPT_RUN_INF_CHOSEN); |
| 40 | printf(" %u. Run classification on all audio signals\n", MENU_OPT_RUN_INF_ALL); |
| 41 | printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO); |
| 42 | printf(" %u. List audio signals\n\n", MENU_OPT_LIST_AUDIO_CLIPS); |
| 43 | printf(" Choice: "); |
George Gekov | 93e5951 | 2021-08-03 11:18:41 +0100 | [diff] [blame] | 44 | fflush(stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | |
| 48 | void main_loop(hal_platform& platform) |
| 49 | { |
| 50 | arm::app::AdModel model; /* Model wrapper object. */ |
| 51 | |
| 52 | /* Load the model. */ |
| 53 | if (!model.Init()) |
| 54 | { |
| 55 | printf_err("failed to initialise model\n"); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | /* Instantiate application context. */ |
| 60 | arm::app::ApplicationContext caseContext; |
| 61 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 62 | arm::app::Profiler profiler{&platform, "ad"}; |
| 63 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 64 | caseContext.Set<hal_platform&>("platform", platform); |
| 65 | caseContext.Set<arm::app::Model&>("model", model); |
| 66 | caseContext.Set<uint32_t>("clipIndex", 0); |
| 67 | caseContext.Set<int>("frameLength", g_FrameLength); |
| 68 | caseContext.Set<int>("frameStride", g_FrameStride); |
| 69 | caseContext.Set<float>("scoreThreshold", g_ScoreThreshold); |
| 70 | caseContext.Set<float>("trainingMean", g_TrainingMean); |
| 71 | |
| 72 | /* Main program loop. */ |
| 73 | bool executionSuccessful = true; |
| 74 | constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; |
| 75 | |
| 76 | /* Loop. */ |
| 77 | do { |
| 78 | int menuOption = MENU_OPT_RUN_INF_NEXT; |
| 79 | if (bUseMenu) { |
| 80 | DisplayMenu(); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame^] | 81 | menuOption = arm::app::ReadUserInputAsInt(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 82 | printf("\n"); |
| 83 | } |
| 84 | switch (menuOption) { |
| 85 | case MENU_OPT_RUN_INF_NEXT: |
| 86 | executionSuccessful = ClassifyVibrationHandler( |
| 87 | caseContext, |
| 88 | caseContext.Get<uint32_t>("clipIndex"), |
| 89 | false); |
| 90 | break; |
| 91 | case MENU_OPT_RUN_INF_CHOSEN: { |
| 92 | printf(" Enter the data index [0, %d]: ", |
| 93 | NUMBER_OF_FILES-1); |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 94 | fflush(stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 95 | auto audioIndex = static_cast<uint32_t>( |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame^] | 96 | arm::app::ReadUserInputAsInt()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 97 | executionSuccessful = ClassifyVibrationHandler(caseContext, |
| 98 | audioIndex, |
| 99 | false); |
| 100 | break; |
| 101 | } |
| 102 | case MENU_OPT_RUN_INF_ALL: |
| 103 | executionSuccessful = ClassifyVibrationHandler( |
| 104 | caseContext, |
| 105 | caseContext.Get<uint32_t>("clipIndex"), |
| 106 | true); |
| 107 | break; |
| 108 | case MENU_OPT_SHOW_MODEL_INFO: |
| 109 | executionSuccessful = model.ShowModelInfoHandler(); |
| 110 | break; |
| 111 | case MENU_OPT_LIST_AUDIO_CLIPS: |
| 112 | executionSuccessful = ListFilesHandler(caseContext); |
| 113 | break; |
| 114 | default: |
| 115 | printf("Incorrect choice, try again."); |
| 116 | break; |
| 117 | } |
| 118 | } while (executionSuccessful && bUseMenu); |
| 119 | info("Main loop terminated.\n"); |
| 120 | } |