alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame^] | 24 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 25 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame^] | 26 | #include <cinttypes> |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 27 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 28 | using ImgClassClassifier = arm::app::Classifier; |
| 29 | |
| 30 | namespace arm { |
| 31 | namespace 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 | **/ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 41 | static bool LoadImageIntoTensor(uint32_t imIdx, TfLiteTensor* inputTensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 42 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 43 | /* Image inference classification handler. */ |
| 44 | bool ClassifyImageHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll) |
| 45 | { |
| 46 | auto& platform = ctx.Get<hal_platform&>("platform"); |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 47 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 48 | |
| 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 56 | 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áin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 60 | if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 61 | 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 Burton | 9b8d67a | 2021-12-10 12:32:51 +0000 | [diff] [blame] | 91 | platform.data_psn->clear(COLOR_BLACK); |
| 92 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 93 | /* Strings for presentation/logging. */ |
| 94 | std::string str_inf{"Running inference... "}; |
| 95 | |
| 96 | /* Copy over the data. */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 97 | LoadImageIntoTensor(ctx.Get<uint32_t>("imgIndex"), inputTensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 98 | |
| 99 | /* Display this image on the LCD. */ |
| 100 | platform.data_psn->present_data_image( |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 101 | static_cast<uint8_t *>(inputTensor->data.data), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 102 | nCols, nRows, nChannels, |
| 103 | dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor); |
| 104 | |
| 105 | /* If the data is signed. */ |
| 106 | if (model.IsDataSigned()) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 107 | image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* Display message on the LCD - inference running. */ |
| 111 | platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(), |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame^] | 112 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 113 | |
| 114 | /* Run inference over this image. */ |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 115 | info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 116 | get_filename(ctx.Get<uint32_t>("imgIndex"))); |
| 117 | |
alexander | 27b62d9 | 2021-05-04 20:46:08 +0100 | [diff] [blame] | 118 | if (!RunInference(model, profiler)) { |
| 119 | return false; |
| 120 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 121 | |
| 122 | /* Erase. */ |
| 123 | str_inf = std::string(str_inf.size(), ' '); |
| 124 | platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(), |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame^] | 125 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 126 | |
| 127 | auto& classifier = ctx.Get<ImgClassClassifier&>("classifier"); |
| 128 | classifier.GetClassificationResults(outputTensor, results, |
| 129 | ctx.Get<std::vector <std::string>&>("labels"), |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame^] | 130 | 5, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 131 | |
| 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áin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 139 | if (!image::PresentInferenceResult(platform, results)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 143 | profiler.PrintProfilingResult(); |
| 144 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 145 | IncrementAppCtxIfmIdx(ctx,"imgIndex"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 146 | |
| 147 | } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx); |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 152 | static bool LoadImageIntoTensor(uint32_t imIdx, TfLiteTensor* inputTensor) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 153 | { |
| 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 Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 158 | printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", imIdx, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 159 | NUMBER_OF_FILES - 1); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | memcpy(inputTensor->data.data, imgSrc, copySz); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 164 | debug("Image %" PRIu32 " loaded\n", imIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 168 | |
| 169 | } /* namespace app */ |
| 170 | } /* namespace arm */ |