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 "UseCaseCommonUtils.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 18 | #include "InputFiles.hpp" |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 19 | #include <inttypes.h> |
| 20 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 21 | |
| 22 | void DisplayCommonMenu() |
| 23 | { |
| 24 | printf("\n\n"); |
| 25 | printf("User input required\n"); |
| 26 | printf("Enter option number from:\n\n"); |
| 27 | printf(" %u. Classify next ifm\n", common::MENU_OPT_RUN_INF_NEXT); |
| 28 | printf(" %u. Classify ifm at chosen index\n", common::MENU_OPT_RUN_INF_CHOSEN); |
| 29 | printf(" %u. Run classification on all ifm\n", common::MENU_OPT_RUN_INF_ALL); |
| 30 | printf(" %u. Show NN model info\n", common::MENU_OPT_SHOW_MODEL_INFO); |
| 31 | printf(" %u. List ifm\n\n", common::MENU_OPT_LIST_IFM); |
| 32 | printf(" Choice: "); |
| 33 | fflush(stdout); |
| 34 | } |
| 35 | |
| 36 | void image::ConvertImgToInt8(void* data, const size_t kMaxImageSize) |
| 37 | { |
| 38 | auto* tmp_req_data = (uint8_t*) data; |
| 39 | auto* tmp_signed_req_data = (int8_t*) data; |
| 40 | |
| 41 | for (size_t i = 0; i < kMaxImageSize; i++) { |
| 42 | tmp_signed_req_data[i] = (int8_t) ( |
| 43 | (int32_t) (tmp_req_data[i]) - 128); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | bool image::PresentInferenceResult(hal_platform& platform, |
| 48 | const std::vector<arm::app::ClassificationResult>& results) |
| 49 | { |
| 50 | return PresentInferenceResult(platform, results, false); |
| 51 | } |
| 52 | |
| 53 | bool image::PresentInferenceResult(hal_platform &platform, |
| 54 | const std::vector<arm::app::ClassificationResult> &results, |
| 55 | const time_t infTimeMs) |
| 56 | { |
| 57 | return PresentInferenceResult(platform, results, true, infTimeMs); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | bool image::PresentInferenceResult(hal_platform &platform, |
| 62 | const std::vector<arm::app::ClassificationResult> &results, |
| 63 | bool profilingEnabled, |
| 64 | const time_t infTimeMs) |
| 65 | { |
| 66 | constexpr uint32_t dataPsnTxtStartX1 = 150; |
| 67 | constexpr uint32_t dataPsnTxtStartY1 = 30; |
| 68 | |
| 69 | constexpr uint32_t dataPsnTxtStartX2 = 10; |
| 70 | constexpr uint32_t dataPsnTxtStartY2 = 150; |
| 71 | |
| 72 | constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment. */ |
| 73 | |
| 74 | if(profilingEnabled) |
| 75 | { |
| 76 | platform.data_psn->set_text_color(COLOR_YELLOW); |
| 77 | |
| 78 | /* If profiling is enabled, and the time is valid. */ |
| 79 | info("Final results:\n"); |
| 80 | info("Total number of inferences: 1\n"); |
| 81 | if (infTimeMs) |
| 82 | { |
| 83 | std::string strInf = |
| 84 | std::string{"Inference: "} + |
| 85 | std::to_string(infTimeMs) + |
| 86 | std::string{"ms"}; |
| 87 | platform.data_psn->present_data_text( |
| 88 | strInf.c_str(), strInf.size(), |
| 89 | dataPsnTxtStartX1, dataPsnTxtStartY1, 0); |
| 90 | } |
| 91 | } |
| 92 | platform.data_psn->set_text_color(COLOR_GREEN); |
| 93 | |
| 94 | /* Display each result. */ |
| 95 | uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr; |
| 96 | uint32_t rowIdx2 = dataPsnTxtStartY2; |
| 97 | |
| 98 | if(!profilingEnabled) |
| 99 | { |
| 100 | info("Final results:\n"); |
| 101 | info("Total number of inferences: 1\n"); |
| 102 | } |
| 103 | |
| 104 | for (uint32_t i = 0; i < results.size(); ++i) { |
| 105 | std::string resultStr = |
| 106 | std::to_string(i + 1) + ") " + |
| 107 | std::to_string(results[i].m_labelIdx) + |
| 108 | " (" + std::to_string(results[i].m_normalisedVal) + ")"; |
| 109 | |
| 110 | platform.data_psn->present_data_text( |
| 111 | resultStr.c_str(), resultStr.size(), |
| 112 | dataPsnTxtStartX1, rowIdx1, 0); |
| 113 | rowIdx1 += dataPsnTxtYIncr; |
| 114 | |
| 115 | resultStr = std::to_string(i + 1) + ") " + results[i].m_label; |
| 116 | platform.data_psn->present_data_text( |
| 117 | resultStr.c_str(), resultStr.size(), |
| 118 | dataPsnTxtStartX2, rowIdx2, 0); |
| 119 | rowIdx2 += dataPsnTxtYIncr; |
| 120 | |
| 121 | if(profilingEnabled) |
| 122 | { |
| 123 | info("%" PRIu32 ") %" PRIu32 " (%f) -> %s\n", i, results[i].m_labelIdx, |
| 124 | results[i].m_normalisedVal, results[i].m_label.c_str()); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | info("%" PRIu32 ") %" PRIu32 " (%f) -> %s\n", i, |
| 129 | results[i].m_labelIdx, results[i].m_normalisedVal, |
| 130 | results[i].m_label.c_str()); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, std::string useCase) |
| 138 | { |
| 139 | auto curImIdx = ctx.Get<uint32_t>(useCase); |
| 140 | |
| 141 | if (curImIdx + 1 >= NUMBER_OF_FILES) { |
| 142 | ctx.Set<uint32_t>(useCase, 0); |
| 143 | return; |
| 144 | } |
| 145 | ++curImIdx; |
| 146 | ctx.Set<uint32_t>(useCase, curImIdx); |
| 147 | } |
| 148 | |
| 149 | bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, std::string ctxIfmName) |
| 150 | { |
| 151 | if (idx >= NUMBER_OF_FILES) { |
| 152 | printf_err("Invalid idx %" PRIu32 " (expected less than %u)\n", |
| 153 | idx, NUMBER_OF_FILES); |
| 154 | return false; |
| 155 | } |
| 156 | ctx.Set<uint32_t>(ctxIfmName, idx); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 161 | namespace arm { |
| 162 | namespace app { |
| 163 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 164 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 165 | bool RunInference(arm::app::Model& model, Profiler& profiler) |
| 166 | { |
| 167 | profiler.StartProfiling("Inference"); |
| 168 | bool runInf = model.RunInference(); |
| 169 | profiler.StopProfiling(); |
| 170 | |
| 171 | return runInf; |
| 172 | } |
| 173 | |
| 174 | int ReadUserInputAsInt(hal_platform& platform) |
| 175 | { |
| 176 | char chInput[128]; |
| 177 | memset(chInput, 0, sizeof(chInput)); |
| 178 | |
| 179 | platform.data_acq->get_input(chInput, sizeof(chInput)); |
| 180 | return atoi(chInput); |
| 181 | } |
| 182 | |
| 183 | void DumpTensorData(const uint8_t* tensorData, |
| 184 | size_t size, |
| 185 | size_t lineBreakForNumElements) |
| 186 | { |
| 187 | char strhex[8]; |
| 188 | std::string strdump; |
| 189 | |
| 190 | for (size_t i = 0; i < size; ++i) { |
| 191 | if (0 == i % lineBreakForNumElements) { |
| 192 | printf("%s\n\t", strdump.c_str()); |
| 193 | strdump.clear(); |
| 194 | } |
| 195 | snprintf(strhex, sizeof(strhex) - 1, |
| 196 | "0x%02x, ", tensorData[i]); |
| 197 | strdump += std::string(strhex); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 200 | if (!strdump.empty()) { |
| 201 | printf("%s\n", strdump.c_str()); |
| 202 | } |
| 203 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 204 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 205 | void DumpTensor(const TfLiteTensor* tensor, const size_t lineBreakForNumElements) |
| 206 | { |
| 207 | if (!tensor) { |
| 208 | printf_err("invalid tensor\n"); |
| 209 | return; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 212 | const uint32_t tensorSz = tensor->bytes; |
| 213 | const uint8_t* tensorData = tflite::GetTensorData<uint8_t>(tensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 214 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 215 | DumpTensorData(tensorData, tensorSz, lineBreakForNumElements); |
| 216 | } |
alexander | 80eecfb | 2021-07-06 19:47:59 +0100 | [diff] [blame] | 217 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 218 | bool ListFilesHandler(ApplicationContext& ctx) |
| 219 | { |
| 220 | auto& model = ctx.Get<Model&>("model"); |
| 221 | auto& platform = ctx.Get<hal_platform&>("platform"); |
alexander | 80eecfb | 2021-07-06 19:47:59 +0100 | [diff] [blame] | 222 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 223 | constexpr uint32_t dataPsnTxtStartX = 20; |
| 224 | constexpr uint32_t dataPsnTxtStartY = 40; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 225 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 226 | if (!model.IsInited()) { |
| 227 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 228 | return false; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 231 | /* Clear the LCD */ |
| 232 | platform.data_psn->clear(COLOR_BLACK); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 233 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 234 | /* Show the total number of embedded files. */ |
| 235 | std::string strNumFiles = std::string{"Total Number of Files: "} + |
| 236 | std::to_string(NUMBER_OF_FILES); |
| 237 | platform.data_psn->present_data_text(strNumFiles.c_str(), |
| 238 | strNumFiles.size(), |
| 239 | dataPsnTxtStartX, |
| 240 | dataPsnTxtStartY, |
| 241 | false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 242 | |
| 243 | #if NUMBER_OF_FILES > 0 |
| 244 | constexpr uint32_t dataPsnTxtYIncr = 16; |
| 245 | info("List of Files:\n"); |
| 246 | uint32_t yVal = dataPsnTxtStartY + dataPsnTxtYIncr; |
| 247 | for (uint32_t i = 0; i < NUMBER_OF_FILES; ++i, yVal += dataPsnTxtYIncr) { |
| 248 | |
| 249 | std::string currentFilename{get_filename(i)}; |
| 250 | platform.data_psn->present_data_text(currentFilename.c_str(), |
| 251 | currentFilename.size(), |
alexander | 80eecfb | 2021-07-06 19:47:59 +0100 | [diff] [blame] | 252 | dataPsnTxtStartX, yVal, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 253 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 254 | info("\t%" PRIu32 " => %s\n", i, currentFilename.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 255 | } |
| 256 | #endif /* NUMBER_OF_FILES > 0 */ |
| 257 | |
| 258 | return true; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame^] | 259 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 260 | |
| 261 | } /* namespace app */ |
| 262 | } /* namespace arm */ |