blob: 1685e5fa50759a8c6ca1f7a27ae4b07f4d171d1e [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 {
Liam Barry213a5432022-05-09 17:06:19 +010028namespace app {
29 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
30 namespace img_class {
31 extern uint8_t* GetModelPointer();
32 extern size_t GetModelLen();
33 } /* namespace img_class */
34} /* namespace app */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010035} /* namespace arm */
36
alexander3c798932021-03-26 21:42:19 +000037TEST_CASE("Model info")
38{
39 /* Model wrapper object. */
40 arm::app::MobileNetModel model;
41
42 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010043 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +010044 sizeof(arm::app::tensorArena),
45 arm::app::img_class::GetModelPointer(),
46 arm::app::img_class::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000047
48 /* Instantiate application context. */
49 arm::app::ApplicationContext caseContext;
50
51 caseContext.Set<arm::app::Model&>("model", model);
52
53 REQUIRE(model.ShowModelInfoHandler());
54}
55
56
57TEST_CASE("Inference by index", "[.]")
58{
alexander3c798932021-03-26 21:42:19 +000059 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010060 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +000061
62 /* Model wrapper object. */
Isabella Gottardi8df12f32021-04-07 17:15:31 +010063 arm::app::MobileNetModel model;
alexander3c798932021-03-26 21:42:19 +000064
65 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010066 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +010067 sizeof(arm::app::tensorArena),
68 arm::app::img_class::GetModelPointer(),
69 arm::app::img_class::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000070
71 /* Instantiate application context. */
72 arm::app::ApplicationContext caseContext;
73
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010074 arm::app::Profiler profiler{"img_class"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010075 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000076 caseContext.Set<arm::app::Model&>("model", model);
77 caseContext.Set<uint32_t>("imgIndex", 0);
78 arm::app::Classifier classifier; /* Classifier wrapper object. */
79 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
80
81 std::vector <std::string> labels;
82 GetLabelsVector(labels);
83 caseContext.Set<const std::vector <std::string>&>("labels", labels);
84
85 REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, false));
86
87 auto results = caseContext.Get<std::vector<arm::app::ClassificationResult>>("results");
88
89 REQUIRE(results[0].m_labelIdx == 282);
90}
91
92
93TEST_CASE("Inference run all images", "[.]")
94{
alexander3c798932021-03-26 21:42:19 +000095 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010096 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +000097
98 /* Model wrapper object. */
99 arm::app::MobileNetModel model;
100
101 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100102 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100103 sizeof(arm::app::tensorArena),
104 arm::app::img_class::GetModelPointer(),
105 arm::app::img_class::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000106
107 /* Instantiate application context. */
108 arm::app::ApplicationContext caseContext;
109
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100110 arm::app::Profiler profiler{"img_class"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100111 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +0000112 caseContext.Set<arm::app::Model&>("model", model);
113 caseContext.Set<uint32_t>("imgIndex", 0);
114 arm::app::Classifier classifier; /* classifier wrapper object. */
115 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
116
117 std::vector <std::string> labels;
118 GetLabelsVector(labels);
119 caseContext.Set<const std::vector <std::string>&>("labels", labels);
120
121 REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, true));
122}
123
124
125TEST_CASE("List all images")
126{
alexander3c798932021-03-26 21:42:19 +0000127 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100128 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +0000129
130 /* Model wrapper object. */
131 arm::app::MobileNetModel model;
132
133 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100134 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100135 sizeof(arm::app::tensorArena),
136 arm::app::img_class::GetModelPointer(),
137 arm::app::img_class::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000138
139 /* Instantiate application context. */
140 arm::app::ApplicationContext caseContext;
alexander3c798932021-03-26 21:42:19 +0000141 caseContext.Set<arm::app::Model&>("model", model);
142
143 REQUIRE(arm::app::ListFilesHandler(caseContext));
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100144}