blob: d4394469d268e0052331d498beaf8a635a470694 [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
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010039
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010040
Richard Burtoned35a6f2022-02-14 11:55:35 +000041
42bool PresentInferenceResult(
Kshitij Sisodiab48b5b62021-12-29 14:32:58 +000043 hal_platform &platform,
Liam Barrye9588502022-01-25 14:31:15 +000044 const std::vector<arm::app::ClassificationResult> &results)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010045{
46 constexpr uint32_t dataPsnTxtStartX1 = 150;
47 constexpr uint32_t dataPsnTxtStartY1 = 30;
48
49 constexpr uint32_t dataPsnTxtStartX2 = 10;
50 constexpr uint32_t dataPsnTxtStartY2 = 150;
51
52 constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment. */
53
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010054 platform.data_psn->set_text_color(COLOR_GREEN);
55
56 /* Display each result. */
57 uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr;
58 uint32_t rowIdx2 = dataPsnTxtStartY2;
59
Liam Barrye9588502022-01-25 14:31:15 +000060 info("Final results:\n");
61 info("Total number of inferences: 1\n");
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010062
63 for (uint32_t i = 0; i < results.size(); ++i) {
64 std::string resultStr =
65 std::to_string(i + 1) + ") " +
66 std::to_string(results[i].m_labelIdx) +
67 " (" + std::to_string(results[i].m_normalisedVal) + ")";
68
69 platform.data_psn->present_data_text(
70 resultStr.c_str(), resultStr.size(),
alexander31ae9f02022-02-10 16:15:54 +000071 dataPsnTxtStartX1, rowIdx1, false);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010072 rowIdx1 += dataPsnTxtYIncr;
73
74 resultStr = std::to_string(i + 1) + ") " + results[i].m_label;
75 platform.data_psn->present_data_text(
76 resultStr.c_str(), resultStr.size(),
77 dataPsnTxtStartX2, rowIdx2, 0);
78 rowIdx2 += dataPsnTxtYIncr;
79
Richard Burton9b8d67a2021-12-10 12:32:51 +000080 info("%" PRIu32 ") %" PRIu32 " (%f) -> %s\n", i,
81 results[i].m_labelIdx, results[i].m_normalisedVal,
82 results[i].m_label.c_str());
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010083 }
84
85 return true;
86}
87
Isabella Gottardi3107aa22022-01-27 16:39:37 +000088
alexander31ae9f02022-02-10 16:15:54 +000089void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, const std::string& useCase)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010090{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010091#if NUMBER_OF_FILES > 0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010092 auto curImIdx = ctx.Get<uint32_t>(useCase);
93
94 if (curImIdx + 1 >= NUMBER_OF_FILES) {
95 ctx.Set<uint32_t>(useCase, 0);
96 return;
97 }
98 ++curImIdx;
99 ctx.Set<uint32_t>(useCase, curImIdx);
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100100#else /* NUMBER_OF_FILES > 0 */
101 UNUSED(ctx);
102 UNUSED(useCase);
103#endif /* NUMBER_OF_FILES > 0 */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100104}
105
alexander31ae9f02022-02-10 16:15:54 +0000106bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, const std::string& ctxIfmName)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100107{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100108#if NUMBER_OF_FILES > 0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100109 if (idx >= NUMBER_OF_FILES) {
110 printf_err("Invalid idx %" PRIu32 " (expected less than %u)\n",
111 idx, NUMBER_OF_FILES);
112 return false;
113 }
114 ctx.Set<uint32_t>(ctxIfmName, idx);
115 return true;
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100116#else /* NUMBER_OF_FILES > 0 */
117 UNUSED(ctx);
118 UNUSED(idx);
119 UNUSED(ctxIfmName);
120 return false;
121#endif /* NUMBER_OF_FILES > 0 */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100122}
123
alexander3c798932021-03-26 21:42:19 +0000124namespace arm {
125namespace app {
126
alexander3c798932021-03-26 21:42:19 +0000127
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100128bool RunInference(arm::app::Model& model, Profiler& profiler)
129{
130 profiler.StartProfiling("Inference");
131 bool runInf = model.RunInference();
132 profiler.StopProfiling();
133
134 return runInf;
135}
136
137int ReadUserInputAsInt(hal_platform& platform)
138{
139 char chInput[128];
140 memset(chInput, 0, sizeof(chInput));
141
142 platform.data_acq->get_input(chInput, sizeof(chInput));
143 return atoi(chInput);
144}
145
146void DumpTensorData(const uint8_t* tensorData,
147 size_t size,
148 size_t lineBreakForNumElements)
149{
150 char strhex[8];
151 std::string strdump;
152
153 for (size_t i = 0; i < size; ++i) {
154 if (0 == i % lineBreakForNumElements) {
155 printf("%s\n\t", strdump.c_str());
156 strdump.clear();
157 }
158 snprintf(strhex, sizeof(strhex) - 1,
159 "0x%02x, ", tensorData[i]);
160 strdump += std::string(strhex);
alexander3c798932021-03-26 21:42:19 +0000161 }
162
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100163 if (!strdump.empty()) {
164 printf("%s\n", strdump.c_str());
165 }
166}
alexander3c798932021-03-26 21:42:19 +0000167
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100168void DumpTensor(const TfLiteTensor* tensor, const size_t lineBreakForNumElements)
169{
170 if (!tensor) {
171 printf_err("invalid tensor\n");
172 return;
alexander3c798932021-03-26 21:42:19 +0000173 }
174
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100175 const uint32_t tensorSz = tensor->bytes;
alexander31ae9f02022-02-10 16:15:54 +0000176 const auto* tensorData = tflite::GetTensorData<uint8_t>(tensor);
alexander3c798932021-03-26 21:42:19 +0000177
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100178 DumpTensorData(tensorData, tensorSz, lineBreakForNumElements);
179}
alexander80eecfb2021-07-06 19:47:59 +0100180
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100181bool ListFilesHandler(ApplicationContext& ctx)
182{
183 auto& model = ctx.Get<Model&>("model");
184 auto& platform = ctx.Get<hal_platform&>("platform");
alexander80eecfb2021-07-06 19:47:59 +0100185
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100186 constexpr uint32_t dataPsnTxtStartX = 20;
187 constexpr uint32_t dataPsnTxtStartY = 40;
alexander3c798932021-03-26 21:42:19 +0000188
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100189 if (!model.IsInited()) {
190 printf_err("Model is not initialised! Terminating processing.\n");
191 return false;
alexander3c798932021-03-26 21:42:19 +0000192 }
193
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100194 /* Clear the LCD */
195 platform.data_psn->clear(COLOR_BLACK);
alexander3c798932021-03-26 21:42:19 +0000196
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100197 /* Show the total number of embedded files. */
198 std::string strNumFiles = std::string{"Total Number of Files: "} +
199 std::to_string(NUMBER_OF_FILES);
200 platform.data_psn->present_data_text(strNumFiles.c_str(),
201 strNumFiles.size(),
202 dataPsnTxtStartX,
203 dataPsnTxtStartY,
204 false);
alexander3c798932021-03-26 21:42:19 +0000205
206#if NUMBER_OF_FILES > 0
207 constexpr uint32_t dataPsnTxtYIncr = 16;
208 info("List of Files:\n");
209 uint32_t yVal = dataPsnTxtStartY + dataPsnTxtYIncr;
210 for (uint32_t i = 0; i < NUMBER_OF_FILES; ++i, yVal += dataPsnTxtYIncr) {
211
212 std::string currentFilename{get_filename(i)};
213 platform.data_psn->present_data_text(currentFilename.c_str(),
214 currentFilename.size(),
alexander80eecfb2021-07-06 19:47:59 +0100215 dataPsnTxtStartX, yVal, false);
alexander3c798932021-03-26 21:42:19 +0000216
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100217 info("\t%" PRIu32 " => %s\n", i, currentFilename.c_str());
alexander3c798932021-03-26 21:42:19 +0000218 }
219#endif /* NUMBER_OF_FILES > 0 */
220
221 return true;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100222}
alexander3c798932021-03-26 21:42:19 +0000223
224} /* namespace app */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000225} /* namespace arm */