blob: d740d107d13ae95317aacc84e3503cb3d7aa9ad0 [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"
alexander3c798932021-03-26 21:42:19 +000018#include "InputFiles.hpp"
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010019#include <inttypes.h>
20
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010021
22void DisplayCommonMenu()
23{
24 printf("\n\n");
25 printf("User input required\n");
26 printf("Enter option number from:\n\n");
27 printf(" %u. Classify next ifm\n", common::MENU_OPT_RUN_INF_NEXT);
28 printf(" %u. Classify ifm at chosen index\n", common::MENU_OPT_RUN_INF_CHOSEN);
29 printf(" %u. Run classification on all ifm\n", common::MENU_OPT_RUN_INF_ALL);
30 printf(" %u. Show NN model info\n", common::MENU_OPT_SHOW_MODEL_INFO);
31 printf(" %u. List ifm\n\n", common::MENU_OPT_LIST_IFM);
32 printf(" Choice: ");
33 fflush(stdout);
34}
35
36void image::ConvertImgToInt8(void* data, const size_t kMaxImageSize)
37{
Isabella Gottardi79d41542021-10-20 15:52:32 +010038 auto* tmp_req_data = static_cast<uint8_t *>(data);
39 auto* tmp_signed_req_data = static_cast<int8_t *>(data);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010040
41 for (size_t i = 0; i < kMaxImageSize; i++) {
42 tmp_signed_req_data[i] = (int8_t) (
43 (int32_t) (tmp_req_data[i]) - 128);
44 }
45}
46
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010047
Kshitij Sisodiab48b5b62021-12-29 14:32:58 +000048bool image::PresentInferenceResult(
49 hal_platform &platform,
Liam Barrye9588502022-01-25 14:31:15 +000050 const std::vector<arm::app::ClassificationResult> &results)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010051{
52 constexpr uint32_t dataPsnTxtStartX1 = 150;
53 constexpr uint32_t dataPsnTxtStartY1 = 30;
54
55 constexpr uint32_t dataPsnTxtStartX2 = 10;
56 constexpr uint32_t dataPsnTxtStartY2 = 150;
57
58 constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment. */
59
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010060 platform.data_psn->set_text_color(COLOR_GREEN);
61
62 /* Display each result. */
63 uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr;
64 uint32_t rowIdx2 = dataPsnTxtStartY2;
65
Liam Barrye9588502022-01-25 14:31:15 +000066 info("Final results:\n");
67 info("Total number of inferences: 1\n");
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010068
69 for (uint32_t i = 0; i < results.size(); ++i) {
70 std::string resultStr =
71 std::to_string(i + 1) + ") " +
72 std::to_string(results[i].m_labelIdx) +
73 " (" + std::to_string(results[i].m_normalisedVal) + ")";
74
75 platform.data_psn->present_data_text(
76 resultStr.c_str(), resultStr.size(),
77 dataPsnTxtStartX1, rowIdx1, 0);
78 rowIdx1 += dataPsnTxtYIncr;
79
80 resultStr = std::to_string(i + 1) + ") " + results[i].m_label;
81 platform.data_psn->present_data_text(
82 resultStr.c_str(), resultStr.size(),
83 dataPsnTxtStartX2, rowIdx2, 0);
84 rowIdx2 += dataPsnTxtYIncr;
85
Richard Burton9b8d67a2021-12-10 12:32:51 +000086 info("%" PRIu32 ") %" PRIu32 " (%f) -> %s\n", i,
87 results[i].m_labelIdx, results[i].m_normalisedVal,
88 results[i].m_label.c_str());
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010089 }
90
91 return true;
92}
93
94void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, std::string useCase)
95{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010096#if NUMBER_OF_FILES > 0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010097 auto curImIdx = ctx.Get<uint32_t>(useCase);
98
99 if (curImIdx + 1 >= NUMBER_OF_FILES) {
100 ctx.Set<uint32_t>(useCase, 0);
101 return;
102 }
103 ++curImIdx;
104 ctx.Set<uint32_t>(useCase, curImIdx);
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100105#else /* NUMBER_OF_FILES > 0 */
106 UNUSED(ctx);
107 UNUSED(useCase);
108#endif /* NUMBER_OF_FILES > 0 */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100109}
110
111bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, std::string ctxIfmName)
112{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100113#if NUMBER_OF_FILES > 0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100114 if (idx >= NUMBER_OF_FILES) {
115 printf_err("Invalid idx %" PRIu32 " (expected less than %u)\n",
116 idx, NUMBER_OF_FILES);
117 return false;
118 }
119 ctx.Set<uint32_t>(ctxIfmName, idx);
120 return true;
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100121#else /* NUMBER_OF_FILES > 0 */
122 UNUSED(ctx);
123 UNUSED(idx);
124 UNUSED(ctxIfmName);
125 return false;
126#endif /* NUMBER_OF_FILES > 0 */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100127}
128
alexander3c798932021-03-26 21:42:19 +0000129namespace arm {
130namespace app {
131
alexander3c798932021-03-26 21:42:19 +0000132
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100133bool RunInference(arm::app::Model& model, Profiler& profiler)
134{
135 profiler.StartProfiling("Inference");
136 bool runInf = model.RunInference();
137 profiler.StopProfiling();
138
139 return runInf;
140}
141
142int ReadUserInputAsInt(hal_platform& platform)
143{
144 char chInput[128];
145 memset(chInput, 0, sizeof(chInput));
146
147 platform.data_acq->get_input(chInput, sizeof(chInput));
148 return atoi(chInput);
149}
150
151void DumpTensorData(const uint8_t* tensorData,
152 size_t size,
153 size_t lineBreakForNumElements)
154{
155 char strhex[8];
156 std::string strdump;
157
158 for (size_t i = 0; i < size; ++i) {
159 if (0 == i % lineBreakForNumElements) {
160 printf("%s\n\t", strdump.c_str());
161 strdump.clear();
162 }
163 snprintf(strhex, sizeof(strhex) - 1,
164 "0x%02x, ", tensorData[i]);
165 strdump += std::string(strhex);
alexander3c798932021-03-26 21:42:19 +0000166 }
167
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100168 if (!strdump.empty()) {
169 printf("%s\n", strdump.c_str());
170 }
171}
alexander3c798932021-03-26 21:42:19 +0000172
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100173void DumpTensor(const TfLiteTensor* tensor, const size_t lineBreakForNumElements)
174{
175 if (!tensor) {
176 printf_err("invalid tensor\n");
177 return;
alexander3c798932021-03-26 21:42:19 +0000178 }
179
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100180 const uint32_t tensorSz = tensor->bytes;
181 const uint8_t* tensorData = tflite::GetTensorData<uint8_t>(tensor);
alexander3c798932021-03-26 21:42:19 +0000182
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100183 DumpTensorData(tensorData, tensorSz, lineBreakForNumElements);
184}
alexander80eecfb2021-07-06 19:47:59 +0100185
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100186bool ListFilesHandler(ApplicationContext& ctx)
187{
188 auto& model = ctx.Get<Model&>("model");
189 auto& platform = ctx.Get<hal_platform&>("platform");
alexander80eecfb2021-07-06 19:47:59 +0100190
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100191 constexpr uint32_t dataPsnTxtStartX = 20;
192 constexpr uint32_t dataPsnTxtStartY = 40;
alexander3c798932021-03-26 21:42:19 +0000193
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100194 if (!model.IsInited()) {
195 printf_err("Model is not initialised! Terminating processing.\n");
196 return false;
alexander3c798932021-03-26 21:42:19 +0000197 }
198
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100199 /* Clear the LCD */
200 platform.data_psn->clear(COLOR_BLACK);
alexander3c798932021-03-26 21:42:19 +0000201
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100202 /* Show the total number of embedded files. */
203 std::string strNumFiles = std::string{"Total Number of Files: "} +
204 std::to_string(NUMBER_OF_FILES);
205 platform.data_psn->present_data_text(strNumFiles.c_str(),
206 strNumFiles.size(),
207 dataPsnTxtStartX,
208 dataPsnTxtStartY,
209 false);
alexander3c798932021-03-26 21:42:19 +0000210
211#if NUMBER_OF_FILES > 0
212 constexpr uint32_t dataPsnTxtYIncr = 16;
213 info("List of Files:\n");
214 uint32_t yVal = dataPsnTxtStartY + dataPsnTxtYIncr;
215 for (uint32_t i = 0; i < NUMBER_OF_FILES; ++i, yVal += dataPsnTxtYIncr) {
216
217 std::string currentFilename{get_filename(i)};
218 platform.data_psn->present_data_text(currentFilename.c_str(),
219 currentFilename.size(),
alexander80eecfb2021-07-06 19:47:59 +0100220 dataPsnTxtStartX, yVal, false);
alexander3c798932021-03-26 21:42:19 +0000221
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100222 info("\t%" PRIu32 " => %s\n", i, currentFilename.c_str());
alexander3c798932021-03-26 21:42:19 +0000223 }
224#endif /* NUMBER_OF_FILES > 0 */
225
226 return true;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100227}
alexander3c798932021-03-26 21:42:19 +0000228
229} /* namespace app */
230} /* namespace arm */