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 "InputFiles.hpp" /* For input audio clips. */ |
| 18 | #include "Classifier.hpp" /* Classifier. */ |
| 19 | #include "DsCnnModel.hpp" /* Model class for running inference. */ |
| 20 | #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. */ |
| 24 | |
| 25 | using KwsClassifier = arm::app::Classifier; |
| 26 | |
| 27 | enum opcodes |
| 28 | { |
| 29 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */ |
| 30 | MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */ |
| 31 | MENU_OPT_RUN_INF_ALL, /* Run inference on all. */ |
| 32 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */ |
| 33 | MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio clips. */ |
| 34 | }; |
| 35 | |
| 36 | static void DisplayMenu() |
| 37 | { |
| 38 | printf("\n\nUser input required\n"); |
| 39 | printf("Enter option number from:\n\n"); |
| 40 | printf(" %u. Classify next audio clip\n", MENU_OPT_RUN_INF_NEXT); |
| 41 | printf(" %u. Classify audio clip at chosen index\n", MENU_OPT_RUN_INF_CHOSEN); |
| 42 | printf(" %u. Run classification on all audio clips\n", MENU_OPT_RUN_INF_ALL); |
| 43 | printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO); |
| 44 | printf(" %u. List audio clips\n\n", MENU_OPT_LIST_AUDIO_CLIPS); |
| 45 | printf(" Choice: "); |
| 46 | } |
| 47 | |
| 48 | void main_loop(hal_platform& platform) |
| 49 | { |
| 50 | arm::app::DsCnnModel model; /* Model wrapper object. */ |
| 51 | |
| 52 | /* Load the model. */ |
| 53 | if (!model.Init()) { |
| 54 | printf_err("Failed to initialise model\n"); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | /* Instantiate application context. */ |
| 59 | arm::app::ApplicationContext caseContext; |
| 60 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame^] | 61 | arm::app::Profiler profiler{&platform, "kws"}; |
| 62 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
| 63 | |
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); /* Normalised score threshold. */ |
| 70 | |
| 71 | KwsClassifier classifier; /* classifier wrapper object. */ |
| 72 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 73 | |
| 74 | std::vector <std::string> labels; |
| 75 | GetLabelsVector(labels); |
| 76 | |
| 77 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 78 | |
| 79 | bool executionSuccessful = true; |
| 80 | constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; |
| 81 | |
| 82 | /* Loop. */ |
| 83 | do { |
| 84 | int menuOption = MENU_OPT_RUN_INF_NEXT; |
| 85 | if (bUseMenu) { |
| 86 | DisplayMenu(); |
| 87 | menuOption = arm::app::ReadUserInputAsInt(platform); |
| 88 | printf("\n"); |
| 89 | } |
| 90 | switch (menuOption) { |
| 91 | case MENU_OPT_RUN_INF_NEXT: |
| 92 | executionSuccessful = ClassifyAudioHandler(caseContext, caseContext.Get<uint32_t>("clipIndex"), false); |
| 93 | break; |
| 94 | case MENU_OPT_RUN_INF_CHOSEN: { |
| 95 | printf(" Enter the audio clip index [0, %d]: ", NUMBER_OF_FILES-1); |
| 96 | auto clipIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt(platform)); |
| 97 | executionSuccessful = ClassifyAudioHandler(caseContext, clipIndex, false); |
| 98 | break; |
| 99 | } |
| 100 | case MENU_OPT_RUN_INF_ALL: |
| 101 | executionSuccessful = ClassifyAudioHandler(caseContext,caseContext.Get<uint32_t>("clipIndex"), true); |
| 102 | break; |
| 103 | case MENU_OPT_SHOW_MODEL_INFO: |
| 104 | executionSuccessful = model.ShowModelInfoHandler(); |
| 105 | break; |
| 106 | case MENU_OPT_LIST_AUDIO_CLIPS: |
| 107 | executionSuccessful = ListFilesHandler(caseContext); |
| 108 | break; |
| 109 | default: |
| 110 | printf("Incorrect choice, try again."); |
| 111 | break; |
| 112 | } |
| 113 | } while (executionSuccessful && bUseMenu); |
| 114 | info("Main loop terminated.\n"); |
| 115 | } |