blob: effc06f29e401bc86dc50b9273ff5095b665e76d [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
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 "UseCaseHandler.hpp"
18
19#include "Classifier.hpp"
20#include "InputFiles.hpp"
21#include "MobileNetModel.hpp"
22#include "UseCaseCommonUtils.hpp"
23#include "hal.h"
24
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010025#include <inttypes.h>
26
alexander3c798932021-03-26 21:42:19 +000027using ImgClassClassifier = arm::app::Classifier;
28
29namespace arm {
30namespace app {
31
32 /**
33 * @brief Helper function to load the current image into the input
34 * tensor.
35 * @param[in] imIdx Image index (from the pool of images available
36 * to the application).
37 * @param[out] inputTensor Pointer to the input tensor to be populated.
38 * @return true if tensor is loaded, false otherwise.
39 **/
alexanderc350cdc2021-04-29 20:36:09 +010040 static bool LoadImageIntoTensor(uint32_t imIdx, TfLiteTensor* inputTensor);
alexander3c798932021-03-26 21:42:19 +000041
alexander3c798932021-03-26 21:42:19 +000042 /* Image inference classification handler. */
43 bool ClassifyImageHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
44 {
45 auto& platform = ctx.Get<hal_platform&>("platform");
Isabella Gottardi8df12f32021-04-07 17:15:31 +010046 auto& profiler = ctx.Get<Profiler&>("profiler");
alexander3c798932021-03-26 21:42:19 +000047
48 constexpr uint32_t dataPsnImgDownscaleFactor = 2;
49 constexpr uint32_t dataPsnImgStartX = 10;
50 constexpr uint32_t dataPsnImgStartY = 35;
51
52 constexpr uint32_t dataPsnTxtInfStartX = 150;
53 constexpr uint32_t dataPsnTxtInfStartY = 40;
54
55 platform.data_psn->clear(COLOR_BLACK);
56
57 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 {
92 /* Strings for presentation/logging. */
93 std::string str_inf{"Running inference... "};
94
95 /* Copy over the data. */
alexanderc350cdc2021-04-29 20:36:09 +010096 LoadImageIntoTensor(ctx.Get<uint32_t>("imgIndex"), inputTensor);
alexander3c798932021-03-26 21:42:19 +000097
98 /* Display this image on the LCD. */
99 platform.data_psn->present_data_image(
Isabella Gottardi79d41542021-10-20 15:52:32 +0100100 static_cast<uint8_t *>(inputTensor->data.data),
alexander3c798932021-03-26 21:42:19 +0000101 nCols, nRows, nChannels,
102 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
103
104 /* If the data is signed. */
105 if (model.IsDataSigned()) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100106 image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes);
alexander3c798932021-03-26 21:42:19 +0000107 }
108
109 /* Display message on the LCD - inference running. */
110 platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(),
111 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
112
113 /* Run inference over this image. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100114 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
alexander3c798932021-03-26 21:42:19 +0000115 get_filename(ctx.Get<uint32_t>("imgIndex")));
116
alexander27b62d92021-05-04 20:46:08 +0100117 if (!RunInference(model, profiler)) {
118 return false;
119 }
alexander3c798932021-03-26 21:42:19 +0000120
121 /* Erase. */
122 str_inf = std::string(str_inf.size(), ' ');
123 platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(),
124 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
125
126 auto& classifier = ctx.Get<ImgClassClassifier&>("classifier");
127 classifier.GetClassificationResults(outputTensor, results,
128 ctx.Get<std::vector <std::string>&>("labels"),
129 5);
130
131 /* Add results to context for access outside handler. */
132 ctx.Set<std::vector<ClassificationResult>>("results", results);
133
134#if VERIFY_TEST_OUTPUT
135 arm::app::DumpTensor(outputTensor);
136#endif /* VERIFY_TEST_OUTPUT */
137
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100138 if (!image::PresentInferenceResult(platform, results)) {
alexander3c798932021-03-26 21:42:19 +0000139 return false;
140 }
141
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100142 profiler.PrintProfilingResult();
143
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100144 IncrementAppCtxIfmIdx(ctx,"imgIndex");
alexander3c798932021-03-26 21:42:19 +0000145
146 } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx);
147
148 return true;
149 }
150
alexanderc350cdc2021-04-29 20:36:09 +0100151 static bool LoadImageIntoTensor(uint32_t imIdx, TfLiteTensor* inputTensor)
alexander3c798932021-03-26 21:42:19 +0000152 {
153 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
154 inputTensor->bytes : IMAGE_DATA_SIZE;
155 const uint8_t* imgSrc = get_img_array(imIdx);
156 if (nullptr == imgSrc) {
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100157 printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", imIdx,
alexander3c798932021-03-26 21:42:19 +0000158 NUMBER_OF_FILES - 1);
159 return false;
160 }
161
162 memcpy(inputTensor->data.data, imgSrc, copySz);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100163 debug("Image %" PRIu32 " loaded\n", imIdx);
alexander3c798932021-03-26 21:42:19 +0000164 return true;
165 }
166
alexander3c798932021-03-26 21:42:19 +0000167
168} /* namespace app */
169} /* namespace arm */