alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Liam Barry | e958850 | 2022-01-25 14:31:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
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 | */ |
| 17 | #ifndef USECASE_COMMON_UTILS_HPP |
| 18 | #define USECASE_COMMON_UTILS_HPP |
| 19 | |
| 20 | #include "hal.h" |
| 21 | #include "Model.hpp" |
| 22 | #include "AppContext.hpp" |
| 23 | #include "Profiler.hpp" |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 24 | #include "UseCaseHandler.hpp" /* Handlers for different user options. */ |
| 25 | #include "Classifier.hpp" /* Classifier. */ |
| 26 | #include "InputFiles.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 27 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 28 | |
| 29 | void DisplayCommonMenu(); |
| 30 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 31 | /** |
| 32 | * @brief Presents inference results using the data presentation |
| 33 | * object. |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 34 | * @param[in] results Vector of classification results to be displayed. |
| 35 | * @return true if successful, false otherwise. |
| 36 | **/ |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 37 | bool PresentInferenceResult(const std::vector<arm::app::ClassificationResult>& results); |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 38 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * @brief Helper function to increment current input feature vector index. |
| 42 | * @param[in,out] ctx Pointer to the application context object. |
| 43 | * @param[in] useCase Use case name |
| 44 | **/ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 45 | void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, const std::string& useCase); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * @brief Helper function to set the input feature map index. |
| 49 | * @param[in,out] ctx Pointer to the application context object. |
| 50 | * @param[in] idx Value to be set. |
| 51 | * @param[in] ctxIfmName Input Feature Map name |
| 52 | * @return true if index is set, false otherwise. |
| 53 | **/ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 54 | bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, const std::string& ctxIfmName); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 55 | |
| 56 | |
| 57 | namespace common { |
| 58 | |
| 59 | enum OPCODES { |
| 60 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */ |
| 61 | MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */ |
| 62 | MENU_OPT_RUN_INF_ALL, /* Run inference on all. */ |
| 63 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */ |
| 64 | MENU_OPT_LIST_IFM /* List the current IFM. */ |
| 65 | }; |
| 66 | |
| 67 | } |
| 68 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | namespace arm { |
| 70 | namespace app { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | /** |
| 72 | * @brief Run inference using given model |
| 73 | * object. If profiling is enabled, it will log the |
| 74 | * statistics too. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 75 | * @param[in] model Reference to the initialised model. |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 76 | * @param[in] profiler Reference to the initialised profiler. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 77 | * @return true if inference succeeds, false otherwise. |
| 78 | **/ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 79 | bool RunInference(arm::app::Model& model, Profiler& profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * @brief Read input and return as an integer. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 83 | * @return Integer value corresponding to the user input. |
| 84 | **/ |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 85 | int ReadUserInputAsInt(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | |
| 87 | #if VERIFY_TEST_OUTPUT |
| 88 | /** |
| 89 | * @brief Helper function to dump a tensor to stdout |
| 90 | * @param[in] tensor tensor to be dumped |
| 91 | * @param[in] lineBreakForNumElements number of elements |
| 92 | * after which line break will be added. |
| 93 | **/ |
alexander | 80eecfb | 2021-07-06 19:47:59 +0100 | [diff] [blame] | 94 | void DumpTensor(const TfLiteTensor* tensor, |
| 95 | size_t lineBreakForNumElements = 16); |
| 96 | |
| 97 | |
| 98 | void DumpTensorData(const uint8_t* tensorData, |
| 99 | size_t size, |
| 100 | size_t lineBreakForNumElements = 16); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 101 | #endif /* VERIFY_TEST_OUTPUT */ |
| 102 | |
| 103 | /** |
| 104 | * @brief List the files baked in the application. |
| 105 | * @param[in] ctx Reference to the application context. |
| 106 | * @return true or false based on event being handled. |
| 107 | **/ |
| 108 | bool ListFilesHandler(ApplicationContext& ctx); |
| 109 | |
| 110 | } /* namespace app */ |
| 111 | } /* namespace arm */ |
| 112 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 113 | |
| 114 | #endif /* USECASE_COMMON_UTILS_HPP */ |