Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 1 | /* |
Liam Barry | e958850 | 2022-01-25 14:31:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [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 | #include "VisualWakeWordModel.hpp" |
| 19 | #include "Classifier.hpp" |
| 20 | #include "InputFiles.hpp" |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 21 | #include "ImageUtils.hpp" |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 22 | #include "UseCaseCommonUtils.hpp" |
| 23 | #include "hal.h" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 24 | #include "log_macros.h" |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 25 | |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 28 | namespace arm { |
| 29 | namespace app { |
| 30 | |
| 31 | /** |
| 32 | * @brief Helper function to load the current image into the input |
| 33 | * tensor. |
| 34 | * @param[in] imIdx Image index (from the pool of images available |
| 35 | * to the application). |
| 36 | * @param[out] inputTensor Pointer to the input tensor to be populated. |
| 37 | * @return true if tensor is loaded, false otherwise. |
| 38 | **/ |
| 39 | static bool LoadImageIntoTensor(uint32_t imIdx, |
| 40 | TfLiteTensor *inputTensor); |
| 41 | |
| 42 | /* Image inference classification handler. */ |
| 43 | bool ClassifyImageHandler(ApplicationContext &ctx, uint32_t imgIndex, bool runAll) |
| 44 | { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 45 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
| 46 | |
| 47 | constexpr uint32_t dataPsnImgDownscaleFactor = 1; |
| 48 | constexpr uint32_t dataPsnImgStartX = 10; |
| 49 | constexpr uint32_t dataPsnImgStartY = 35; |
| 50 | |
| 51 | constexpr uint32_t dataPsnTxtInfStartX = 150; |
| 52 | constexpr uint32_t dataPsnTxtInfStartY = 70; |
| 53 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 54 | auto& model = ctx.Get<Model&>("model"); |
| 55 | |
| 56 | /* If the request has a valid size, set the image index. */ |
| 57 | if (imgIndex < NUMBER_OF_FILES) { |
| 58 | if (!SetAppCtxIfmIdx(ctx, imgIndex,"imgIndex")) { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | if (!model.IsInited()) { |
| 63 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | auto curImIdx = ctx.Get<uint32_t>("imgIndex"); |
| 68 | |
| 69 | TfLiteTensor *outputTensor = model.GetOutputTensor(0); |
| 70 | TfLiteTensor *inputTensor = model.GetInputTensor(0); |
| 71 | |
| 72 | if (!inputTensor->dims) { |
| 73 | printf_err("Invalid input tensor dims\n"); |
| 74 | return false; |
| 75 | } else if (inputTensor->dims->size < 3) { |
| 76 | printf_err("Input tensor dimension should be >= 3\n"); |
| 77 | return false; |
| 78 | } |
| 79 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 80 | const uint32_t nCols = inputShape->data[arm::app::VisualWakeWordModel::ms_inputColsIdx]; |
| 81 | const uint32_t nRows = inputShape->data[arm::app::VisualWakeWordModel::ms_inputRowsIdx]; |
| 82 | if (arm::app::VisualWakeWordModel::ms_inputChannelsIdx >= static_cast<uint32_t>(inputShape->size)) { |
| 83 | printf_err("Invalid channel index.\n"); |
| 84 | return false; |
| 85 | } |
| 86 | const uint32_t nChannels = inputShape->data[arm::app::VisualWakeWordModel::ms_inputChannelsIdx]; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 87 | |
| 88 | std::vector<ClassificationResult> results; |
| 89 | |
| 90 | do { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame^] | 91 | hal_lcd_clear(COLOR_BLACK); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 92 | |
| 93 | /* Strings for presentation/logging. */ |
| 94 | std::string str_inf{"Running inference... "}; |
| 95 | |
| 96 | /* Copy over the data. */ |
| 97 | LoadImageIntoTensor(ctx.Get<uint32_t>("imgIndex"), inputTensor); |
| 98 | |
| 99 | /* Display this image on the LCD. */ |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame^] | 100 | hal_lcd_display_image( |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 101 | static_cast<uint8_t *>(inputTensor->data.data), |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 102 | nCols, nRows, nChannels, |
| 103 | dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor); |
| 104 | |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 105 | /* Vww model preprocessing is image conversion from uint8 to [0,1] float values, |
| 106 | * then quantize them with input quantization info. */ |
| 107 | QuantParams inQuantParams = GetTensorQuantParams(inputTensor); |
| 108 | |
| 109 | auto* req_data = static_cast<uint8_t *>(inputTensor->data.data); |
| 110 | auto* signed_req_data = static_cast<int8_t *>(inputTensor->data.data); |
| 111 | for (size_t i = 0; i < inputTensor->bytes; i++) { |
| 112 | auto i_data_int8 = static_cast<int8_t>(((static_cast<float>(req_data[i]) / 255.0f) / inQuantParams.scale) + inQuantParams.offset); |
| 113 | signed_req_data[i] = std::min<int8_t>(INT8_MAX, std::max<int8_t>(i_data_int8, INT8_MIN)); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* Display message on the LCD - inference running. */ |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame^] | 117 | hal_lcd_display_text( |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 118 | str_inf.c_str(), str_inf.size(), |
| 119 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
| 120 | |
| 121 | /* Run inference over this image. */ |
| 122 | info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"), |
| 123 | get_filename(ctx.Get<uint32_t>("imgIndex"))); |
| 124 | |
| 125 | if (!RunInference(model, profiler)) { |
| 126 | return false; |
| 127 | } |
| 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( |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 132 | str_inf.c_str(), str_inf.size(), |
| 133 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
| 134 | |
| 135 | auto& classifier = ctx.Get<Classifier&>("classifier"); |
| 136 | classifier.GetClassificationResults(outputTensor, results, |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 137 | ctx.Get<std::vector <std::string>&>("labels"), 1, |
| 138 | false); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 139 | |
| 140 | /* Add results to context for access outside handler. */ |
| 141 | ctx.Set<std::vector<ClassificationResult>>("results", results); |
| 142 | |
| 143 | #if VERIFY_TEST_OUTPUT |
| 144 | arm::app::DumpTensor(outputTensor); |
| 145 | #endif /* VERIFY_TEST_OUTPUT */ |
| 146 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame^] | 147 | if (!PresentInferenceResult(results)) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | |
| 151 | profiler.PrintProfilingResult(); |
| 152 | IncrementAppCtxIfmIdx(ctx,"imgIndex"); |
| 153 | |
| 154 | } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx); |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | static bool LoadImageIntoTensor(const uint32_t imIdx, |
| 160 | TfLiteTensor *inputTensor) |
| 161 | { |
| 162 | const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ? |
| 163 | inputTensor->bytes : IMAGE_DATA_SIZE; |
| 164 | if (imIdx >= NUMBER_OF_FILES) { |
| 165 | printf_err("invalid image index %" PRIu32 " (max: %u)\n", imIdx, |
| 166 | NUMBER_OF_FILES - 1); |
| 167 | return false; |
| 168 | } |
| 169 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 170 | if (arm::app::VisualWakeWordModel::ms_inputChannelsIdx >= static_cast<uint32_t>(inputTensor->dims->size)) { |
| 171 | printf_err("Invalid channel index.\n"); |
| 172 | return false; |
| 173 | } |
| 174 | const uint32_t nChannels = inputTensor->dims->data[arm::app::VisualWakeWordModel::ms_inputChannelsIdx]; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 175 | |
| 176 | const uint8_t* srcPtr = get_img_array(imIdx); |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 177 | auto* dstPtr = static_cast<uint8_t *>(inputTensor->data.data); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 178 | if (1 == nChannels) { |
| 179 | /** |
| 180 | * Visual Wake Word model accepts only one channel => |
| 181 | * Convert image to grayscale here |
| 182 | **/ |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 183 | image::RgbToGrayscale(srcPtr, dstPtr, copySz); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 184 | } else { |
| 185 | memcpy(inputTensor->data.data, srcPtr, copySz); |
| 186 | } |
| 187 | |
| 188 | debug("Image %" PRIu32 " loaded\n", imIdx); |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | } /* namespace app */ |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 193 | } /* namespace arm */ |