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