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