blob: ac4ea47c84e2b41b049b814d91f5b701d7ac6e3e [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
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 "TestModel.hpp"
20#include "UseCaseCommonUtils.hpp"
21#include "hal.h"
22
23#include <cstdlib>
24
25namespace arm {
26namespace app {
27
28 bool RunInferenceHandler(ApplicationContext& ctx)
29 {
30 auto& platform = ctx.Get<hal_platform&>("platform");
31 auto& model = ctx.Get<Model&>("model");
32
33 constexpr uint32_t dataPsnTxtInfStartX = 150;
34 constexpr uint32_t dataPsnTxtInfStartY = 40;
35
36 if (!model.IsInited()) {
37 printf_err("Model is not initialised! Terminating processing.\n");
38 return false;
39 }
40
41 const size_t numInputs = model.GetNumInputs();
42
43 /* Populate each input tensor with random data. */
44 for (size_t inputIndex = 0; inputIndex < numInputs; inputIndex++) {
45
46 TfLiteTensor* inputTensor = model.GetInputTensor(inputIndex);
47
48 debug("Populating input tensor %zu@%p\n", inputIndex, inputTensor);
49 debug("Total input size to be populated: %zu\n", inputTensor->bytes);
50
51 /* Create a random input. */
52 if (inputTensor->bytes > 0) {
53
54 uint8_t* tData = tflite::GetTensorData<uint8_t>(inputTensor);
55
56 for (size_t j = 0; j < inputTensor->bytes; ++j) {
57 tData[j] = static_cast<uint8_t>(std::rand() & 0xFF);
58 }
59 }
60 }
61
62 /* Strings for presentation/logging. */
63 std::string str_inf{"Running inference... "};
64
65 /* Display message on the LCD - inference running. */
66 platform.data_psn->present_data_text(
67 str_inf.c_str(), str_inf.size(),
68 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
69
70 RunInference(platform, model);
71
72 /* Erase. */
73 str_inf = std::string(str_inf.size(), ' ');
74 platform.data_psn->present_data_text(
75 str_inf.c_str(), str_inf.size(),
76 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
77
78#if VERIFY_TEST_OUTPUT
79 for (size_t outputIndex = 0; outputIndex < model.GetNumOutputs(); outputIndex++) {
80 arm::app::DumpTensor(model.GetOutputTensor(outputIndex));
81 }
82#endif /* VERIFY_TEST_OUTPUT */
83
84 return true;
85 }
86
87} /* namespace app */
88} /* namespace arm */