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 "UseCaseCommonUtils.hpp" |
| 18 | |
| 19 | #include "InputFiles.hpp" |
| 20 | |
| 21 | namespace arm { |
| 22 | namespace app { |
| 23 | |
| 24 | bool RunInference(hal_platform& platform, arm::app::Model& model) |
| 25 | { |
| 26 | Profiler profiler{&platform, "Inference"}; |
| 27 | profiler.StartProfiling(); |
| 28 | |
| 29 | bool runInf = model.RunInference(); |
| 30 | |
| 31 | profiler.StopProfiling(); |
| 32 | std::string profileResults = profiler.GetResultsAndReset(); |
| 33 | info("%s\n", profileResults.c_str()); |
| 34 | |
| 35 | return runInf; |
| 36 | } |
| 37 | |
| 38 | int ReadUserInputAsInt(hal_platform& platform) |
| 39 | { |
| 40 | char chInput[128]; |
| 41 | memset(chInput, 0, sizeof(chInput)); |
| 42 | |
| 43 | platform.data_acq->get_input(chInput, sizeof(chInput)); |
| 44 | return atoi(chInput); |
| 45 | } |
| 46 | |
| 47 | void DumpTensor(TfLiteTensor* tensor, const size_t lineBreakForNumElements) |
| 48 | { |
| 49 | char strhex[8]; |
| 50 | std::string strdump; |
| 51 | |
| 52 | if (!tensor) { |
| 53 | printf_err("invalid tensor\n"); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | const uint32_t tensorSz = tensor->bytes; |
| 58 | const uint8_t* tensorData = tflite::GetTensorData<uint8_t>(tensor); |
| 59 | |
| 60 | for (size_t i = 0; i < tensorSz; ++i) { |
| 61 | if (0 == i % lineBreakForNumElements) { |
| 62 | printf("%s\n\t", strdump.c_str()); |
| 63 | strdump.clear(); |
| 64 | } |
| 65 | snprintf(strhex, sizeof(strhex) - 1, |
| 66 | "0x%02x, ", tensorData[i]); |
| 67 | strdump += std::string(strhex); |
| 68 | } |
| 69 | |
| 70 | if (strdump.size()) { |
| 71 | printf("%s\n", strdump.c_str()); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | bool ListFilesHandler(ApplicationContext& ctx) |
| 76 | { |
| 77 | auto& model = ctx.Get<Model&>("model"); |
| 78 | auto& platform = ctx.Get<hal_platform&>("platform"); |
| 79 | |
| 80 | constexpr uint32_t dataPsnTxtStartX = 20; |
| 81 | constexpr uint32_t dataPsnTxtStartY = 40; |
| 82 | |
| 83 | if (!model.IsInited()) { |
| 84 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | /* Clear the LCD */ |
| 89 | platform.data_psn->clear(COLOR_BLACK); |
| 90 | |
| 91 | /* Show the total number of embedded files. */ |
| 92 | std::string strNumFiles = std::string{"Total Number of Files: "} + |
| 93 | std::to_string(NUMBER_OF_FILES); |
| 94 | platform.data_psn->present_data_text(strNumFiles.c_str(), |
| 95 | strNumFiles.size(), |
| 96 | dataPsnTxtStartX, |
| 97 | dataPsnTxtStartY, |
| 98 | 0); |
| 99 | |
| 100 | #if NUMBER_OF_FILES > 0 |
| 101 | constexpr uint32_t dataPsnTxtYIncr = 16; |
| 102 | info("List of Files:\n"); |
| 103 | uint32_t yVal = dataPsnTxtStartY + dataPsnTxtYIncr; |
| 104 | for (uint32_t i = 0; i < NUMBER_OF_FILES; ++i, yVal += dataPsnTxtYIncr) { |
| 105 | |
| 106 | std::string currentFilename{get_filename(i)}; |
| 107 | platform.data_psn->present_data_text(currentFilename.c_str(), |
| 108 | currentFilename.size(), |
| 109 | dataPsnTxtStartX, yVal, 0); |
| 110 | |
| 111 | info("\t%u => %s\n", i, currentFilename.c_str()); |
| 112 | } |
| 113 | #endif /* NUMBER_OF_FILES > 0 */ |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | } /* namespace app */ |
| 119 | } /* namespace arm */ |