blob: de3779f6c7f8609e3f422647f0801e583b7643b7 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Isabella Gottardi3107aa22022-01-27 16:39:37 +00002 * Copyright (c) 2021 - 2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 "hal.h" /* Brings in platform definitions. */
18#include "Classifier.hpp" /* Classifier. */
19#include "InputFiles.hpp" /* For input images. */
20#include "Labels.hpp" /* For label strings. */
21#include "MobileNetModel.hpp" /* Model class for running inference. */
22#include "UseCaseHandler.hpp" /* Handlers for different user options. */
23#include "UseCaseCommonUtils.hpp" /* Utils functions. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010024#include "BufAttributes.hpp" /* Buffer attributes to be applied */
25
26namespace arm {
27 namespace app {
28 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
29 } /* namespace app */
30} /* namespace arm */
31
32extern uint8_t* GetModelPointer();
33extern size_t GetModelLen();
alexander3c798932021-03-26 21:42:19 +000034
35using ImgClassClassifier = arm::app::Classifier;
36
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010037void main_loop()
alexander3c798932021-03-26 21:42:19 +000038{
39 arm::app::MobileNetModel model; /* Model wrapper object. */
40
41 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010042 if (!model.Init(arm::app::tensorArena,
43 sizeof(arm::app::tensorArena),
44 GetModelPointer(),
45 GetModelLen())) {
alexander3c798932021-03-26 21:42:19 +000046 printf_err("Failed to initialise model\n");
47 return;
48 }
49
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010050#if !defined(ARM_NPU)
51 /* If it is not a NPU build check if the model contains a NPU operator */
52 if (model.ContainsEthosUOperator()) {
53 printf_err("No driver support for Ethos-U operator found in the model.\n");
54 return;
55 }
56#endif /* ARM_NPU */
57
alexander3c798932021-03-26 21:42:19 +000058 /* Instantiate application context. */
59 arm::app::ApplicationContext caseContext;
60
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010061 arm::app::Profiler profiler{"img_class"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010062 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000063 caseContext.Set<arm::app::Model&>("model", model);
64 caseContext.Set<uint32_t>("imgIndex", 0);
65
66 ImgClassClassifier classifier; /* Classifier wrapper object. */
67 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
68
Isabella Gottardi3107aa22022-01-27 16:39:37 +000069 std::vector<std::string> labels;
alexander3c798932021-03-26 21:42:19 +000070 GetLabelsVector(labels);
71 caseContext.Set<const std::vector <std::string>&>("labels", labels);
72
73 /* Loop. */
74 bool executionSuccessful = true;
75 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
76
77 /* Loop. */
78 do {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010079 int menuOption = common::MENU_OPT_RUN_INF_NEXT;
alexander3c798932021-03-26 21:42:19 +000080 if (bUseMenu) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010081 DisplayCommonMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010082 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +000083 printf("\n");
84 }
85 switch (menuOption) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010086 case common::MENU_OPT_RUN_INF_NEXT:
alexander3c798932021-03-26 21:42:19 +000087 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
88 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010089 case common::MENU_OPT_RUN_INF_CHOSEN: {
alexander3c798932021-03-26 21:42:19 +000090 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +010091 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010092 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +000093 executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false);
94 break;
95 }
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010096 case common::MENU_OPT_RUN_INF_ALL:
alexander3c798932021-03-26 21:42:19 +000097 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
98 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010099 case common::MENU_OPT_SHOW_MODEL_INFO:
alexander3c798932021-03-26 21:42:19 +0000100 executionSuccessful = model.ShowModelInfoHandler();
101 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100102 case common::MENU_OPT_LIST_IFM:
alexander3c798932021-03-26 21:42:19 +0000103 executionSuccessful = ListFilesHandler(caseContext);
104 break;
105 default:
106 printf("Incorrect choice, try again.");
107 break;
108 }
109 } while (executionSuccessful && bUseMenu);
110 info("Main loop terminated.\n");
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000111}