blob: a47f1911e7c86438877484b67a8f93f0ef038fcd [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"
Richard Burtoned35a6f2022-02-14 11:55:35 +000021#include "ImageUtils.hpp"
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010022#include "UseCaseCommonUtils.hpp"
23#include "hal.h"
alexander31ae9f02022-02-10 16:15:54 +000024#include "log_macros.h"
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010025
Isabella Gottardi79d41542021-10-20 15:52:32 +010026#include <algorithm>
27
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010028namespace arm {
29namespace app {
30
31 /**
32 * @brief Helper function to load the current image into the input
33 * tensor.
34 * @param[in] imIdx Image index (from the pool of images available
35 * to the application).
36 * @param[out] inputTensor Pointer to the input tensor to be populated.
37 * @return true if tensor is loaded, false otherwise.
38 **/
39 static bool LoadImageIntoTensor(uint32_t imIdx,
40 TfLiteTensor *inputTensor);
41
42 /* Image inference classification handler. */
43 bool ClassifyImageHandler(ApplicationContext &ctx, uint32_t imgIndex, bool runAll)
44 {
45 auto& platform = ctx.Get<hal_platform &>("platform");
46 auto& profiler = ctx.Get<Profiler&>("profiler");
47
48 constexpr uint32_t dataPsnImgDownscaleFactor = 1;
49 constexpr uint32_t dataPsnImgStartX = 10;
50 constexpr uint32_t dataPsnImgStartY = 35;
51
52 constexpr uint32_t dataPsnTxtInfStartX = 150;
53 constexpr uint32_t dataPsnTxtInfStartY = 70;
54
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010055 auto& model = ctx.Get<Model&>("model");
56
57 /* If the request has a valid size, set the image index. */
58 if (imgIndex < NUMBER_OF_FILES) {
59 if (!SetAppCtxIfmIdx(ctx, imgIndex,"imgIndex")) {
60 return false;
61 }
62 }
63 if (!model.IsInited()) {
64 printf_err("Model is not initialised! Terminating processing.\n");
65 return false;
66 }
67
68 auto curImIdx = ctx.Get<uint32_t>("imgIndex");
69
70 TfLiteTensor *outputTensor = model.GetOutputTensor(0);
71 TfLiteTensor *inputTensor = model.GetInputTensor(0);
72
73 if (!inputTensor->dims) {
74 printf_err("Invalid input tensor dims\n");
75 return false;
76 } else if (inputTensor->dims->size < 3) {
77 printf_err("Input tensor dimension should be >= 3\n");
78 return false;
79 }
80 TfLiteIntArray* inputShape = model.GetInputShape(0);
Isabella Gottardi3107aa22022-01-27 16:39:37 +000081 const uint32_t nCols = inputShape->data[arm::app::VisualWakeWordModel::ms_inputColsIdx];
82 const uint32_t nRows = inputShape->data[arm::app::VisualWakeWordModel::ms_inputRowsIdx];
83 if (arm::app::VisualWakeWordModel::ms_inputChannelsIdx >= static_cast<uint32_t>(inputShape->size)) {
84 printf_err("Invalid channel index.\n");
85 return false;
86 }
87 const uint32_t nChannels = inputShape->data[arm::app::VisualWakeWordModel::ms_inputChannelsIdx];
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010088
89 std::vector<ClassificationResult> results;
90
91 do {
Richard Burton9b8d67a2021-12-10 12:32:51 +000092 platform.data_psn->clear(COLOR_BLACK);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010093
94 /* Strings for presentation/logging. */
95 std::string str_inf{"Running inference... "};
96
97 /* Copy over the data. */
98 LoadImageIntoTensor(ctx.Get<uint32_t>("imgIndex"), inputTensor);
99
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),
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100103 nCols, nRows, nChannels,
104 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
105
Isabella Gottardi79d41542021-10-20 15:52:32 +0100106 /* Vww model preprocessing is image conversion from uint8 to [0,1] float values,
107 * then quantize them with input quantization info. */
108 QuantParams inQuantParams = GetTensorQuantParams(inputTensor);
109
110 auto* req_data = static_cast<uint8_t *>(inputTensor->data.data);
111 auto* signed_req_data = static_cast<int8_t *>(inputTensor->data.data);
112 for (size_t i = 0; i < inputTensor->bytes; i++) {
113 auto i_data_int8 = static_cast<int8_t>(((static_cast<float>(req_data[i]) / 255.0f) / inQuantParams.scale) + inQuantParams.offset);
114 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 +0100115 }
116
117 /* Display message on the LCD - inference running. */
118 platform.data_psn->present_data_text(
119 str_inf.c_str(), str_inf.size(),
120 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
121
122 /* Run inference over this image. */
123 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
124 get_filename(ctx.Get<uint32_t>("imgIndex")));
125
126 if (!RunInference(model, profiler)) {
127 return false;
128 }
129
130 /* Erase. */
131 str_inf = std::string(str_inf.size(), ' ');
132 platform.data_psn->present_data_text(
133 str_inf.c_str(), str_inf.size(),
134 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
135
136 auto& classifier = ctx.Get<Classifier&>("classifier");
137 classifier.GetClassificationResults(outputTensor, results,
alexander31ae9f02022-02-10 16:15:54 +0000138 ctx.Get<std::vector <std::string>&>("labels"), 1,
139 false);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100140
141 /* Add results to context for access outside handler. */
142 ctx.Set<std::vector<ClassificationResult>>("results", results);
143
144#if VERIFY_TEST_OUTPUT
145 arm::app::DumpTensor(outputTensor);
146#endif /* VERIFY_TEST_OUTPUT */
147
Richard Burtoned35a6f2022-02-14 11:55:35 +0000148 if (!PresentInferenceResult(platform, results)) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100149 return false;
150 }
151
152 profiler.PrintProfilingResult();
153 IncrementAppCtxIfmIdx(ctx,"imgIndex");
154
155 } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx);
156
157 return true;
158 }
159
160 static bool LoadImageIntoTensor(const uint32_t imIdx,
161 TfLiteTensor *inputTensor)
162 {
163 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
164 inputTensor->bytes : IMAGE_DATA_SIZE;
165 if (imIdx >= NUMBER_OF_FILES) {
166 printf_err("invalid image index %" PRIu32 " (max: %u)\n", imIdx,
167 NUMBER_OF_FILES - 1);
168 return false;
169 }
170
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000171 if (arm::app::VisualWakeWordModel::ms_inputChannelsIdx >= static_cast<uint32_t>(inputTensor->dims->size)) {
172 printf_err("Invalid channel index.\n");
173 return false;
174 }
175 const uint32_t nChannels = inputTensor->dims->data[arm::app::VisualWakeWordModel::ms_inputChannelsIdx];
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100176
177 const uint8_t* srcPtr = get_img_array(imIdx);
Isabella Gottardi79d41542021-10-20 15:52:32 +0100178 auto* dstPtr = static_cast<uint8_t *>(inputTensor->data.data);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100179 if (1 == nChannels) {
180 /**
181 * Visual Wake Word model accepts only one channel =>
182 * Convert image to grayscale here
183 **/
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000184 image::RgbToGrayscale(srcPtr, dstPtr, copySz);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100185 } else {
186 memcpy(inputTensor->data.data, srcPtr, copySz);
187 }
188
189 debug("Image %" PRIu32 " loaded\n", imIdx);
190 return true;
191 }
192
193} /* namespace app */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000194} /* namespace arm */