blob: 340a7673a5a1574b65788d71add7ef6046540f17 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Liam Barrye9588502022-01-25 14:31:15 +00002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 Burtoned35a6f2022-02-14 11:55:35 +000018#include "ImageUtils.hpp"
alexander3c798932021-03-26 21:42:19 +000019#include "InputFiles.hpp"
alexander31ae9f02022-02-10 16:15:54 +000020#include "log_macros.h"
21
22#include <cinttypes>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010023
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010024
25void 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 Sisodia68fdd112022-04-06 13:03:20 +010039bool PresentInferenceResult(const std::vector<arm::app::ClassificationResult> &results)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010040{
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 Sisodia68fdd112022-04-06 13:03:20 +010049 hal_lcd_set_text_color(COLOR_GREEN);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010050
51 /* Display each result. */
52 uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr;
53 uint32_t rowIdx2 = dataPsnTxtStartY2;
54
Liam Barrye9588502022-01-25 14:31:15 +000055 info("Final results:\n");
56 info("Total number of inferences: 1\n");
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010057
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 Sisodia68fdd112022-04-06 13:03:20 +010064 hal_lcd_display_text(
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010065 resultStr.c_str(), resultStr.size(),
alexander31ae9f02022-02-10 16:15:54 +000066 dataPsnTxtStartX1, rowIdx1, false);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010067 rowIdx1 += dataPsnTxtYIncr;
68
69 resultStr = std::to_string(i + 1) + ") " + results[i].m_label;
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010070 hal_lcd_display_text(
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010071 resultStr.c_str(), resultStr.size(),
72 dataPsnTxtStartX2, rowIdx2, 0);
73 rowIdx2 += dataPsnTxtYIncr;
74
Richard Burton9b8d67a2021-12-10 12:32:51 +000075 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áin8f958872021-09-15 09:32:30 +010078 }
79
80 return true;
81}
82
Isabella Gottardi3107aa22022-01-27 16:39:37 +000083
alexander31ae9f02022-02-10 16:15:54 +000084void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, const std::string& useCase)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010085{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010086#if NUMBER_OF_FILES > 0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010087 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 Sisodiaaa5e1f62021-09-24 14:42:08 +010095#else /* NUMBER_OF_FILES > 0 */
96 UNUSED(ctx);
97 UNUSED(useCase);
98#endif /* NUMBER_OF_FILES > 0 */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010099}
100
alexander31ae9f02022-02-10 16:15:54 +0000101bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, const std::string& ctxIfmName)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100102{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100103#if NUMBER_OF_FILES > 0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100104 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 Sisodiaaa5e1f62021-09-24 14:42:08 +0100111#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áin8f958872021-09-15 09:32:30 +0100117}
118
alexander3c798932021-03-26 21:42:19 +0000119namespace arm {
120namespace app {
121
alexander3c798932021-03-26 21:42:19 +0000122
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100123bool 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 Sisodia68fdd112022-04-06 13:03:20 +0100132int ReadUserInputAsInt()
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100133{
134 char chInput[128];
135 memset(chInput, 0, sizeof(chInput));
136
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100137 hal_get_user_input(chInput, sizeof(chInput));
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100138 return atoi(chInput);
139}
140
141void 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);
alexander3c798932021-03-26 21:42:19 +0000156 }
157
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100158 if (!strdump.empty()) {
159 printf("%s\n", strdump.c_str());
160 }
161}
alexander3c798932021-03-26 21:42:19 +0000162
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100163void DumpTensor(const TfLiteTensor* tensor, const size_t lineBreakForNumElements)
164{
165 if (!tensor) {
166 printf_err("invalid tensor\n");
167 return;
alexander3c798932021-03-26 21:42:19 +0000168 }
169
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100170 const uint32_t tensorSz = tensor->bytes;
alexander31ae9f02022-02-10 16:15:54 +0000171 const auto* tensorData = tflite::GetTensorData<uint8_t>(tensor);
alexander3c798932021-03-26 21:42:19 +0000172
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100173 DumpTensorData(tensorData, tensorSz, lineBreakForNumElements);
174}
alexander80eecfb2021-07-06 19:47:59 +0100175
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100176bool ListFilesHandler(ApplicationContext& ctx)
177{
178 auto& model = ctx.Get<Model&>("model");
alexander80eecfb2021-07-06 19:47:59 +0100179
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100180 constexpr uint32_t dataPsnTxtStartX = 20;
181 constexpr uint32_t dataPsnTxtStartY = 40;
alexander3c798932021-03-26 21:42:19 +0000182
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100183 if (!model.IsInited()) {
184 printf_err("Model is not initialised! Terminating processing.\n");
185 return false;
alexander3c798932021-03-26 21:42:19 +0000186 }
187
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100188 /* Clear the LCD */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100189 hal_lcd_clear(COLOR_BLACK);
alexander3c798932021-03-26 21:42:19 +0000190
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100191 /* 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 Sisodia68fdd112022-04-06 13:03:20 +0100194 hal_lcd_display_text(strNumFiles.c_str(),
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100195 strNumFiles.size(),
196 dataPsnTxtStartX,
197 dataPsnTxtStartY,
198 false);
alexander3c798932021-03-26 21:42:19 +0000199
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 Sisodia68fdd112022-04-06 13:03:20 +0100207 hal_lcd_display_text(currentFilename.c_str(),
alexander3c798932021-03-26 21:42:19 +0000208 currentFilename.size(),
alexander80eecfb2021-07-06 19:47:59 +0100209 dataPsnTxtStartX, yVal, false);
alexander3c798932021-03-26 21:42:19 +0000210
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100211 info("\t%" PRIu32 " => %s\n", i, currentFilename.c_str());
alexander3c798932021-03-26 21:42:19 +0000212 }
213#endif /* NUMBER_OF_FILES > 0 */
214
215 return true;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100216}
alexander3c798932021-03-26 21:42:19 +0000217
218} /* namespace app */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000219} /* namespace arm */