blob: c22013c5f67b1d8dd1902302597651ad06d9aaa8 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00003 * 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"
Richard Burtonec5e99b2022-10-05 11:00:37 +010024#include "BufAttributes.hpp"
alexander3c798932021-03-26 21:42:19 +000025
26#include <catch.hpp>
27
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010028namespace arm {
Liam Barry213a5432022-05-09 17:06:19 +010029namespace app {
30 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
31 namespace img_class {
32 extern uint8_t* GetModelPointer();
33 extern size_t GetModelLen();
34 } /* namespace img_class */
35} /* namespace app */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010036} /* namespace arm */
37
alexander3c798932021-03-26 21:42:19 +000038TEST_CASE("Model info")
39{
40 /* Model wrapper object. */
41 arm::app::MobileNetModel model;
42
43 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010044 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +010045 sizeof(arm::app::tensorArena),
46 arm::app::img_class::GetModelPointer(),
47 arm::app::img_class::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000048
49 /* Instantiate application context. */
50 arm::app::ApplicationContext caseContext;
51
52 caseContext.Set<arm::app::Model&>("model", model);
53
54 REQUIRE(model.ShowModelInfoHandler());
55}
56
57
58TEST_CASE("Inference by index", "[.]")
59{
alexander3c798932021-03-26 21:42:19 +000060 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010061 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +000062
63 /* Model wrapper object. */
Isabella Gottardi8df12f32021-04-07 17:15:31 +010064 arm::app::MobileNetModel model;
alexander3c798932021-03-26 21:42:19 +000065
66 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010067 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +010068 sizeof(arm::app::tensorArena),
69 arm::app::img_class::GetModelPointer(),
70 arm::app::img_class::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000071
72 /* Instantiate application context. */
73 arm::app::ApplicationContext caseContext;
74
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010075 arm::app::Profiler profiler{"img_class"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010076 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000077 caseContext.Set<arm::app::Model&>("model", model);
78 caseContext.Set<uint32_t>("imgIndex", 0);
79 arm::app::Classifier classifier; /* Classifier wrapper object. */
80 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
81
82 std::vector <std::string> labels;
83 GetLabelsVector(labels);
84 caseContext.Set<const std::vector <std::string>&>("labels", labels);
85
86 REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, false));
87
88 auto results = caseContext.Get<std::vector<arm::app::ClassificationResult>>("results");
89
90 REQUIRE(results[0].m_labelIdx == 282);
91}
92
93
94TEST_CASE("Inference run all images", "[.]")
95{
alexander3c798932021-03-26 21:42:19 +000096 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010097 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +000098
99 /* Model wrapper object. */
100 arm::app::MobileNetModel model;
101
102 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100103 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100104 sizeof(arm::app::tensorArena),
105 arm::app::img_class::GetModelPointer(),
106 arm::app::img_class::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000107
108 /* Instantiate application context. */
109 arm::app::ApplicationContext caseContext;
110
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100111 arm::app::Profiler profiler{"img_class"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100112 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +0000113 caseContext.Set<arm::app::Model&>("model", model);
114 caseContext.Set<uint32_t>("imgIndex", 0);
115 arm::app::Classifier classifier; /* classifier wrapper object. */
116 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
117
118 std::vector <std::string> labels;
119 GetLabelsVector(labels);
120 caseContext.Set<const std::vector <std::string>&>("labels", labels);
121
122 REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, true));
123}
124
125
126TEST_CASE("List all images")
127{
alexander3c798932021-03-26 21:42:19 +0000128 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100129 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +0000130
131 /* Model wrapper object. */
132 arm::app::MobileNetModel model;
133
134 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100135 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100136 sizeof(arm::app::tensorArena),
137 arm::app::img_class::GetModelPointer(),
138 arm::app::img_class::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000139
140 /* Instantiate application context. */
141 arm::app::ApplicationContext caseContext;
alexander3c798932021-03-26 21:42:19 +0000142 caseContext.Set<arm::app::Model&>("model", model);
143
144 REQUIRE(arm::app::ListFilesHandler(caseContext));
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100145}