blob: 52c42f3bddb6cd65c94ac072bf1f839fce0ae728 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Kshitij Sisodia2ea46232022-12-19 16:37:33 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates
3 * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0
alexander3c798932021-03-26 21:42:19 +00004 *
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"
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000020#include "ImageUtils.hpp"
21#include "ImgClassProcessing.hpp"
alexander3c798932021-03-26 21:42:19 +000022#include "InputFiles.hpp"
23#include "MobileNetModel.hpp"
24#include "UseCaseCommonUtils.hpp"
25#include "hal.h"
alexander31ae9f02022-02-10 16:15:54 +000026#include "log_macros.h"
alexander3c798932021-03-26 21:42:19 +000027
alexander31ae9f02022-02-10 16:15:54 +000028#include <cinttypes>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010029
alexander3c798932021-03-26 21:42:19 +000030using ImgClassClassifier = arm::app::Classifier;
31
32namespace arm {
33namespace app {
34
Richard Burton11b75cc2022-04-07 18:00:55 +010035 /* Image classification inference handler. */
alexander3c798932021-03-26 21:42:19 +000036 bool ClassifyImageHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
37 {
Isabella Gottardi8df12f32021-04-07 17:15:31 +010038 auto& profiler = ctx.Get<Profiler&>("profiler");
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000039 auto& model = ctx.Get<Model&>("model");
Richard Burtone6398cd2022-04-13 11:58:28 +010040 /* If the request has a valid size, set the image index as it might not be set. */
41 if (imgIndex < NUMBER_OF_FILES) {
42 if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) {
43 return false;
44 }
45 }
Richard Burtonc20be972022-04-19 17:01:08 +010046 auto initialImgIdx = ctx.Get<uint32_t>("imgIndex");
alexander3c798932021-03-26 21:42:19 +000047
48 constexpr uint32_t dataPsnImgDownscaleFactor = 2;
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000049 constexpr uint32_t dataPsnImgStartX = 10;
50 constexpr uint32_t dataPsnImgStartY = 35;
alexander3c798932021-03-26 21:42:19 +000051
52 constexpr uint32_t dataPsnTxtInfStartX = 150;
53 constexpr uint32_t dataPsnTxtInfStartY = 40;
54
alexander3c798932021-03-26 21:42:19 +000055 if (!model.IsInited()) {
56 printf_err("Model is not initialised! Terminating processing.\n");
57 return false;
58 }
59
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000060 TfLiteTensor* inputTensor = model.GetInputTensor(0);
Richard Burtonb40ecf82022-04-22 16:14:57 +010061 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
alexander3c798932021-03-26 21:42:19 +000062 if (!inputTensor->dims) {
63 printf_err("Invalid input tensor dims\n");
64 return false;
Richard Burtonc20be972022-04-19 17:01:08 +010065 } else if (inputTensor->dims->size < 4) {
66 printf_err("Input tensor dimension should be = 4\n");
alexander3c798932021-03-26 21:42:19 +000067 return false;
68 }
69
Richard Burton11b75cc2022-04-07 18:00:55 +010070 /* Get input shape for displaying the image. */
alexander3c798932021-03-26 21:42:19 +000071 TfLiteIntArray* inputShape = model.GetInputShape(0);
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000072 const uint32_t nCols = inputShape->data[arm::app::MobileNetModel::ms_inputColsIdx];
73 const uint32_t nRows = inputShape->data[arm::app::MobileNetModel::ms_inputRowsIdx];
alexander3c798932021-03-26 21:42:19 +000074 const uint32_t nChannels = inputShape->data[arm::app::MobileNetModel::ms_inputChannelsIdx];
75
Richard Burton11b75cc2022-04-07 18:00:55 +010076 /* Set up pre and post-processing. */
Richard Burtonb40ecf82022-04-22 16:14:57 +010077 ImgClassPreProcess preProcess = ImgClassPreProcess(inputTensor, model.IsDataSigned());
Richard Burton11b75cc2022-04-07 18:00:55 +010078
alexander3c798932021-03-26 21:42:19 +000079 std::vector<ClassificationResult> results;
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000080 ImgClassPostProcess postProcess =
81 ImgClassPostProcess(outputTensor,
82 ctx.Get<ImgClassClassifier&>("classifier"),
83 ctx.Get<std::vector<std::string>&>("labels"),
84 results);
alexander3c798932021-03-26 21:42:19 +000085
86 do {
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010087 hal_lcd_clear(COLOR_BLACK);
Richard Burton9b8d67a2021-12-10 12:32:51 +000088
alexander3c798932021-03-26 21:42:19 +000089 /* Strings for presentation/logging. */
90 std::string str_inf{"Running inference... "};
91
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000092 const uint8_t* imgSrc = GetImgArray(ctx.Get<uint32_t>("imgIndex"));
Richard Burton11b75cc2022-04-07 18:00:55 +010093 if (nullptr == imgSrc) {
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000094 printf_err("Failed to get image index %" PRIu32 " (max: %u)\n",
95 ctx.Get<uint32_t>("imgIndex"),
Richard Burton11b75cc2022-04-07 18:00:55 +010096 NUMBER_OF_FILES - 1);
97 return false;
98 }
alexander3c798932021-03-26 21:42:19 +000099
100 /* Display this image on the LCD. */
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000101 hal_lcd_display_image(imgSrc,
102 nCols,
103 nRows,
104 nChannels,
105 dataPsnImgStartX,
106 dataPsnImgStartY,
107 dataPsnImgDownscaleFactor);
alexander3c798932021-03-26 21:42:19 +0000108
alexander3c798932021-03-26 21:42:19 +0000109 /* Display message on the LCD - inference running. */
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000110 hal_lcd_display_text(
111 str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
alexander3c798932021-03-26 21:42:19 +0000112
Richard Burton11b75cc2022-04-07 18:00:55 +0100113 /* Select the image to run inference with. */
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000114 info("Running inference on image %" PRIu32 " => %s\n",
115 ctx.Get<uint32_t>("imgIndex"),
116 GetFilename(ctx.Get<uint32_t>("imgIndex")));
alexander3c798932021-03-26 21:42:19 +0000117
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000118 const size_t imgSz =
119 inputTensor->bytes < IMAGE_DATA_SIZE ? inputTensor->bytes : IMAGE_DATA_SIZE;
Richard Burton11b75cc2022-04-07 18:00:55 +0100120
121 /* Run the pre-processing, inference and post-processing. */
Richard Burtonb40ecf82022-04-22 16:14:57 +0100122 if (!preProcess.DoPreProcess(imgSrc, imgSz)) {
123 printf_err("Pre-processing failed.");
Richard Burton11b75cc2022-04-07 18:00:55 +0100124 return false;
125 }
126
Richard Burtonb40ecf82022-04-22 16:14:57 +0100127 if (!RunInference(model, profiler)) {
128 printf_err("Inference failed.");
Richard Burton11b75cc2022-04-07 18:00:55 +0100129 return false;
130 }
Richard Burton11b75cc2022-04-07 18:00:55 +0100131
Richard Burtonb40ecf82022-04-22 16:14:57 +0100132 if (!postProcess.DoPostProcess()) {
133 printf_err("Post-processing failed.");
alexander27b62d92021-05-04 20:46:08 +0100134 return false;
135 }
alexander3c798932021-03-26 21:42:19 +0000136
137 /* Erase. */
138 str_inf = std::string(str_inf.size(), ' ');
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000139 hal_lcd_display_text(
140 str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
alexander3c798932021-03-26 21:42:19 +0000141
alexander3c798932021-03-26 21:42:19 +0000142 /* Add results to context for access outside handler. */
143 ctx.Set<std::vector<ClassificationResult>>("results", results);
144
145#if VERIFY_TEST_OUTPUT
146 arm::app::DumpTensor(outputTensor);
147#endif /* VERIFY_TEST_OUTPUT */
148
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100149 if (!PresentInferenceResult(results)) {
alexander3c798932021-03-26 21:42:19 +0000150 return false;
151 }
152
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100153 profiler.PrintProfilingResult();
154
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000155 IncrementAppCtxIfmIdx(ctx, "imgIndex");
alexander3c798932021-03-26 21:42:19 +0000156
Richard Burtonc20be972022-04-19 17:01:08 +0100157 } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx);
alexander3c798932021-03-26 21:42:19 +0000158
159 return true;
160 }
161
alexander3c798932021-03-26 21:42:19 +0000162} /* namespace app */
163} /* namespace arm */