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" |
| 24 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 25 | #include <inttypes.h> |
| 26 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 27 | using ImgClassClassifier = arm::app::Classifier; |
| 28 | |
| 29 | namespace arm { |
| 30 | namespace app { |
| 31 | |
| 32 | /** |
| 33 | * @brief Helper function to load the current image into the input |
| 34 | * tensor. |
| 35 | * @param[in] imIdx Image index (from the pool of images available |
| 36 | * to the application). |
| 37 | * @param[out] inputTensor Pointer to the input tensor to be populated. |
| 38 | * @return true if tensor is loaded, false otherwise. |
| 39 | **/ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 40 | static bool LoadImageIntoTensor(uint32_t imIdx, TfLiteTensor* inputTensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 41 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 42 | /* Image inference classification handler. */ |
| 43 | bool ClassifyImageHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll) |
| 44 | { |
| 45 | auto& platform = ctx.Get<hal_platform&>("platform"); |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 46 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 55 | auto& model = ctx.Get<Model&>("model"); |
| 56 | |
| 57 | /* If the request has a valid size, set the image index. */ |
| 58 | if (imgIndex < NUMBER_OF_FILES) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 59 | if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | if (!model.IsInited()) { |
| 64 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | auto curImIdx = ctx.Get<uint32_t>("imgIndex"); |
| 69 | |
| 70 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
| 71 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 72 | |
| 73 | if (!inputTensor->dims) { |
| 74 | printf_err("Invalid input tensor dims\n"); |
| 75 | return false; |
| 76 | } else if (inputTensor->dims->size < 3) { |
| 77 | printf_err("Input tensor dimension should be >= 3\n"); |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
| 82 | |
| 83 | const uint32_t nCols = inputShape->data[arm::app::MobileNetModel::ms_inputColsIdx]; |
| 84 | const uint32_t nRows = inputShape->data[arm::app::MobileNetModel::ms_inputRowsIdx]; |
| 85 | const uint32_t nChannels = inputShape->data[arm::app::MobileNetModel::ms_inputChannelsIdx]; |
| 86 | |
| 87 | std::vector<ClassificationResult> results; |
| 88 | |
| 89 | do { |
Richard Burton | 9b8d67a | 2021-12-10 12:32:51 +0000 | [diff] [blame] | 90 | platform.data_psn->clear(COLOR_BLACK); |
| 91 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | /* Strings for presentation/logging. */ |
| 93 | std::string str_inf{"Running inference... "}; |
| 94 | |
| 95 | /* Copy over the data. */ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 96 | LoadImageIntoTensor(ctx.Get<uint32_t>("imgIndex"), inputTensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 97 | |
| 98 | /* Display this image on the LCD. */ |
| 99 | platform.data_psn->present_data_image( |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 100 | static_cast<uint8_t *>(inputTensor->data.data), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 101 | nCols, nRows, nChannels, |
| 102 | dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor); |
| 103 | |
| 104 | /* If the data is signed. */ |
| 105 | if (model.IsDataSigned()) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 106 | image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* Display message on the LCD - inference running. */ |
| 110 | platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(), |
| 111 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
| 112 | |
| 113 | /* Run inference over this image. */ |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 114 | info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 115 | get_filename(ctx.Get<uint32_t>("imgIndex"))); |
| 116 | |
alexander | 27b62d9 | 2021-05-04 20:46:08 +0100 | [diff] [blame] | 117 | if (!RunInference(model, profiler)) { |
| 118 | return false; |
| 119 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 120 | |
| 121 | /* Erase. */ |
| 122 | str_inf = std::string(str_inf.size(), ' '); |
| 123 | platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(), |
| 124 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
| 125 | |
| 126 | auto& classifier = ctx.Get<ImgClassClassifier&>("classifier"); |
| 127 | classifier.GetClassificationResults(outputTensor, results, |
| 128 | ctx.Get<std::vector <std::string>&>("labels"), |
| 129 | 5); |
| 130 | |
| 131 | /* Add results to context for access outside handler. */ |
| 132 | ctx.Set<std::vector<ClassificationResult>>("results", results); |
| 133 | |
| 134 | #if VERIFY_TEST_OUTPUT |
| 135 | arm::app::DumpTensor(outputTensor); |
| 136 | #endif /* VERIFY_TEST_OUTPUT */ |
| 137 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 138 | if (!image::PresentInferenceResult(platform, results)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 142 | profiler.PrintProfilingResult(); |
| 143 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 144 | IncrementAppCtxIfmIdx(ctx,"imgIndex"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 145 | |
| 146 | } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx); |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 151 | static bool LoadImageIntoTensor(uint32_t imIdx, TfLiteTensor* inputTensor) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 152 | { |
| 153 | const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ? |
| 154 | inputTensor->bytes : IMAGE_DATA_SIZE; |
| 155 | const uint8_t* imgSrc = get_img_array(imIdx); |
| 156 | if (nullptr == imgSrc) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 157 | printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", imIdx, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 158 | NUMBER_OF_FILES - 1); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | memcpy(inputTensor->data.data, imgSrc, copySz); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 163 | debug("Image %" PRIu32 " loaded\n", imIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 167 | |
| 168 | } /* namespace app */ |
| 169 | } /* namespace arm */ |