blob: 62d8eb8d349427c31bf105330ee34565a16945bc [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 "ClassificationResult.hpp"
18#include "Classifier.hpp"
19#include "hal.h"
20#include "Labels.hpp"
21#include "MobileNetModel.hpp"
22#include "UseCaseHandler.hpp"
23#include "UseCaseCommonUtils.hpp"
24
25#include <catch.hpp>
26
27TEST_CASE("Model info")
28{
29 /* Model wrapper object. */
30 arm::app::MobileNetModel model;
31
32 /* Load the model. */
33 REQUIRE(model.Init());
34
35 /* Instantiate application context. */
36 arm::app::ApplicationContext caseContext;
37
38 caseContext.Set<arm::app::Model&>("model", model);
39
40 REQUIRE(model.ShowModelInfoHandler());
41}
42
43
44TEST_CASE("Inference by index", "[.]")
45{
46 hal_platform platform;
alexander3c798932021-03-26 21:42:19 +000047 platform_timer timer;
48
49 /* Initialise the HAL and platform. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010050 hal_init(&platform, &timer);
alexander3c798932021-03-26 21:42:19 +000051 hal_platform_init(&platform);
52
53 /* Model wrapper object. */
Isabella Gottardi8df12f32021-04-07 17:15:31 +010054 arm::app::MobileNetModel model;
alexander3c798932021-03-26 21:42:19 +000055
56 /* Load the model. */
57 REQUIRE(model.Init());
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 arm::app::Classifier 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 REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, false));
75
76 auto results = caseContext.Get<std::vector<arm::app::ClassificationResult>>("results");
77
78 REQUIRE(results[0].m_labelIdx == 282);
79}
80
81
82TEST_CASE("Inference run all images", "[.]")
83{
84 hal_platform platform;
alexander3c798932021-03-26 21:42:19 +000085 platform_timer timer;
86
87 /* Initialise the HAL and platform. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010088 hal_init(&platform, &timer);
alexander3c798932021-03-26 21:42:19 +000089 hal_platform_init(&platform);
90
91 /* Model wrapper object. */
92 arm::app::MobileNetModel model;
93
94 /* Load the model. */
95 REQUIRE(model.Init());
96
97 /* Instantiate application context. */
98 arm::app::ApplicationContext caseContext;
99
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100100 arm::app::Profiler profiler{&platform, "img_class"};
101 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +0000102 caseContext.Set<hal_platform&>("platform", platform);
103 caseContext.Set<arm::app::Model&>("model", model);
104 caseContext.Set<uint32_t>("imgIndex", 0);
105 arm::app::Classifier classifier; /* classifier wrapper object. */
106 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
107
108 std::vector <std::string> labels;
109 GetLabelsVector(labels);
110 caseContext.Set<const std::vector <std::string>&>("labels", labels);
111
112 REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, true));
113}
114
115
116TEST_CASE("List all images")
117{
118 hal_platform platform;
alexander3c798932021-03-26 21:42:19 +0000119 platform_timer timer;
120
121 /* Initialise the HAL and platform. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100122 hal_init(&platform, &timer);
alexander3c798932021-03-26 21:42:19 +0000123 hal_platform_init(&platform);
124
125 /* Model wrapper object. */
126 arm::app::MobileNetModel model;
127
128 /* Load the model. */
129 REQUIRE(model.Init());
130
131 /* Instantiate application context. */
132 arm::app::ApplicationContext caseContext;
133
134 caseContext.Set<hal_platform&>("platform", platform);
135 caseContext.Set<arm::app::Model&>("model", model);
136
137 REQUIRE(arm::app::ListFilesHandler(caseContext));
138}