blob: e4dc4793547076d0e7be90b370a68d86b018e06f [file] [log] [blame]
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01001/*
Liam Barrye9588502022-01-25 14:31:15 +00002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01003 * 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#include "VisualWakeWordModel.hpp"
19#include "Classifier.hpp"
20#include "InputFiles.hpp"
21#include "UseCaseCommonUtils.hpp"
22#include "hal.h"
23
Isabella Gottardi79d41542021-10-20 15:52:32 +010024#include <algorithm>
25
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010026namespace arm {
27namespace app {
28
29 /**
30 * @brief Helper function to load the current image into the input
31 * tensor.
32 * @param[in] imIdx Image index (from the pool of images available
33 * to the application).
34 * @param[out] inputTensor Pointer to the input tensor to be populated.
35 * @return true if tensor is loaded, false otherwise.
36 **/
37 static bool LoadImageIntoTensor(uint32_t imIdx,
38 TfLiteTensor *inputTensor);
39
40 /* Image inference classification handler. */
41 bool ClassifyImageHandler(ApplicationContext &ctx, uint32_t imgIndex, bool runAll)
42 {
43 auto& platform = ctx.Get<hal_platform &>("platform");
44 auto& profiler = ctx.Get<Profiler&>("profiler");
45
46 constexpr uint32_t dataPsnImgDownscaleFactor = 1;
47 constexpr uint32_t dataPsnImgStartX = 10;
48 constexpr uint32_t dataPsnImgStartY = 35;
49
50 constexpr uint32_t dataPsnTxtInfStartX = 150;
51 constexpr uint32_t dataPsnTxtInfStartY = 70;
52
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010053 auto& model = ctx.Get<Model&>("model");
54
55 /* If the request has a valid size, set the image index. */
56 if (imgIndex < NUMBER_OF_FILES) {
57 if (!SetAppCtxIfmIdx(ctx, imgIndex,"imgIndex")) {
58 return false;
59 }
60 }
61 if (!model.IsInited()) {
62 printf_err("Model is not initialised! Terminating processing.\n");
63 return false;
64 }
65
66 auto curImIdx = ctx.Get<uint32_t>("imgIndex");
67
68 TfLiteTensor *outputTensor = model.GetOutputTensor(0);
69 TfLiteTensor *inputTensor = model.GetInputTensor(0);
70
71 if (!inputTensor->dims) {
72 printf_err("Invalid input tensor dims\n");
73 return false;
74 } else if (inputTensor->dims->size < 3) {
75 printf_err("Input tensor dimension should be >= 3\n");
76 return false;
77 }
78 TfLiteIntArray* inputShape = model.GetInputShape(0);
Isabella Gottardi3107aa22022-01-27 16:39:37 +000079 const uint32_t nCols = inputShape->data[arm::app::VisualWakeWordModel::ms_inputColsIdx];
80 const uint32_t nRows = inputShape->data[arm::app::VisualWakeWordModel::ms_inputRowsIdx];
81 if (arm::app::VisualWakeWordModel::ms_inputChannelsIdx >= static_cast<uint32_t>(inputShape->size)) {
82 printf_err("Invalid channel index.\n");
83 return false;
84 }
85 const uint32_t nChannels = inputShape->data[arm::app::VisualWakeWordModel::ms_inputChannelsIdx];
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010086
87 std::vector<ClassificationResult> results;
88
89 do {
Richard Burton9b8d67a2021-12-10 12:32:51 +000090 platform.data_psn->clear(COLOR_BLACK);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010091
92 /* Strings for presentation/logging. */
93 std::string str_inf{"Running inference... "};
94
95 /* Copy over the data. */
96 LoadImageIntoTensor(ctx.Get<uint32_t>("imgIndex"), inputTensor);
97
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),
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100101 nCols, nRows, nChannels,
102 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
103
Isabella Gottardi79d41542021-10-20 15:52:32 +0100104 /* Vww model preprocessing is image conversion from uint8 to [0,1] float values,
105 * then quantize them with input quantization info. */
106 QuantParams inQuantParams = GetTensorQuantParams(inputTensor);
107
108 auto* req_data = static_cast<uint8_t *>(inputTensor->data.data);
109 auto* signed_req_data = static_cast<int8_t *>(inputTensor->data.data);
110 for (size_t i = 0; i < inputTensor->bytes; i++) {
111 auto i_data_int8 = static_cast<int8_t>(((static_cast<float>(req_data[i]) / 255.0f) / inQuantParams.scale) + inQuantParams.offset);
112 signed_req_data[i] = std::min<int8_t>(INT8_MAX, std::max<int8_t>(i_data_int8, INT8_MIN));
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100113 }
114
115 /* Display message on the LCD - inference running. */
116 platform.data_psn->present_data_text(
117 str_inf.c_str(), str_inf.size(),
118 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
119
120 /* Run inference over this image. */
121 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
122 get_filename(ctx.Get<uint32_t>("imgIndex")));
123
124 if (!RunInference(model, profiler)) {
125 return false;
126 }
127
128 /* Erase. */
129 str_inf = std::string(str_inf.size(), ' ');
130 platform.data_psn->present_data_text(
131 str_inf.c_str(), str_inf.size(),
132 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
133
134 auto& classifier = ctx.Get<Classifier&>("classifier");
135 classifier.GetClassificationResults(outputTensor, results,
136 ctx.Get<std::vector <std::string>&>("labels"), 1);
137
138 /* Add results to context for access outside handler. */
139 ctx.Set<std::vector<ClassificationResult>>("results", results);
140
141#if VERIFY_TEST_OUTPUT
142 arm::app::DumpTensor(outputTensor);
143#endif /* VERIFY_TEST_OUTPUT */
144
Liam Barrye9588502022-01-25 14:31:15 +0000145 if (!image::PresentInferenceResult(platform, results)) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100146 return false;
147 }
148
149 profiler.PrintProfilingResult();
150 IncrementAppCtxIfmIdx(ctx,"imgIndex");
151
152 } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx);
153
154 return true;
155 }
156
157 static bool LoadImageIntoTensor(const uint32_t imIdx,
158 TfLiteTensor *inputTensor)
159 {
160 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
161 inputTensor->bytes : IMAGE_DATA_SIZE;
162 if (imIdx >= NUMBER_OF_FILES) {
163 printf_err("invalid image index %" PRIu32 " (max: %u)\n", imIdx,
164 NUMBER_OF_FILES - 1);
165 return false;
166 }
167
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000168 if (arm::app::VisualWakeWordModel::ms_inputChannelsIdx >= static_cast<uint32_t>(inputTensor->dims->size)) {
169 printf_err("Invalid channel index.\n");
170 return false;
171 }
172 const uint32_t nChannels = inputTensor->dims->data[arm::app::VisualWakeWordModel::ms_inputChannelsIdx];
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100173
174 const uint8_t* srcPtr = get_img_array(imIdx);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100175 auto* dstPtr = static_cast<uint8_t *>(inputTensor->data.data);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100176 if (1 == nChannels) {
177 /**
178 * Visual Wake Word model accepts only one channel =>
179 * Convert image to grayscale here
180 **/
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000181 image::RgbToGrayscale(srcPtr, dstPtr, copySz);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100182 } else {
183 memcpy(inputTensor->data.data, srcPtr, copySz);
184 }
185
186 debug("Image %" PRIu32 " loaded\n", imIdx);
187 return true;
188 }
189
190} /* namespace app */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000191} /* namespace arm */