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