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