blob: 255f8e0755b70da7aaddfddc5a6aff87d35bfe11 [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 "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. */
24
25using ImgClassClassifier = arm::app::Classifier;
26
27enum opcodes
28{
29 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
30 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
31 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
32 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
33 MENU_OPT_LIST_IMAGES /* List the current baked images. */
34};
35
36static void DisplayMenu()
37{
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010038 printf("\n\n");
39 printf("User input required\n");
alexander3c798932021-03-26 21:42:19 +000040 printf("Enter option number from:\n\n");
41 printf(" %u. Classify next image\n", MENU_OPT_RUN_INF_NEXT);
42 printf(" %u. Classify image at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
43 printf(" %u. Run classification on all images\n", MENU_OPT_RUN_INF_ALL);
44 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
45 printf(" %u. List images\n\n", MENU_OPT_LIST_IMAGES);
46 printf(" Choice: ");
47}
48
49void main_loop(hal_platform& platform)
50{
51 arm::app::MobileNetModel model; /* Model wrapper object. */
52
53 /* Load the model. */
54 if (!model.Init()) {
55 printf_err("Failed to initialise model\n");
56 return;
57 }
58
59 /* Instantiate application context. */
60 arm::app::ApplicationContext caseContext;
61
Isabella Gottardi8df12f32021-04-07 17:15:31 +010062 arm::app::Profiler profiler{&platform, "img_class"};
63 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000064 caseContext.Set<hal_platform&>("platform", platform);
65 caseContext.Set<arm::app::Model&>("model", model);
66 caseContext.Set<uint32_t>("imgIndex", 0);
67
68 ImgClassClassifier classifier; /* Classifier wrapper object. */
69 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
70
71 std::vector <std::string> labels;
72 GetLabelsVector(labels);
73 caseContext.Set<const std::vector <std::string>&>("labels", labels);
74
75 /* Loop. */
76 bool executionSuccessful = true;
77 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
78
79 /* Loop. */
80 do {
81 int menuOption = MENU_OPT_RUN_INF_NEXT;
82 if (bUseMenu) {
83 DisplayMenu();
84 menuOption = arm::app::ReadUserInputAsInt(platform);
85 printf("\n");
86 }
87 switch (menuOption) {
88 case MENU_OPT_RUN_INF_NEXT:
89 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
90 break;
91 case MENU_OPT_RUN_INF_CHOSEN: {
92 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
93 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt(platform));
94 executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false);
95 break;
96 }
97 case MENU_OPT_RUN_INF_ALL:
98 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
99 break;
100 case MENU_OPT_SHOW_MODEL_INFO:
101 executionSuccessful = model.ShowModelInfoHandler();
102 break;
103 case MENU_OPT_LIST_IMAGES:
104 executionSuccessful = ListFilesHandler(caseContext);
105 break;
106 default:
107 printf("Incorrect choice, try again.");
108 break;
109 }
110 } while (executionSuccessful && bUseMenu);
111 info("Main loop terminated.\n");
112}