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 | { |
| 46 | hal_platform platform; |
| 47 | data_acq_module data_acq; |
| 48 | data_psn_module data_psn; |
| 49 | platform_timer timer; |
| 50 | |
| 51 | /* Initialise the HAL and platform. */ |
| 52 | hal_init(&platform, &data_acq, &data_psn, &timer); |
| 53 | hal_platform_init(&platform); |
| 54 | |
| 55 | /* Model wrapper object. */ |
| 56 | arm::app::MobileNetModel model; |
| 57 | |
| 58 | /* Load the model. */ |
| 59 | REQUIRE(model.Init()); |
| 60 | |
| 61 | /* Instantiate application context. */ |
| 62 | arm::app::ApplicationContext caseContext; |
| 63 | |
| 64 | caseContext.Set<hal_platform&>("platform", platform); |
| 65 | caseContext.Set<arm::app::Model&>("model", model); |
| 66 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 67 | arm::app::Classifier classifier; /* Classifier wrapper object. */ |
| 68 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 69 | |
| 70 | std::vector <std::string> labels; |
| 71 | GetLabelsVector(labels); |
| 72 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 73 | |
| 74 | REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, false)); |
| 75 | |
| 76 | auto results = caseContext.Get<std::vector<arm::app::ClassificationResult>>("results"); |
| 77 | |
| 78 | REQUIRE(results[0].m_labelIdx == 282); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | TEST_CASE("Inference run all images", "[.]") |
| 83 | { |
| 84 | hal_platform platform; |
| 85 | data_acq_module data_acq; |
| 86 | data_psn_module data_psn; |
| 87 | platform_timer timer; |
| 88 | |
| 89 | /* Initialise the HAL and platform. */ |
| 90 | hal_init(&platform, &data_acq, &data_psn, &timer); |
| 91 | hal_platform_init(&platform); |
| 92 | |
| 93 | /* Model wrapper object. */ |
| 94 | arm::app::MobileNetModel model; |
| 95 | |
| 96 | /* Load the model. */ |
| 97 | REQUIRE(model.Init()); |
| 98 | |
| 99 | /* Instantiate application context. */ |
| 100 | arm::app::ApplicationContext caseContext; |
| 101 | |
| 102 | caseContext.Set<hal_platform&>("platform", platform); |
| 103 | caseContext.Set<arm::app::Model&>("model", model); |
| 104 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 105 | arm::app::Classifier classifier; /* classifier wrapper object. */ |
| 106 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 107 | |
| 108 | std::vector <std::string> labels; |
| 109 | GetLabelsVector(labels); |
| 110 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 111 | |
| 112 | REQUIRE(arm::app::ClassifyImageHandler(caseContext, 0, true)); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | TEST_CASE("List all images") |
| 117 | { |
| 118 | hal_platform platform; |
| 119 | data_acq_module data_acq; |
| 120 | data_psn_module data_psn; |
| 121 | platform_timer timer; |
| 122 | |
| 123 | /* Initialise the HAL and platform. */ |
| 124 | hal_init(&platform, &data_acq, &data_psn, &timer); |
| 125 | hal_platform_init(&platform); |
| 126 | |
| 127 | /* Model wrapper object. */ |
| 128 | arm::app::MobileNetModel model; |
| 129 | |
| 130 | /* Load the model. */ |
| 131 | REQUIRE(model.Init()); |
| 132 | |
| 133 | /* Instantiate application context. */ |
| 134 | arm::app::ApplicationContext caseContext; |
| 135 | |
| 136 | caseContext.Set<hal_platform&>("platform", platform); |
| 137 | caseContext.Set<arm::app::Model&>("model", model); |
| 138 | |
| 139 | REQUIRE(arm::app::ListFilesHandler(caseContext)); |
| 140 | } |