Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [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 | #include "VisualWakeWordModel.hpp" |
| 19 | #include "Classifier.hpp" |
| 20 | #include "InputFiles.hpp" |
| 21 | #include "UseCaseCommonUtils.hpp" |
| 22 | #include "hal.h" |
| 23 | |
| 24 | namespace arm { |
| 25 | namespace app { |
| 26 | |
| 27 | /** |
| 28 | * @brief Helper function to load the current image into the input |
| 29 | * tensor. |
| 30 | * @param[in] imIdx Image index (from the pool of images available |
| 31 | * to the application). |
| 32 | * @param[out] inputTensor Pointer to the input tensor to be populated. |
| 33 | * @return true if tensor is loaded, false otherwise. |
| 34 | **/ |
| 35 | static bool LoadImageIntoTensor(uint32_t imIdx, |
| 36 | TfLiteTensor *inputTensor); |
| 37 | |
| 38 | /* Image inference classification handler. */ |
| 39 | bool ClassifyImageHandler(ApplicationContext &ctx, uint32_t imgIndex, bool runAll) |
| 40 | { |
| 41 | auto& platform = ctx.Get<hal_platform &>("platform"); |
| 42 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
| 43 | |
| 44 | constexpr uint32_t dataPsnImgDownscaleFactor = 1; |
| 45 | constexpr uint32_t dataPsnImgStartX = 10; |
| 46 | constexpr uint32_t dataPsnImgStartY = 35; |
| 47 | |
| 48 | constexpr uint32_t dataPsnTxtInfStartX = 150; |
| 49 | constexpr uint32_t dataPsnTxtInfStartY = 70; |
| 50 | |
| 51 | |
| 52 | platform.data_psn->clear(COLOR_BLACK); |
| 53 | time_t infTimeMs = 0; |
| 54 | |
| 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) { |
| 59 | if (!SetAppCtxIfmIdx(ctx, imgIndex,"imgIndex")) { |
| 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 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
| 81 | const uint32_t nCols = inputShape->data[2]; |
| 82 | const uint32_t nRows = inputShape->data[1]; |
| 83 | const uint32_t nChannels = (inputShape->size == 4) ? inputShape->data[3] : 1; |
| 84 | |
| 85 | std::vector<ClassificationResult> results; |
| 86 | |
| 87 | do { |
| 88 | |
| 89 | /* Strings for presentation/logging. */ |
| 90 | std::string str_inf{"Running inference... "}; |
| 91 | |
| 92 | /* Copy over the data. */ |
| 93 | LoadImageIntoTensor(ctx.Get<uint32_t>("imgIndex"), inputTensor); |
| 94 | |
| 95 | /* Display this image on the LCD. */ |
| 96 | platform.data_psn->present_data_image( |
| 97 | (uint8_t *) inputTensor->data.data, |
| 98 | nCols, nRows, nChannels, |
| 99 | dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor); |
| 100 | |
| 101 | /* If the data is signed. */ |
| 102 | if (model.IsDataSigned()) { |
| 103 | image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes); |
| 104 | } |
| 105 | |
| 106 | /* Display message on the LCD - inference running. */ |
| 107 | platform.data_psn->present_data_text( |
| 108 | str_inf.c_str(), str_inf.size(), |
| 109 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
| 110 | |
| 111 | /* Run inference over this image. */ |
| 112 | info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"), |
| 113 | get_filename(ctx.Get<uint32_t>("imgIndex"))); |
| 114 | |
| 115 | if (!RunInference(model, profiler)) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | /* Erase. */ |
| 120 | str_inf = std::string(str_inf.size(), ' '); |
| 121 | platform.data_psn->present_data_text( |
| 122 | str_inf.c_str(), str_inf.size(), |
| 123 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
| 124 | |
| 125 | auto& classifier = ctx.Get<Classifier&>("classifier"); |
| 126 | classifier.GetClassificationResults(outputTensor, results, |
| 127 | ctx.Get<std::vector <std::string>&>("labels"), 1); |
| 128 | |
| 129 | /* Add results to context for access outside handler. */ |
| 130 | ctx.Set<std::vector<ClassificationResult>>("results", results); |
| 131 | |
| 132 | #if VERIFY_TEST_OUTPUT |
| 133 | arm::app::DumpTensor(outputTensor); |
| 134 | #endif /* VERIFY_TEST_OUTPUT */ |
| 135 | |
| 136 | if (!image::PresentInferenceResult(platform, results, infTimeMs)) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | profiler.PrintProfilingResult(); |
| 141 | IncrementAppCtxIfmIdx(ctx,"imgIndex"); |
| 142 | |
| 143 | } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx); |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | static bool LoadImageIntoTensor(const uint32_t imIdx, |
| 149 | TfLiteTensor *inputTensor) |
| 150 | { |
| 151 | const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ? |
| 152 | inputTensor->bytes : IMAGE_DATA_SIZE; |
| 153 | if (imIdx >= NUMBER_OF_FILES) { |
| 154 | printf_err("invalid image index %" PRIu32 " (max: %u)\n", imIdx, |
| 155 | NUMBER_OF_FILES - 1); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | const uint32_t nChannels = (inputTensor->dims->size == 4) ? inputTensor->dims->data[3] : 1; |
| 160 | |
| 161 | const uint8_t* srcPtr = get_img_array(imIdx); |
| 162 | auto* dstPtr = (uint8_t*)inputTensor->data.data; |
| 163 | if (1 == nChannels) { |
| 164 | /** |
| 165 | * Visual Wake Word model accepts only one channel => |
| 166 | * Convert image to grayscale here |
| 167 | **/ |
| 168 | for (size_t i = 0; i < copySz; ++i, srcPtr += 3) { |
| 169 | *dstPtr++ = 0.2989*(*srcPtr) + |
| 170 | 0.587*(*(srcPtr+1)) + |
| 171 | 0.114*(*(srcPtr+2)); |
| 172 | } |
| 173 | } else { |
| 174 | memcpy(inputTensor->data.data, srcPtr, copySz); |
| 175 | } |
| 176 | |
| 177 | debug("Image %" PRIu32 " loaded\n", imIdx); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | } /* namespace app */ |
| 182 | } /* namespace arm */ |