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