alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates |
| 3 | * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0 |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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" |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 20 | #include "ImageUtils.hpp" |
| 21 | #include "ImgClassProcessing.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 22 | #include "InputFiles.hpp" |
| 23 | #include "MobileNetModel.hpp" |
| 24 | #include "UseCaseCommonUtils.hpp" |
| 25 | #include "hal.h" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 26 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 27 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 28 | #include <cinttypes> |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 29 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 30 | using ImgClassClassifier = arm::app::Classifier; |
| 31 | |
| 32 | namespace arm { |
| 33 | namespace app { |
| 34 | |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 35 | /* Image classification inference handler. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 36 | bool ClassifyImageHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll) |
| 37 | { |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 38 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 39 | auto& model = ctx.Get<Model&>("model"); |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 40 | /* 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 Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 46 | auto initialImgIdx = ctx.Get<uint32_t>("imgIndex"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 47 | |
| 48 | constexpr uint32_t dataPsnImgDownscaleFactor = 2; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 49 | constexpr uint32_t dataPsnImgStartX = 10; |
| 50 | constexpr uint32_t dataPsnImgStartY = 35; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | |
| 52 | constexpr uint32_t dataPsnTxtInfStartX = 150; |
| 53 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
| 54 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 55 | if (!model.IsInited()) { |
| 56 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 57 | return false; |
| 58 | } |
| 59 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 60 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 61 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 62 | if (!inputTensor->dims) { |
| 63 | printf_err("Invalid input tensor dims\n"); |
| 64 | return false; |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 65 | } else if (inputTensor->dims->size < 4) { |
| 66 | printf_err("Input tensor dimension should be = 4\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 70 | /* Get input shape for displaying the image. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 72 | const uint32_t nCols = inputShape->data[arm::app::MobileNetModel::ms_inputColsIdx]; |
| 73 | const uint32_t nRows = inputShape->data[arm::app::MobileNetModel::ms_inputRowsIdx]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 74 | const uint32_t nChannels = inputShape->data[arm::app::MobileNetModel::ms_inputChannelsIdx]; |
| 75 | |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 76 | /* Set up pre and post-processing. */ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 77 | ImgClassPreProcess preProcess = ImgClassPreProcess(inputTensor, model.IsDataSigned()); |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 78 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 79 | std::vector<ClassificationResult> results; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 80 | ImgClassPostProcess postProcess = |
| 81 | ImgClassPostProcess(outputTensor, |
| 82 | ctx.Get<ImgClassClassifier&>("classifier"), |
| 83 | ctx.Get<std::vector<std::string>&>("labels"), |
| 84 | results); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 85 | |
| 86 | do { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 87 | hal_lcd_clear(COLOR_BLACK); |
Richard Burton | 9b8d67a | 2021-12-10 12:32:51 +0000 | [diff] [blame] | 88 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 89 | /* Strings for presentation/logging. */ |
| 90 | std::string str_inf{"Running inference... "}; |
| 91 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 92 | const uint8_t* imgSrc = GetImgArray(ctx.Get<uint32_t>("imgIndex")); |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 93 | if (nullptr == imgSrc) { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 94 | printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", |
| 95 | ctx.Get<uint32_t>("imgIndex"), |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 96 | NUMBER_OF_FILES - 1); |
| 97 | return false; |
| 98 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 99 | |
| 100 | /* Display this image on the LCD. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 101 | hal_lcd_display_image(imgSrc, |
| 102 | nCols, |
| 103 | nRows, |
| 104 | nChannels, |
| 105 | dataPsnImgStartX, |
| 106 | dataPsnImgStartY, |
| 107 | dataPsnImgDownscaleFactor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 108 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 109 | /* Display message on the LCD - inference running. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 110 | hal_lcd_display_text( |
| 111 | str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 112 | |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 113 | /* Select the image to run inference with. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 114 | info("Running inference on image %" PRIu32 " => %s\n", |
| 115 | ctx.Get<uint32_t>("imgIndex"), |
| 116 | GetFilename(ctx.Get<uint32_t>("imgIndex"))); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 117 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 118 | const size_t imgSz = |
| 119 | inputTensor->bytes < IMAGE_DATA_SIZE ? inputTensor->bytes : IMAGE_DATA_SIZE; |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 120 | |
| 121 | /* Run the pre-processing, inference and post-processing. */ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 122 | if (!preProcess.DoPreProcess(imgSrc, imgSz)) { |
| 123 | printf_err("Pre-processing failed."); |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 127 | if (!RunInference(model, profiler)) { |
| 128 | printf_err("Inference failed."); |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 129 | return false; |
| 130 | } |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 131 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 132 | if (!postProcess.DoPostProcess()) { |
| 133 | printf_err("Post-processing failed."); |
alexander | 27b62d9 | 2021-05-04 20:46:08 +0100 | [diff] [blame] | 134 | return false; |
| 135 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 136 | |
| 137 | /* Erase. */ |
| 138 | str_inf = std::string(str_inf.size(), ' '); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 139 | hal_lcd_display_text( |
| 140 | str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 141 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 142 | /* 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 Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 149 | if (!PresentInferenceResult(results)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 153 | profiler.PrintProfilingResult(); |
| 154 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 155 | IncrementAppCtxIfmIdx(ctx, "imgIndex"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 156 | |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 157 | } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 162 | } /* namespace app */ |
| 163 | } /* namespace arm */ |