blob: 66d7064aded0dff5a4c5e761a9e437e53053df05 [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{
38 printf("\n\nUser input required\n");
39 printf("Enter option number from:\n\n");
40 printf(" %u. Classify next image\n", MENU_OPT_RUN_INF_NEXT);
41 printf(" %u. Classify image at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
42 printf(" %u. Run classification on all images\n", MENU_OPT_RUN_INF_ALL);
43 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
44 printf(" %u. List images\n\n", MENU_OPT_LIST_IMAGES);
45 printf(" Choice: ");
46}
47
48void main_loop(hal_platform& platform)
49{
50 arm::app::MobileNetModel model; /* Model wrapper object. */
51
52 /* Load the model. */
53 if (!model.Init()) {
54 printf_err("Failed to initialise model\n");
55 return;
56 }
57
58 /* Instantiate application context. */
59 arm::app::ApplicationContext caseContext;
60
Isabella Gottardi8df12f32021-04-07 17:15:31 +010061 arm::app::Profiler profiler{&platform, "img_class"};
62 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000063 caseContext.Set<hal_platform&>("platform", platform);
64 caseContext.Set<arm::app::Model&>("model", model);
65 caseContext.Set<uint32_t>("imgIndex", 0);
66
67 ImgClassClassifier classifier; /* Classifier wrapper object. */
68 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
69
70 std::vector <std::string> labels;
71 GetLabelsVector(labels);
72 caseContext.Set<const std::vector <std::string>&>("labels", labels);
73
74 /* Loop. */
75 bool executionSuccessful = true;
76 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
77
78 /* Loop. */
79 do {
80 int menuOption = MENU_OPT_RUN_INF_NEXT;
81 if (bUseMenu) {
82 DisplayMenu();
83 menuOption = arm::app::ReadUserInputAsInt(platform);
84 printf("\n");
85 }
86 switch (menuOption) {
87 case MENU_OPT_RUN_INF_NEXT:
88 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
89 break;
90 case MENU_OPT_RUN_INF_CHOSEN: {
91 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
92 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt(platform));
93 executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false);
94 break;
95 }
96 case MENU_OPT_RUN_INF_ALL:
97 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
98 break;
99 case MENU_OPT_SHOW_MODEL_INFO:
100 executionSuccessful = model.ShowModelInfoHandler();
101 break;
102 case MENU_OPT_LIST_IMAGES:
103 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");
111}