blob: d8339b6ef8656a10509f1a44ee86c480aff381cd [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
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010027namespace arm {
28 namespace app {
29 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
30 } /* namespace app */
31} /* namespace arm */
32
33extern uint8_t* GetModelPointer();
34extern size_t GetModelLen();
35
alexander3c798932021-03-26 21:42:19 +000036TEST_CASE("Model info")
37{
38 /* Model wrapper object. */
39 arm::app::MobileNetModel model;
40
41 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010042 REQUIRE(model.Init(arm::app::tensorArena,
43 sizeof(arm::app::tensorArena),
44 GetModelPointer(),
45 GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000046
47 /* Instantiate application context. */
48 arm::app::ApplicationContext caseContext;
49
50 caseContext.Set<arm::app::Model&>("model", model);
51
52 REQUIRE(model.ShowModelInfoHandler());
53}
54
55
56TEST_CASE("Inference by index", "[.]")
57{
alexander3c798932021-03-26 21:42:19 +000058 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010059 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +000060
61 /* Model wrapper object. */
Isabella Gottardi8df12f32021-04-07 17:15:31 +010062 arm::app::MobileNetModel model;
alexander3c798932021-03-26 21:42:19 +000063
64 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010065 REQUIRE(model.Init(arm::app::tensorArena,
66 sizeof(arm::app::tensorArena),
67 GetModelPointer(),
68 GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000069
70 /* Instantiate application context. */
71 arm::app::ApplicationContext caseContext;
72
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010073 arm::app::Profiler profiler{"img_class"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010074 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000075 caseContext.Set<arm::app::Model&>("model", model);
76 caseContext.Set<uint32_t>("imgIndex", 0);
77 arm::app::Classifier classifier; /* Classifier wrapper object. */
78 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
79
80 std::vector <std::string> labels;
81 GetLabelsVector(labels);
82 caseContext.Set<const std::vector <std::string>&>("labels", labels);
83
84 REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, false));
85
86 auto results = caseContext.Get<std::vector<arm::app::ClassificationResult>>("results");
87
88 REQUIRE(results[0].m_labelIdx == 282);
89}
90
91
92TEST_CASE("Inference run all images", "[.]")
93{
alexander3c798932021-03-26 21:42:19 +000094 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010095 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +000096
97 /* Model wrapper object. */
98 arm::app::MobileNetModel model;
99
100 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100101 REQUIRE(model.Init(arm::app::tensorArena,
102 sizeof(arm::app::tensorArena),
103 GetModelPointer(),
104 GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000105
106 /* Instantiate application context. */
107 arm::app::ApplicationContext caseContext;
108
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100109 arm::app::Profiler profiler{"img_class"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100110 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +0000111 caseContext.Set<arm::app::Model&>("model", model);
112 caseContext.Set<uint32_t>("imgIndex", 0);
113 arm::app::Classifier classifier; /* classifier wrapper object. */
114 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
115
116 std::vector <std::string> labels;
117 GetLabelsVector(labels);
118 caseContext.Set<const std::vector <std::string>&>("labels", labels);
119
120 REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, true));
121}
122
123
124TEST_CASE("List all images")
125{
alexander3c798932021-03-26 21:42:19 +0000126 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100127 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +0000128
129 /* Model wrapper object. */
130 arm::app::MobileNetModel model;
131
132 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100133 REQUIRE(model.Init(arm::app::tensorArena,
134 sizeof(arm::app::tensorArena),
135 GetModelPointer(),
136 GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000137
138 /* Instantiate application context. */
139 arm::app::ApplicationContext caseContext;
alexander3c798932021-03-26 21:42:19 +0000140 caseContext.Set<arm::app::Model&>("model", model);
141
142 REQUIRE(arm::app::ListFilesHandler(caseContext));
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100143}