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 | #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" |
| 27 | #include <inttypes.h> |
| 28 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | |
| 30 | /* Helper macro to convert RGB888 to RGB565 format. */ |
| 31 | #define RGB888_TO_RGB565(R8,G8,B8) ((((R8>>3) & 0x1F) << 11) | \ |
| 32 | (((G8>>2) & 0x3F) << 5) | \ |
| 33 | ((B8>>3) & 0x1F)) |
| 34 | |
| 35 | constexpr uint16_t COLOR_BLACK = 0; |
| 36 | constexpr uint16_t COLOR_GREEN = RGB888_TO_RGB565( 0, 255, 0); // 2016; |
| 37 | constexpr uint16_t COLOR_YELLOW = RGB888_TO_RGB565(255, 255, 0); // 65504; |
| 38 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 39 | |
| 40 | void DisplayCommonMenu(); |
| 41 | |
| 42 | namespace image{ |
| 43 | |
| 44 | /** |
| 45 | * @brief Helper function to convert a UINT8 image to INT8 format. |
| 46 | * @param[in,out] data Pointer to the data start. |
| 47 | * @param[in] kMaxImageSize Total number of pixels in the image. |
| 48 | **/ |
| 49 | void ConvertImgToInt8(void * data, size_t kMaxImageSize); |
| 50 | |
| 51 | /** |
| 52 | * @brief Presents inference results using the data presentation |
| 53 | * object. |
| 54 | * @param[in] platform Reference to the hal platform object. |
| 55 | * @param[in] results Vector of classification results to be displayed. |
| 56 | * @return true if successful, false otherwise. |
| 57 | **/ |
| 58 | bool PresentInferenceResult(hal_platform & platform, |
| 59 | const std::vector < arm::app::ClassificationResult > & results); |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * @brief Presents inference results along with the inference time using the data presentation |
| 64 | * object. |
| 65 | * @param[in] platform Reference to the hal platform object. |
| 66 | * @param[in] results Vector of classification results to be displayed. |
| 67 | * @param[in] results Inference time in ms. |
| 68 | * @return true if successful, false otherwise. |
| 69 | **/ |
| 70 | bool PresentInferenceResult(hal_platform & platform, |
| 71 | const std::vector < arm::app::ClassificationResult > & results, |
| 72 | const time_t infTimeMs); |
| 73 | |
| 74 | /** |
| 75 | * @brief Presents inference results along with the inference time using the data presentation |
| 76 | * object. |
| 77 | * @param[in] platform Reference to the hal platform object. |
| 78 | * @param[in] results Vector of classification results to be displayed. |
| 79 | * @param[in] results Inference time in ms. |
| 80 | * @return true if successful, false otherwise. |
| 81 | **/ |
| 82 | bool PresentInferenceResult(hal_platform & platform, |
| 83 | const std::vector < arm::app::ClassificationResult > & results, |
| 84 | bool profilingEnabled, |
| 85 | const time_t infTimeMs = 0); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @brief Helper function to increment current input feature vector index. |
| 90 | * @param[in,out] ctx Pointer to the application context object. |
| 91 | * @param[in] useCase Use case name |
| 92 | **/ |
| 93 | void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, std::string useCase); |
| 94 | |
| 95 | /** |
| 96 | * @brief Helper function to set the input feature map index. |
| 97 | * @param[in,out] ctx Pointer to the application context object. |
| 98 | * @param[in] idx Value to be set. |
| 99 | * @param[in] ctxIfmName Input Feature Map name |
| 100 | * @return true if index is set, false otherwise. |
| 101 | **/ |
| 102 | bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, std::string ctxIfmName); |
| 103 | |
| 104 | |
| 105 | namespace common { |
| 106 | |
| 107 | enum OPCODES { |
| 108 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */ |
| 109 | MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */ |
| 110 | MENU_OPT_RUN_INF_ALL, /* Run inference on all. */ |
| 111 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */ |
| 112 | MENU_OPT_LIST_IFM /* List the current IFM. */ |
| 113 | }; |
| 114 | |
| 115 | } |
| 116 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 117 | namespace arm { |
| 118 | namespace app { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 119 | /** |
| 120 | * @brief Run inference using given model |
| 121 | * object. If profiling is enabled, it will log the |
| 122 | * statistics too. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 123 | * @param[in] model Reference to the initialised model. |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 124 | * @param[in] profiler Reference to the initialised profiler. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 125 | * @return true if inference succeeds, false otherwise. |
| 126 | **/ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 127 | bool RunInference(arm::app::Model& model, Profiler& profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * @brief Read input and return as an integer. |
| 131 | * @param[in] platform Reference to the hal platform object. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 132 | * @return Integer value corresponding to the user input. |
| 133 | **/ |
| 134 | int ReadUserInputAsInt(hal_platform& platform); |
| 135 | |
| 136 | #if VERIFY_TEST_OUTPUT |
| 137 | /** |
| 138 | * @brief Helper function to dump a tensor to stdout |
| 139 | * @param[in] tensor tensor to be dumped |
| 140 | * @param[in] lineBreakForNumElements number of elements |
| 141 | * after which line break will be added. |
| 142 | **/ |
alexander | 80eecfb | 2021-07-06 19:47:59 +0100 | [diff] [blame] | 143 | void DumpTensor(const TfLiteTensor* tensor, |
| 144 | size_t lineBreakForNumElements = 16); |
| 145 | |
| 146 | |
| 147 | void DumpTensorData(const uint8_t* tensorData, |
| 148 | size_t size, |
| 149 | size_t lineBreakForNumElements = 16); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 150 | #endif /* VERIFY_TEST_OUTPUT */ |
| 151 | |
| 152 | /** |
| 153 | * @brief List the files baked in the application. |
| 154 | * @param[in] ctx Reference to the application context. |
| 155 | * @return true or false based on event being handled. |
| 156 | **/ |
| 157 | bool ListFilesHandler(ApplicationContext& ctx); |
| 158 | |
| 159 | } /* namespace app */ |
| 160 | } /* namespace arm */ |
| 161 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 162 | |
| 163 | #endif /* USECASE_COMMON_UTILS_HPP */ |