blob: a2b3fb74a8e4fe385c50cc39c5f2fc033ddf3954 [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");
Isabella Gottardi8df12f32021-04-07 17:15:31 +010031 auto& profiler = ctx.Get<Profiler&>("profiler");
alexander3c798932021-03-26 21:42:19 +000032 auto& model = ctx.Get<Model&>("model");
33
34 constexpr uint32_t dataPsnTxtInfStartX = 150;
35 constexpr uint32_t dataPsnTxtInfStartY = 40;
36
37 if (!model.IsInited()) {
38 printf_err("Model is not initialised! Terminating processing.\n");
39 return false;
40 }
41
42 const size_t numInputs = model.GetNumInputs();
43
44 /* Populate each input tensor with random data. */
45 for (size_t inputIndex = 0; inputIndex < numInputs; inputIndex++) {
46
47 TfLiteTensor* inputTensor = model.GetInputTensor(inputIndex);
48
49 debug("Populating input tensor %zu@%p\n", inputIndex, inputTensor);
50 debug("Total input size to be populated: %zu\n", inputTensor->bytes);
51
52 /* Create a random input. */
53 if (inputTensor->bytes > 0) {
54
55 uint8_t* tData = tflite::GetTensorData<uint8_t>(inputTensor);
56
57 for (size_t j = 0; j < inputTensor->bytes; ++j) {
58 tData[j] = static_cast<uint8_t>(std::rand() & 0xFF);
59 }
60 }
61 }
62
63 /* Strings for presentation/logging. */
64 std::string str_inf{"Running inference... "};
65
66 /* Display message on the LCD - inference running. */
67 platform.data_psn->present_data_text(
68 str_inf.c_str(), str_inf.size(),
69 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
70
Isabella Gottardi8df12f32021-04-07 17:15:31 +010071 RunInference(model, profiler);
alexander3c798932021-03-26 21:42:19 +000072
73 /* Erase. */
74 str_inf = std::string(str_inf.size(), ' ');
75 platform.data_psn->present_data_text(
76 str_inf.c_str(), str_inf.size(),
77 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
78
Isabella Gottardi8df12f32021-04-07 17:15:31 +010079 info("Final results:\n");
80 profiler.PrintProfilingResult();
81
alexander3c798932021-03-26 21:42:19 +000082#if VERIFY_TEST_OUTPUT
83 for (size_t outputIndex = 0; outputIndex < model.GetNumOutputs(); outputIndex++) {
84 arm::app::DumpTensor(model.GetOutputTensor(outputIndex));
85 }
86#endif /* VERIFY_TEST_OUTPUT */
87
88 return true;
89 }
90
91} /* namespace app */
92} /* namespace arm */