blob: 86ea2ea87459c4a7fa1fc251131a6ee1d6eedb2b [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
50 /* Instantiate application context. */
51 arm::app::ApplicationContext caseContext;
52
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010053 arm::app::Profiler profiler{"img_class"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010054 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000055 caseContext.Set<arm::app::Model&>("model", model);
56 caseContext.Set<uint32_t>("imgIndex", 0);
57
58 ImgClassClassifier classifier; /* Classifier wrapper object. */
59 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
60
Isabella Gottardi3107aa22022-01-27 16:39:37 +000061 std::vector<std::string> labels;
alexander3c798932021-03-26 21:42:19 +000062 GetLabelsVector(labels);
63 caseContext.Set<const std::vector <std::string>&>("labels", labels);
64
65 /* Loop. */
66 bool executionSuccessful = true;
67 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
68
69 /* Loop. */
70 do {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010071 int menuOption = common::MENU_OPT_RUN_INF_NEXT;
alexander3c798932021-03-26 21:42:19 +000072 if (bUseMenu) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010073 DisplayCommonMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010074 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +000075 printf("\n");
76 }
77 switch (menuOption) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010078 case common::MENU_OPT_RUN_INF_NEXT:
alexander3c798932021-03-26 21:42:19 +000079 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
80 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010081 case common::MENU_OPT_RUN_INF_CHOSEN: {
alexander3c798932021-03-26 21:42:19 +000082 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +010083 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010084 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +000085 executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false);
86 break;
87 }
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010088 case common::MENU_OPT_RUN_INF_ALL:
alexander3c798932021-03-26 21:42:19 +000089 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
90 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010091 case common::MENU_OPT_SHOW_MODEL_INFO:
alexander3c798932021-03-26 21:42:19 +000092 executionSuccessful = model.ShowModelInfoHandler();
93 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010094 case common::MENU_OPT_LIST_IFM:
alexander3c798932021-03-26 21:42:19 +000095 executionSuccessful = ListFilesHandler(caseContext);
96 break;
97 default:
98 printf("Incorrect choice, try again.");
99 break;
100 }
101 } while (executionSuccessful && bUseMenu);
102 info("Main loop terminated.\n");
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000103}