blob: d0a8a3f016a473abbc11982e19ef6b0117309b3f [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 <catch.hpp>
Kshitij Sisodia76a15802021-12-24 11:05:11 +000018#include "MicroNetKwsModel.hpp"
alexander3c798932021-03-26 21:42:19 +000019#include "hal.h"
20
21#include "KwsResult.hpp"
22#include "Labels.hpp"
23#include "UseCaseHandler.hpp"
24#include "Classifier.hpp"
25#include "UseCaseCommonUtils.hpp"
26
27TEST_CASE("Model info")
28{
29 /* Model wrapper object. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000030 arm::app::MicroNetKwsModel model;
alexander3c798932021-03-26 21:42:19 +000031
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{
alexander3c798932021-03-26 21:42:19 +000046 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010047 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +000048
49 /* Model wrapper object. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000050 arm::app::MicroNetKwsModel model;
alexander3c798932021-03-26 21:42:19 +000051
52 /* Load the model. */
53 REQUIRE(model.Init());
54
55 /* Instantiate application context. */
56 arm::app::ApplicationContext caseContext;
Isabella Gottardi8df12f32021-04-07 17:15:31 +010057
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010058 arm::app::Profiler profiler{"kws"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010059 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000060 caseContext.Set<arm::app::Model&>("model", model);
Kshitij Sisodia76a15802021-12-24 11:05:11 +000061 caseContext.Set<int>("frameLength", g_FrameLength); /* 640 sample length for MicroNetKws. */
62 caseContext.Set<int>("frameStride", g_FrameStride); /* 320 sample stride for MicroNetKws. */
alexander3c798932021-03-26 21:42:19 +000063 caseContext.Set<float>("scoreThreshold", 0.5); /* Normalised score threshold. */
64
65 arm::app::Classifier classifier; /* classifier wrapper object. */
66 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
67
68 auto checker = [&](uint32_t audioIndex, std::vector<uint32_t> labelIndex)
69 {
70 caseContext.Set<uint32_t>("audioIndex", audioIndex);
71
72 std::vector<std::string> labels;
73 GetLabelsVector(labels);
74 caseContext.Set<const std::vector<std::string> &>("labels", labels);
75
76 REQUIRE(arm::app::ClassifyAudioHandler(caseContext, audioIndex, false));
77 REQUIRE(caseContext.Has("results"));
78
79 auto results = caseContext.Get<std::vector<arm::app::kws::KwsResult>>("results");
80
81 REQUIRE(results.size() == labelIndex.size());
82
83 for (size_t i = 0; i < results.size(); i++ ) {
84 REQUIRE(results[i].m_resultVec.size());
85 REQUIRE(results[i].m_resultVec[0].m_labelIdx == labelIndex[i]);
86 }
87
88 };
89
90 SECTION("Index = 0, short clip down")
91 {
92 /* Result: down. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000093 checker(0, {0});
alexander3c798932021-03-26 21:42:19 +000094 }
95
96 SECTION("Index = 1, long clip right->left->up")
97 {
98 /* Result: right->right->left->up->up. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000099 checker(1, {6, 6, 2, 8, 8});
alexander3c798932021-03-26 21:42:19 +0000100 }
101
102 SECTION("Index = 2, short clip yes")
103 {
104 /* Result: yes. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000105 checker(2, {9});
alexander3c798932021-03-26 21:42:19 +0000106 }
107
108 SECTION("Index = 3, long clip yes->no->go->stop")
109 {
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000110 /* Result: yes->no->no->go->go->stop->stop. */
111 checker(3, {9, 3, 3, 1, 1, 7, 7});
alexander3c798932021-03-26 21:42:19 +0000112 }
113}
114
115
116TEST_CASE("Inference run all clips")
117{
alexander3c798932021-03-26 21:42:19 +0000118 /* Initialise the HAL and platform. */
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100119 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +0000120
121 /* Model wrapper object. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000122 arm::app::MicroNetKwsModel model;
alexander3c798932021-03-26 21:42:19 +0000123
124 /* Load the model. */
125 REQUIRE(model.Init());
126
127 /* Instantiate application context. */
128 arm::app::ApplicationContext caseContext;
129
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100130 arm::app::Profiler profiler{"kws"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100131 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +0000132 caseContext.Set<arm::app::Model&>("model", model);
133 caseContext.Set<uint32_t>("clipIndex", 0);
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000134 caseContext.Set<int>("frameLength", g_FrameLength); /* 640 sample length for MicroNet. */
135 caseContext.Set<int>("frameStride", g_FrameStride); /* 320 sample stride for MicroNet. */
136 caseContext.Set<float>("scoreThreshold", 0.7); /* Normalised score threshold. */
alexander3c798932021-03-26 21:42:19 +0000137 arm::app::Classifier classifier; /* classifier wrapper object. */
138 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
139
140 std::vector <std::string> labels;
141 GetLabelsVector(labels);
142 caseContext.Set<const std::vector <std::string>&>("labels", labels);
143 REQUIRE(arm::app::ClassifyAudioHandler(caseContext, 0, true));
144}
145
146
147TEST_CASE("List all audio clips")
148{
Kshitij Sisodia4cc40212022-04-08 09:54:53 +0100149 /* Initialise the HAL and platform. */
150 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +0000151
152 /* Model wrapper object. */
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000153 arm::app::MicroNetKwsModel model;
alexander3c798932021-03-26 21:42:19 +0000154
155 /* Load the model. */
156 REQUIRE(model.Init());
157
158 /* Instantiate application context. */
159 arm::app::ApplicationContext caseContext;
160
alexander3c798932021-03-26 21:42:19 +0000161 caseContext.Set<arm::app::Model&>("model", model);
162
163 REQUIRE(arm::app::ListFilesHandler(caseContext));
164}