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 "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" |
| 24 | |
| 25 | #include <catch.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 app */ |
| 31 | } /* namespace arm */ |
| 32 | |
| 33 | extern uint8_t* GetModelPointer(); |
| 34 | extern size_t GetModelLen(); |
| 35 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 36 | TEST_CASE("Model info") |
| 37 | { |
| 38 | /* Model wrapper object. */ |
| 39 | arm::app::MobileNetModel model; |
| 40 | |
| 41 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame^] | 42 | REQUIRE(model.Init(arm::app::tensorArena, |
| 43 | sizeof(arm::app::tensorArena), |
| 44 | GetModelPointer(), |
| 45 | GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 46 | |
| 47 | /* Instantiate application context. */ |
| 48 | arm::app::ApplicationContext caseContext; |
| 49 | |
| 50 | caseContext.Set<arm::app::Model&>("model", model); |
| 51 | |
| 52 | REQUIRE(model.ShowModelInfoHandler()); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | TEST_CASE("Inference by index", "[.]") |
| 57 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 58 | /* Initialise the HAL and platform. */ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 59 | hal_platform_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 60 | |
| 61 | /* Model wrapper object. */ |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 62 | arm::app::MobileNetModel model; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 63 | |
| 64 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame^] | 65 | REQUIRE(model.Init(arm::app::tensorArena, |
| 66 | sizeof(arm::app::tensorArena), |
| 67 | GetModelPointer(), |
| 68 | GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | |
| 70 | /* Instantiate application context. */ |
| 71 | arm::app::ApplicationContext caseContext; |
| 72 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 73 | arm::app::Profiler profiler{"img_class"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 74 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 75 | caseContext.Set<arm::app::Model&>("model", model); |
| 76 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 77 | arm::app::Classifier classifier; /* Classifier wrapper object. */ |
| 78 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 79 | |
| 80 | std::vector <std::string> labels; |
| 81 | GetLabelsVector(labels); |
| 82 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 83 | |
| 84 | REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, false)); |
| 85 | |
| 86 | auto results = caseContext.Get<std::vector<arm::app::ClassificationResult>>("results"); |
| 87 | |
| 88 | REQUIRE(results[0].m_labelIdx == 282); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | TEST_CASE("Inference run all images", "[.]") |
| 93 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 94 | /* Initialise the HAL and platform. */ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 95 | hal_platform_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | |
| 97 | /* Model wrapper object. */ |
| 98 | arm::app::MobileNetModel model; |
| 99 | |
| 100 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame^] | 101 | REQUIRE(model.Init(arm::app::tensorArena, |
| 102 | sizeof(arm::app::tensorArena), |
| 103 | GetModelPointer(), |
| 104 | GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 105 | |
| 106 | /* Instantiate application context. */ |
| 107 | arm::app::ApplicationContext caseContext; |
| 108 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 109 | arm::app::Profiler profiler{"img_class"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 110 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 111 | caseContext.Set<arm::app::Model&>("model", model); |
| 112 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 113 | arm::app::Classifier classifier; /* classifier wrapper object. */ |
| 114 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 115 | |
| 116 | std::vector <std::string> labels; |
| 117 | GetLabelsVector(labels); |
| 118 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 119 | |
| 120 | REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, true)); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | TEST_CASE("List all images") |
| 125 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 126 | /* Initialise the HAL and platform. */ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 127 | hal_platform_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 128 | |
| 129 | /* Model wrapper object. */ |
| 130 | arm::app::MobileNetModel model; |
| 131 | |
| 132 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame^] | 133 | REQUIRE(model.Init(arm::app::tensorArena, |
| 134 | sizeof(arm::app::tensorArena), |
| 135 | GetModelPointer(), |
| 136 | GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 137 | |
| 138 | /* Instantiate application context. */ |
| 139 | arm::app::ApplicationContext caseContext; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | caseContext.Set<arm::app::Model&>("model", model); |
| 141 | |
| 142 | REQUIRE(arm::app::ListFilesHandler(caseContext)); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame^] | 143 | } |