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