blob: fafc6b9b2487c76a7e6cc3d4ffc1ed0b46486d26 [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"
alexander31ae9f02022-02-10 16:15:54 +000024#include "log_macros.h"
alexander3c798932021-03-26 21:42:19 +000025
alexander31ae9f02022-02-10 16:15:54 +000026#include <cinttypes>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010027
alexander3c798932021-03-26 21:42:19 +000028using ImgClassClassifier = arm::app::Classifier;
29
30namespace arm {
31namespace app {
32
33 /**
34 * @brief Helper function to load the current image into the input
35 * tensor.
36 * @param[in] imIdx Image index (from the pool of images available
37 * to the application).
38 * @param[out] inputTensor Pointer to the input tensor to be populated.
39 * @return true if tensor is loaded, false otherwise.
40 **/
alexanderc350cdc2021-04-29 20:36:09 +010041 static bool LoadImageIntoTensor(uint32_t imIdx, TfLiteTensor* inputTensor);
alexander3c798932021-03-26 21:42:19 +000042
alexander3c798932021-03-26 21:42:19 +000043 /* Image inference classification handler. */
44 bool ClassifyImageHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
45 {
46 auto& platform = ctx.Get<hal_platform&>("platform");
Isabella Gottardi8df12f32021-04-07 17:15:31 +010047 auto& profiler = ctx.Get<Profiler&>("profiler");
alexander3c798932021-03-26 21:42:19 +000048
49 constexpr uint32_t dataPsnImgDownscaleFactor = 2;
50 constexpr uint32_t dataPsnImgStartX = 10;
51 constexpr uint32_t dataPsnImgStartY = 35;
52
53 constexpr uint32_t dataPsnTxtInfStartX = 150;
54 constexpr uint32_t dataPsnTxtInfStartY = 40;
55
alexander3c798932021-03-26 21:42:19 +000056 auto& model = ctx.Get<Model&>("model");
57
58 /* If the request has a valid size, set the image index. */
59 if (imgIndex < NUMBER_OF_FILES) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010060 if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) {
alexander3c798932021-03-26 21:42:19 +000061 return false;
62 }
63 }
64 if (!model.IsInited()) {
65 printf_err("Model is not initialised! Terminating processing.\n");
66 return false;
67 }
68
69 auto curImIdx = ctx.Get<uint32_t>("imgIndex");
70
71 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
72 TfLiteTensor* inputTensor = model.GetInputTensor(0);
73
74 if (!inputTensor->dims) {
75 printf_err("Invalid input tensor dims\n");
76 return false;
77 } else if (inputTensor->dims->size < 3) {
78 printf_err("Input tensor dimension should be >= 3\n");
79 return false;
80 }
81
82 TfLiteIntArray* inputShape = model.GetInputShape(0);
83
84 const uint32_t nCols = inputShape->data[arm::app::MobileNetModel::ms_inputColsIdx];
85 const uint32_t nRows = inputShape->data[arm::app::MobileNetModel::ms_inputRowsIdx];
86 const uint32_t nChannels = inputShape->data[arm::app::MobileNetModel::ms_inputChannelsIdx];
87
88 std::vector<ClassificationResult> results;
89
90 do {
Richard Burton9b8d67a2021-12-10 12:32:51 +000091 platform.data_psn->clear(COLOR_BLACK);
92
alexander3c798932021-03-26 21:42:19 +000093 /* Strings for presentation/logging. */
94 std::string str_inf{"Running inference... "};
95
96 /* Copy over the data. */
alexanderc350cdc2021-04-29 20:36:09 +010097 LoadImageIntoTensor(ctx.Get<uint32_t>("imgIndex"), inputTensor);
alexander3c798932021-03-26 21:42:19 +000098
99 /* Display this image on the LCD. */
100 platform.data_psn->present_data_image(
Isabella Gottardi79d41542021-10-20 15:52:32 +0100101 static_cast<uint8_t *>(inputTensor->data.data),
alexander3c798932021-03-26 21:42:19 +0000102 nCols, nRows, nChannels,
103 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
104
105 /* If the data is signed. */
106 if (model.IsDataSigned()) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100107 image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes);
alexander3c798932021-03-26 21:42:19 +0000108 }
109
110 /* Display message on the LCD - inference running. */
111 platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(),
alexander31ae9f02022-02-10 16:15:54 +0000112 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
alexander3c798932021-03-26 21:42:19 +0000113
114 /* Run inference over this image. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100115 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
alexander3c798932021-03-26 21:42:19 +0000116 get_filename(ctx.Get<uint32_t>("imgIndex")));
117
alexander27b62d92021-05-04 20:46:08 +0100118 if (!RunInference(model, profiler)) {
119 return false;
120 }
alexander3c798932021-03-26 21:42:19 +0000121
122 /* Erase. */
123 str_inf = std::string(str_inf.size(), ' ');
124 platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(),
alexander31ae9f02022-02-10 16:15:54 +0000125 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
alexander3c798932021-03-26 21:42:19 +0000126
127 auto& classifier = ctx.Get<ImgClassClassifier&>("classifier");
128 classifier.GetClassificationResults(outputTensor, results,
129 ctx.Get<std::vector <std::string>&>("labels"),
alexander31ae9f02022-02-10 16:15:54 +0000130 5, false);
alexander3c798932021-03-26 21:42:19 +0000131
132 /* Add results to context for access outside handler. */
133 ctx.Set<std::vector<ClassificationResult>>("results", results);
134
135#if VERIFY_TEST_OUTPUT
136 arm::app::DumpTensor(outputTensor);
137#endif /* VERIFY_TEST_OUTPUT */
138
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100139 if (!image::PresentInferenceResult(platform, results)) {
alexander3c798932021-03-26 21:42:19 +0000140 return false;
141 }
142
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100143 profiler.PrintProfilingResult();
144
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100145 IncrementAppCtxIfmIdx(ctx,"imgIndex");
alexander3c798932021-03-26 21:42:19 +0000146
147 } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx);
148
149 return true;
150 }
151
alexanderc350cdc2021-04-29 20:36:09 +0100152 static bool LoadImageIntoTensor(uint32_t imIdx, TfLiteTensor* inputTensor)
alexander3c798932021-03-26 21:42:19 +0000153 {
154 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
155 inputTensor->bytes : IMAGE_DATA_SIZE;
156 const uint8_t* imgSrc = get_img_array(imIdx);
157 if (nullptr == imgSrc) {
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100158 printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", imIdx,
alexander3c798932021-03-26 21:42:19 +0000159 NUMBER_OF_FILES - 1);
160 return false;
161 }
162
163 memcpy(inputTensor->data.data, imgSrc, copySz);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100164 debug("Image %" PRIu32 " loaded\n", imIdx);
alexander3c798932021-03-26 21:42:19 +0000165 return true;
166 }
167
alexander3c798932021-03-26 21:42:19 +0000168
169} /* namespace app */
170} /* namespace arm */