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 | |
| 27 | TEST_CASE("Model info") |
| 28 | { |
| 29 | /* Model wrapper object. */ |
| 30 | arm::app::MobileNetModel model; |
| 31 | |
| 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 | |
| 44 | TEST_CASE("Inference by index", "[.]") |
| 45 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 46 | /* Initialise the HAL and platform. */ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 47 | hal_platform_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 48 | |
| 49 | /* Model wrapper object. */ |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 50 | arm::app::MobileNetModel model; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | |
| 52 | /* Load the model. */ |
| 53 | REQUIRE(model.Init()); |
| 54 | |
| 55 | /* Instantiate application context. */ |
| 56 | arm::app::ApplicationContext caseContext; |
| 57 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 58 | arm::app::Profiler profiler{"img_class"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 59 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 60 | caseContext.Set<arm::app::Model&>("model", model); |
| 61 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 62 | arm::app::Classifier classifier; /* Classifier wrapper object. */ |
| 63 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 64 | |
| 65 | std::vector <std::string> labels; |
| 66 | GetLabelsVector(labels); |
| 67 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 68 | |
| 69 | REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, false)); |
| 70 | |
| 71 | auto results = caseContext.Get<std::vector<arm::app::ClassificationResult>>("results"); |
| 72 | |
| 73 | REQUIRE(results[0].m_labelIdx == 282); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | TEST_CASE("Inference run all images", "[.]") |
| 78 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 79 | /* Initialise the HAL and platform. */ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 80 | hal_platform_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 81 | |
| 82 | /* Model wrapper object. */ |
| 83 | arm::app::MobileNetModel model; |
| 84 | |
| 85 | /* Load the model. */ |
| 86 | REQUIRE(model.Init()); |
| 87 | |
| 88 | /* Instantiate application context. */ |
| 89 | arm::app::ApplicationContext caseContext; |
| 90 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 91 | arm::app::Profiler profiler{"img_class"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 92 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 93 | caseContext.Set<arm::app::Model&>("model", model); |
| 94 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 95 | arm::app::Classifier classifier; /* classifier wrapper object. */ |
| 96 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 97 | |
| 98 | std::vector <std::string> labels; |
| 99 | GetLabelsVector(labels); |
| 100 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 101 | |
| 102 | REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, true)); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | TEST_CASE("List all images") |
| 107 | { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 108 | /* Initialise the HAL and platform. */ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 109 | hal_platform_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 110 | |
| 111 | /* Model wrapper object. */ |
| 112 | arm::app::MobileNetModel model; |
| 113 | |
| 114 | /* Load the model. */ |
| 115 | REQUIRE(model.Init()); |
| 116 | |
| 117 | /* Instantiate application context. */ |
| 118 | arm::app::ApplicationContext caseContext; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 119 | caseContext.Set<arm::app::Model&>("model", model); |
| 120 | |
| 121 | REQUIRE(arm::app::ListFilesHandler(caseContext)); |
| 122 | } |