Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [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> |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [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> |
| 18 | #include "VisualWakeWordModel.hpp" |
| 19 | #include "hal.h" |
| 20 | |
| 21 | #include "ClassificationResult.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" |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 27 | |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 28 | namespace arm { |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 29 | namespace app { |
| 30 | static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE; |
| 31 | namespace vww { |
| 32 | extern uint8_t* GetModelPointer(); |
| 33 | extern size_t GetModelLen(); |
| 34 | } /* namespace vww */ |
| 35 | } /* namespace app */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 36 | } /* namespace arm */ |
| 37 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 38 | TEST_CASE("Model info") |
| 39 | { |
| 40 | arm::app::VisualWakeWordModel model; /* model wrapper object */ |
| 41 | |
| 42 | /* Load the model */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 43 | REQUIRE(model.Init(arm::app::tensorArena, |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 44 | sizeof(arm::app::tensorArena), |
| 45 | arm::app::vww::GetModelPointer(), |
| 46 | arm::app::vww::GetModelLen())); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [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 | TEST_CASE("Inference by index") |
| 57 | { |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 58 | hal_platform_init(); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 59 | |
| 60 | arm::app::VisualWakeWordModel model; /* model wrapper object */ |
| 61 | |
| 62 | /* Load the model */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 63 | REQUIRE(model.Init(arm::app::tensorArena, |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 64 | sizeof(arm::app::tensorArena), |
| 65 | arm::app::vww::GetModelPointer(), |
| 66 | arm::app::vww::GetModelLen())); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 67 | |
| 68 | /* Instantiate application context */ |
| 69 | arm::app::ApplicationContext caseContext; |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 70 | arm::app::Profiler profiler{"pd"}; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 71 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 72 | caseContext.Set<arm::app::Model&>("model", model); |
| 73 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 74 | arm::app::Classifier classifier; /* classifier wrapper object */ |
| 75 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 76 | |
| 77 | std::vector <std::string> labels; |
| 78 | GetLabelsVector(labels); |
| 79 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 80 | |
| 81 | REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, false)); |
| 82 | |
| 83 | auto results = caseContext.Get<std::vector<arm::app::ClassificationResult>>("results"); |
| 84 | |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 85 | REQUIRE(results[0].m_labelIdx == 1); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST_CASE("Inference run all images") |
| 89 | { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 90 | /* Initialise the HAL and platform */ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 91 | hal_platform_init(); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 92 | |
| 93 | arm::app::VisualWakeWordModel model; /* model wrapper object */ |
| 94 | |
| 95 | /* Load the model */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 96 | REQUIRE(model.Init(arm::app::tensorArena, |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 97 | sizeof(arm::app::tensorArena), |
| 98 | arm::app::vww::GetModelPointer(), |
| 99 | arm::app::vww::GetModelLen())); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 100 | |
| 101 | /* Instantiate application context */ |
| 102 | arm::app::ApplicationContext caseContext; |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 103 | arm::app::Profiler profiler{"pd"}; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 104 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 105 | caseContext.Set<arm::app::Model&>("model", model); |
| 106 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 107 | arm::app::Classifier classifier; /* classifier wrapper object */ |
| 108 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 109 | |
| 110 | std::vector <std::string> labels; |
| 111 | GetLabelsVector(labels); |
| 112 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 113 | |
| 114 | REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, true)); |
| 115 | } |
| 116 | |
| 117 | TEST_CASE("List all images") |
| 118 | { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 119 | /* Initialise the HAL and platform */ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 120 | hal_platform_init(); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 121 | |
| 122 | arm::app::VisualWakeWordModel model; /* model wrapper object */ |
| 123 | |
| 124 | /* Load the model */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 125 | REQUIRE(model.Init(arm::app::tensorArena, |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 126 | sizeof(arm::app::tensorArena), |
| 127 | arm::app::vww::GetModelPointer(), |
| 128 | arm::app::vww::GetModelLen())); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 129 | |
| 130 | /* Instantiate application context */ |
| 131 | arm::app::ApplicationContext caseContext; |
| 132 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 133 | caseContext.Set<arm::app::Model&>("model", model); |
| 134 | |
| 135 | REQUIRE(arm::app::ListFilesHandler(caseContext)); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 136 | } |