alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 18 | #include "MicroNetKwsModel.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 19 | #include "hal.h" |
| 20 | |
| 21 | #include "KwsResult.hpp" |
| 22 | #include "Labels.hpp" |
| 23 | #include "UseCaseHandler.hpp" |
| 24 | #include "Classifier.hpp" |
| 25 | #include "UseCaseCommonUtils.hpp" |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 26 | #include "BufAttributes.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 27 | |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 28 | namespace arm { |
| 29 | namespace app { |
| 30 | static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE; |
| 31 | namespace kws { |
| 32 | extern uint8_t* GetModelPointer(); |
| 33 | extern size_t GetModelLen(); |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 34 | } /* namespace kws */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 35 | } /* namespace app */ |
| 36 | } /* namespace arm */ |
| 37 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 38 | TEST_CASE("Model info") |
| 39 | { |
| 40 | /* Model wrapper object. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 41 | arm::app::MicroNetKwsModel model; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 42 | |
| 43 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 44 | REQUIRE(model.Init(arm::app::tensorArena, |
| 45 | sizeof(arm::app::tensorArena), |
| 46 | arm::app::kws::GetModelPointer(), |
| 47 | arm::app::kws::GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 48 | |
| 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 | |
| 58 | TEST_CASE("Inference by index") |
| 59 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 60 | /* Initialise the HAL and platform. */ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 61 | hal_platform_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 62 | |
| 63 | /* Model wrapper object. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 64 | arm::app::MicroNetKwsModel model; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 65 | |
| 66 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 67 | REQUIRE(model.Init(arm::app::tensorArena, |
| 68 | sizeof(arm::app::tensorArena), |
| 69 | arm::app::kws::GetModelPointer(), |
| 70 | arm::app::kws::GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | |
| 72 | /* Instantiate application context. */ |
| 73 | arm::app::ApplicationContext caseContext; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 74 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 75 | arm::app::Profiler profiler{"kws"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 76 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 77 | caseContext.Set<arm::app::Model&>("model", model); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 78 | caseContext.Set<int>("frameLength", arm::app::kws::g_FrameLength); /* 640 sample length for MicroNetKws. */ |
| 79 | caseContext.Set<int>("frameStride", arm::app::kws::g_FrameStride); /* 320 sample stride for MicroNetKws. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 80 | caseContext.Set<float>("scoreThreshold", 0.5); /* Normalised score threshold. */ |
| 81 | |
| 82 | arm::app::Classifier classifier; /* classifier wrapper object. */ |
| 83 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 84 | |
| 85 | auto checker = [&](uint32_t audioIndex, std::vector<uint32_t> labelIndex) |
| 86 | { |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 87 | caseContext.Set<uint32_t>("clipIndex", audioIndex); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 88 | |
| 89 | std::vector<std::string> labels; |
| 90 | GetLabelsVector(labels); |
| 91 | caseContext.Set<const std::vector<std::string> &>("labels", labels); |
| 92 | |
| 93 | REQUIRE(arm::app::ClassifyAudioHandler(caseContext, audioIndex, false)); |
| 94 | REQUIRE(caseContext.Has("results")); |
| 95 | |
| 96 | auto results = caseContext.Get<std::vector<arm::app::kws::KwsResult>>("results"); |
| 97 | |
| 98 | REQUIRE(results.size() == labelIndex.size()); |
| 99 | |
| 100 | for (size_t i = 0; i < results.size(); i++ ) { |
| 101 | REQUIRE(results[i].m_resultVec.size()); |
| 102 | REQUIRE(results[i].m_resultVec[0].m_labelIdx == labelIndex[i]); |
| 103 | } |
| 104 | |
| 105 | }; |
| 106 | |
| 107 | SECTION("Index = 0, short clip down") |
| 108 | { |
| 109 | /* Result: down. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 110 | checker(0, {0}); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | SECTION("Index = 1, long clip right->left->up") |
| 114 | { |
| 115 | /* Result: right->right->left->up->up. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 116 | checker(1, {6, 6, 2, 8, 8}); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | SECTION("Index = 2, short clip yes") |
| 120 | { |
| 121 | /* Result: yes. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 122 | checker(2, {9}); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | SECTION("Index = 3, long clip yes->no->go->stop") |
| 126 | { |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 127 | /* Result: yes->no->no->go->go->stop->stop. */ |
| 128 | checker(3, {9, 3, 3, 1, 1, 7, 7}); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | TEST_CASE("Inference run all clips") |
| 134 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 135 | /* Initialise the HAL and platform. */ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 136 | hal_platform_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 137 | |
| 138 | /* Model wrapper object. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 139 | arm::app::MicroNetKwsModel model; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | |
| 141 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 142 | REQUIRE(model.Init(arm::app::tensorArena, |
| 143 | sizeof(arm::app::tensorArena), |
| 144 | arm::app::kws::GetModelPointer(), |
| 145 | arm::app::kws::GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 146 | |
| 147 | /* Instantiate application context. */ |
| 148 | arm::app::ApplicationContext caseContext; |
| 149 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 150 | arm::app::Profiler profiler{"kws"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 151 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 152 | caseContext.Set<arm::app::Model&>("model", model); |
| 153 | caseContext.Set<uint32_t>("clipIndex", 0); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 154 | caseContext.Set<int>("frameLength", arm::app::kws::g_FrameLength); /* 640 sample length for MicroNet. */ |
| 155 | caseContext.Set<int>("frameStride", arm::app::kws::g_FrameStride); /* 320 sample stride for MicroNet. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 156 | caseContext.Set<float>("scoreThreshold", 0.7); /* Normalised score threshold. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 157 | arm::app::Classifier classifier; /* classifier wrapper object. */ |
| 158 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 159 | |
| 160 | std::vector <std::string> labels; |
| 161 | GetLabelsVector(labels); |
| 162 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 163 | REQUIRE(arm::app::ClassifyAudioHandler(caseContext, 0, true)); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | TEST_CASE("List all audio clips") |
| 168 | { |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 169 | /* Initialise the HAL and platform. */ |
| 170 | hal_platform_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 171 | |
| 172 | /* Model wrapper object. */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 173 | arm::app::MicroNetKwsModel model; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 174 | |
| 175 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 176 | REQUIRE(model.Init(arm::app::tensorArena, |
| 177 | sizeof(arm::app::tensorArena), |
| 178 | arm::app::kws::GetModelPointer(), |
| 179 | arm::app::kws::GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 180 | |
| 181 | /* Instantiate application context. */ |
| 182 | arm::app::ApplicationContext caseContext; |
| 183 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 184 | caseContext.Set<arm::app::Model&>("model", model); |
| 185 | |
| 186 | REQUIRE(arm::app::ListFilesHandler(caseContext)); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 187 | } |