blob: 1f1d78b1360b18701d56879599cec6f4c2fca7c0 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtoned35a6f2022-02-14 11:55:35 +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 "UseCaseHandler.hpp"
18
19#include "Classifier.hpp"
20#include "InputFiles.hpp"
21#include "MobileNetModel.hpp"
Richard Burtoned35a6f2022-02-14 11:55:35 +000022#include "ImageUtils.hpp"
alexander3c798932021-03-26 21:42:19 +000023#include "UseCaseCommonUtils.hpp"
24#include "hal.h"
alexander31ae9f02022-02-10 16:15:54 +000025#include "log_macros.h"
alexander3c798932021-03-26 21:42:19 +000026
alexander31ae9f02022-02-10 16:15:54 +000027#include <cinttypes>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010028
alexander3c798932021-03-26 21:42:19 +000029using ImgClassClassifier = arm::app::Classifier;
30
31namespace arm {
32namespace app {
33
34 /**
35 * @brief Helper function to load the current image into the input
36 * tensor.
37 * @param[in] imIdx Image index (from the pool of images available
38 * to the application).
39 * @param[out] inputTensor Pointer to the input tensor to be populated.
40 * @return true if tensor is loaded, false otherwise.
41 **/
alexanderc350cdc2021-04-29 20:36:09 +010042 static bool LoadImageIntoTensor(uint32_t imIdx, TfLiteTensor* inputTensor);
alexander3c798932021-03-26 21:42:19 +000043
alexander3c798932021-03-26 21:42:19 +000044 /* Image inference classification handler. */
45 bool ClassifyImageHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
46 {
47 auto& platform = ctx.Get<hal_platform&>("platform");
Isabella Gottardi8df12f32021-04-07 17:15:31 +010048 auto& profiler = ctx.Get<Profiler&>("profiler");
alexander3c798932021-03-26 21:42:19 +000049
50 constexpr uint32_t dataPsnImgDownscaleFactor = 2;
51 constexpr uint32_t dataPsnImgStartX = 10;
52 constexpr uint32_t dataPsnImgStartY = 35;
53
54 constexpr uint32_t dataPsnTxtInfStartX = 150;
55 constexpr uint32_t dataPsnTxtInfStartY = 40;
56
alexander3c798932021-03-26 21:42:19 +000057 auto& model = ctx.Get<Model&>("model");
58
59 /* If the request has a valid size, set the image index. */
60 if (imgIndex < NUMBER_OF_FILES) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010061 if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) {
alexander3c798932021-03-26 21:42:19 +000062 return false;
63 }
64 }
65 if (!model.IsInited()) {
66 printf_err("Model is not initialised! Terminating processing.\n");
67 return false;
68 }
69
70 auto curImIdx = ctx.Get<uint32_t>("imgIndex");
71
72 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
73 TfLiteTensor* inputTensor = model.GetInputTensor(0);
74
75 if (!inputTensor->dims) {
76 printf_err("Invalid input tensor dims\n");
77 return false;
78 } else if (inputTensor->dims->size < 3) {
79 printf_err("Input tensor dimension should be >= 3\n");
80 return false;
81 }
82
83 TfLiteIntArray* inputShape = model.GetInputShape(0);
84
85 const uint32_t nCols = inputShape->data[arm::app::MobileNetModel::ms_inputColsIdx];
86 const uint32_t nRows = inputShape->data[arm::app::MobileNetModel::ms_inputRowsIdx];
87 const uint32_t nChannels = inputShape->data[arm::app::MobileNetModel::ms_inputChannelsIdx];
88
89 std::vector<ClassificationResult> results;
90
91 do {
Richard Burton9b8d67a2021-12-10 12:32:51 +000092 platform.data_psn->clear(COLOR_BLACK);
93
alexander3c798932021-03-26 21:42:19 +000094 /* Strings for presentation/logging. */
95 std::string str_inf{"Running inference... "};
96
97 /* Copy over the data. */
alexanderc350cdc2021-04-29 20:36:09 +010098 LoadImageIntoTensor(ctx.Get<uint32_t>("imgIndex"), inputTensor);
alexander3c798932021-03-26 21:42:19 +000099
100 /* Display this image on the LCD. */
101 platform.data_psn->present_data_image(
Isabella Gottardi79d41542021-10-20 15:52:32 +0100102 static_cast<uint8_t *>(inputTensor->data.data),
alexander3c798932021-03-26 21:42:19 +0000103 nCols, nRows, nChannels,
104 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
105
106 /* If the data is signed. */
107 if (model.IsDataSigned()) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100108 image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes);
alexander3c798932021-03-26 21:42:19 +0000109 }
110
111 /* Display message on the LCD - inference running. */
112 platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(),
alexander31ae9f02022-02-10 16:15:54 +0000113 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
alexander3c798932021-03-26 21:42:19 +0000114
115 /* Run inference over this image. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100116 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
alexander3c798932021-03-26 21:42:19 +0000117 get_filename(ctx.Get<uint32_t>("imgIndex")));
118
alexander27b62d92021-05-04 20:46:08 +0100119 if (!RunInference(model, profiler)) {
120 return false;
121 }
alexander3c798932021-03-26 21:42:19 +0000122
123 /* Erase. */
124 str_inf = std::string(str_inf.size(), ' ');
125 platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(),
alexander31ae9f02022-02-10 16:15:54 +0000126 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
alexander3c798932021-03-26 21:42:19 +0000127
128 auto& classifier = ctx.Get<ImgClassClassifier&>("classifier");
129 classifier.GetClassificationResults(outputTensor, results,
130 ctx.Get<std::vector <std::string>&>("labels"),
alexander31ae9f02022-02-10 16:15:54 +0000131 5, false);
alexander3c798932021-03-26 21:42:19 +0000132
133 /* Add results to context for access outside handler. */
134 ctx.Set<std::vector<ClassificationResult>>("results", results);
135
136#if VERIFY_TEST_OUTPUT
137 arm::app::DumpTensor(outputTensor);
138#endif /* VERIFY_TEST_OUTPUT */
139
Richard Burtoned35a6f2022-02-14 11:55:35 +0000140 if (!PresentInferenceResult(platform, results)) {
alexander3c798932021-03-26 21:42:19 +0000141 return false;
142 }
143
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100144 profiler.PrintProfilingResult();
145
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100146 IncrementAppCtxIfmIdx(ctx,"imgIndex");
alexander3c798932021-03-26 21:42:19 +0000147
148 } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx);
149
150 return true;
151 }
152
alexanderc350cdc2021-04-29 20:36:09 +0100153 static bool LoadImageIntoTensor(uint32_t imIdx, TfLiteTensor* inputTensor)
alexander3c798932021-03-26 21:42:19 +0000154 {
155 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
156 inputTensor->bytes : IMAGE_DATA_SIZE;
157 const uint8_t* imgSrc = get_img_array(imIdx);
158 if (nullptr == imgSrc) {
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100159 printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", imIdx,
alexander3c798932021-03-26 21:42:19 +0000160 NUMBER_OF_FILES - 1);
161 return false;
162 }
163
164 memcpy(inputTensor->data.data, imgSrc, copySz);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100165 debug("Image %" PRIu32 " loaded\n", imIdx);
alexander3c798932021-03-26 21:42:19 +0000166 return true;
167 }
168
alexander3c798932021-03-26 21:42:19 +0000169
170} /* namespace app */
171} /* namespace arm */