blob: a44a401ab30180ad9fea86ad3d95a417b71e8c36 [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 {
Liam Barry213a5432022-05-09 17:06:19 +010027namespace app {
28 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
29 namespace img_class {
30 extern uint8_t* GetModelPointer();
31 extern size_t GetModelLen();
32 } /* namespace img_class */
33} /* namespace app */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010034} /* namespace arm */
35
alexander3c798932021-03-26 21:42:19 +000036using ImgClassClassifier = arm::app::Classifier;
37
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010038void main_loop()
alexander3c798932021-03-26 21:42:19 +000039{
40 arm::app::MobileNetModel model; /* Model wrapper object. */
41
42 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010043 if (!model.Init(arm::app::tensorArena,
44 sizeof(arm::app::tensorArena),
Liam Barry213a5432022-05-09 17:06:19 +010045 arm::app::img_class::GetModelPointer(),
46 arm::app::img_class::GetModelLen())) {
alexander3c798932021-03-26 21:42:19 +000047 printf_err("Failed to initialise model\n");
48 return;
49 }
50
51 /* Instantiate application context. */
52 arm::app::ApplicationContext caseContext;
53
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010054 arm::app::Profiler profiler{"img_class"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010055 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000056 caseContext.Set<arm::app::Model&>("model", model);
57 caseContext.Set<uint32_t>("imgIndex", 0);
58
59 ImgClassClassifier classifier; /* Classifier wrapper object. */
60 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
61
Isabella Gottardi3107aa22022-01-27 16:39:37 +000062 std::vector<std::string> labels;
alexander3c798932021-03-26 21:42:19 +000063 GetLabelsVector(labels);
64 caseContext.Set<const std::vector <std::string>&>("labels", labels);
65
66 /* Loop. */
67 bool executionSuccessful = true;
68 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
69
70 /* Loop. */
71 do {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010072 int menuOption = common::MENU_OPT_RUN_INF_NEXT;
alexander3c798932021-03-26 21:42:19 +000073 if (bUseMenu) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010074 DisplayCommonMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010075 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +000076 printf("\n");
77 }
78 switch (menuOption) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010079 case common::MENU_OPT_RUN_INF_NEXT:
alexander3c798932021-03-26 21:42:19 +000080 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
81 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010082 case common::MENU_OPT_RUN_INF_CHOSEN: {
alexander3c798932021-03-26 21:42:19 +000083 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +010084 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010085 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +000086 executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false);
87 break;
88 }
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010089 case common::MENU_OPT_RUN_INF_ALL:
alexander3c798932021-03-26 21:42:19 +000090 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
91 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010092 case common::MENU_OPT_SHOW_MODEL_INFO:
alexander3c798932021-03-26 21:42:19 +000093 executionSuccessful = model.ShowModelInfoHandler();
94 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010095 case common::MENU_OPT_LIST_IFM:
alexander3c798932021-03-26 21:42:19 +000096 executionSuccessful = ListFilesHandler(caseContext);
97 break;
98 default:
99 printf("Incorrect choice, try again.");
100 break;
101 }
102 } while (executionSuccessful && bUseMenu);
103 info("Main loop terminated.\n");
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000104}