From 06fcf7502b5c9b0e30ac06acfa25aa914d936500 Mon Sep 17 00:00:00 2001 From: Michael Levit Date: Wed, 12 Jan 2022 11:53:46 +0200 Subject: Face detection demo from Emza Visual Sense Signed-off-by: Michael Levit michaell@emza-vs.com Change-Id: I7958b05b5dbe9a785e0f8a241b716c17a9ca976f --- .../ExpectedObjectDetectionResults.cpp | 66 ++++++++++ .../object_detection/InferenceTestYoloFastest.cc | 124 +++++++++++++++++++ .../object_detection/ObjectDetectionTests.cc | 18 +++ .../object_detection/ObjectDetectionUCTest.cc | 135 +++++++++++++++++++++ .../include/ExpectedObjectDetectionResults.hpp | 26 ++++ 5 files changed, 369 insertions(+) create mode 100644 tests/use_case/object_detection/ExpectedObjectDetectionResults.cpp create mode 100644 tests/use_case/object_detection/InferenceTestYoloFastest.cc create mode 100644 tests/use_case/object_detection/ObjectDetectionTests.cc create mode 100644 tests/use_case/object_detection/ObjectDetectionUCTest.cc create mode 100644 tests/use_case/object_detection/include/ExpectedObjectDetectionResults.hpp (limited to 'tests') diff --git a/tests/use_case/object_detection/ExpectedObjectDetectionResults.cpp b/tests/use_case/object_detection/ExpectedObjectDetectionResults.cpp new file mode 100644 index 0000000..2c69057 --- /dev/null +++ b/tests/use_case/object_detection/ExpectedObjectDetectionResults.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ExpectedObjectDetectionResults.hpp" + + +/* +//Reference results +Got 2 boxes +0) (0.999246) -> Detection box: {x=89,y=17,w=41,h=56} +1) (0.995367) -> Detection box: {x=27,y=81,w=48,h=53} +Entering TestInference +Got 1 boxes +0) (0.998107) -> Detection box: {x=87,y=35,w=53,h=64} +Entering TestInference +Got 2 boxes +0) (0.999244) -> Detection box: {x=105,y=73,w=58,h=66} +1) (0.985984) -> Detection box: {x=34,y=40,w=70,h=95} +Entering TestInference +Got 2 boxes +0) (0.993294) -> Detection box: {x=22,y=43,w=39,h=53} +1) (0.992021) -> Detection box: {x=63,y=60,w=38,h=45} + +*/ + +void get_expected_ut_results(std::vector> &expected_results) +{ + + expected_results.resize(4); + + std::vector img_1(2); + std::vector img_2(1); + std::vector img_3(2); + std::vector img_4(2); + + img_1[0] = arm::app::DetectionResult(0.99,89,17,41,56); + img_1[1] = arm::app::DetectionResult(0.99,27,81,48,53); + + img_2[0] = arm::app::DetectionResult(0.99,87,35,53,64); + + img_3[0] = arm::app::DetectionResult(0.99,105,73,58,66); + img_3[1] = arm::app::DetectionResult(0.98,34,40,70,95); + + img_4[0] = arm::app::DetectionResult(0.99,22,43,39,53); + img_4[1] = arm::app::DetectionResult(0.99,63,60,38,45); + + expected_results[0] = img_1; + expected_results[1] = img_2; + expected_results[2] = img_3; + expected_results[3] = img_4; + + +} diff --git a/tests/use_case/object_detection/InferenceTestYoloFastest.cc b/tests/use_case/object_detection/InferenceTestYoloFastest.cc new file mode 100644 index 0000000..e6ae573 --- /dev/null +++ b/tests/use_case/object_detection/InferenceTestYoloFastest.cc @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2022 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "hal.h" +#include "ImageUtils.hpp" +#include "YoloFastestModel.hpp" +#include "TensorFlowLiteMicro.hpp" +#include "DetectorPostProcessing.hpp" +#include "InputFiles.hpp" +#include "UseCaseCommonUtils.hpp" +#include "DetectionUseCaseUtils.hpp" +#include "ExpectedObjectDetectionResults.hpp" + +#include + + +bool RunInference(arm::app::Model& model, const uint8_t imageData[]) +{ + TfLiteTensor* inputTensor = model.GetInputTensor(0); + REQUIRE(inputTensor); + + const size_t copySz = inputTensor->bytes < (INPUT_IMAGE_WIDTH*INPUT_IMAGE_HEIGHT) ? + inputTensor->bytes : + (INPUT_IMAGE_WIDTH*INPUT_IMAGE_HEIGHT); + + arm::app::RgbToGrayscale(imageData,inputTensor->data.uint8,INPUT_IMAGE_WIDTH,INPUT_IMAGE_HEIGHT); + + if(model.IsDataSigned()){ + convertImgIoInt8(inputTensor->data.data, copySz); + } + + return model.RunInference(); +} + +template +void TestInference(int imageIdx, arm::app::Model& model, T tolerance) { + + info("Entering TestInference for image %d \n", imageIdx); + + std::vector results; + auto image = get_img_array(imageIdx); + + REQUIRE(RunInference(model, image)); + + + TfLiteTensor* output_arr[2] = {nullptr,nullptr}; + output_arr[0] = model.GetOutputTensor(0); + output_arr[1] = model.GetOutputTensor(1); + + for (int i =0; i < 2; i++) { + REQUIRE(output_arr[i]); + REQUIRE(tflite::GetTensorData(output_arr[i])); + } + + RunPostProcessing(NULL,output_arr,results); + + info("Got %ld boxes \n",results.size()); + + std::vector> expected_results; + get_expected_ut_results(expected_results); + + /*validate got the same number of boxes */ + REQUIRE(results.size() == expected_results[imageIdx].size()); + + + for (int i=0; i < (int)results.size(); i++) { + + info("%" PRIu32 ") (%f) -> %s {x=%d,y=%d,w=%d,h=%d}\n", (int)i, + results[i].m_normalisedVal, "Detection box:", + results[i].m_x0, results[i].m_y0, results[i].m_w, results[i].m_h ); + + /*validate confidence and box dimensions */ + REQUIRE(fabs(results[i].m_normalisedVal - expected_results[imageIdx][i].m_normalisedVal) < 0.1); + REQUIRE(static_cast(results[i].m_x0) == Approx(static_cast((T)expected_results[imageIdx][i].m_x0)).epsilon(tolerance)); + REQUIRE(static_cast(results[i].m_y0) == Approx(static_cast((T)expected_results[imageIdx][i].m_y0)).epsilon(tolerance)); + REQUIRE(static_cast(results[i].m_w) == Approx(static_cast((T)expected_results[imageIdx][i].m_w)).epsilon(tolerance)); + REQUIRE(static_cast(results[i].m_h) == Approx(static_cast((T)expected_results[imageIdx][i].m_h)).epsilon(tolerance)); + } + + +} + + +TEST_CASE("Running inference with TensorFlow Lite Micro and YoloFastest", "[YoloFastest]") +{ + SECTION("Executing inferences sequentially") + { + arm::app::YoloFastestModel model{}; + + REQUIRE_FALSE(model.IsInited()); + REQUIRE(model.Init()); + REQUIRE(model.IsInited()); + + for (uint32_t i = 0 ; i < NUMBER_OF_FILES; ++i) { + TestInference(i, model, 1); + } + } + + for (uint32_t i = 0 ; i < NUMBER_OF_FILES; ++i) { + DYNAMIC_SECTION("Executing inference with re-init") + { + arm::app::YoloFastestModel model{}; + + REQUIRE_FALSE(model.IsInited()); + REQUIRE(model.Init()); + REQUIRE(model.IsInited()); + + TestInference(i, model, 1); + } + } +} diff --git a/tests/use_case/object_detection/ObjectDetectionTests.cc b/tests/use_case/object_detection/ObjectDetectionTests.cc new file mode 100644 index 0000000..dd2d707 --- /dev/null +++ b/tests/use_case/object_detection/ObjectDetectionTests.cc @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2022 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define CATCH_CONFIG_MAIN +#include diff --git a/tests/use_case/object_detection/ObjectDetectionUCTest.cc b/tests/use_case/object_detection/ObjectDetectionUCTest.cc new file mode 100644 index 0000000..0a0486e --- /dev/null +++ b/tests/use_case/object_detection/ObjectDetectionUCTest.cc @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2022 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "DetectionResult.hpp" +//include "Detector.hpp" +#include "hal.h" +#include "YoloFastestModel.hpp" +#include "UseCaseHandler.hpp" +#include "UseCaseCommonUtils.hpp" + +#include + +TEST_CASE("Model info") +{ +printf("Entering Model info \n"); + /* Model wrapper object. */ + arm::app::YoloFastestModel model; + + /* Load the model. */ + REQUIRE(model.Init()); + + /* Instantiate application context. */ + arm::app::ApplicationContext caseContext; + + caseContext.Set("model", model); + + REQUIRE(model.ShowModelInfoHandler()); +} + + +TEST_CASE("Inference by index") +{ +printf("Entering Inference by index \n"); + hal_platform platform; + data_acq_module data_acq; + data_psn_module data_psn; + platform_timer timer; + + /* Initialise the HAL and platform. */ + hal_init(&platform, &data_acq, &data_psn, &timer); + hal_platform_init(&platform); + + /* Model wrapper object. */ + arm::app::YoloFastestModel model; + + /* Load the model. */ + REQUIRE(model.Init()); + + /* Instantiate application context. */ + arm::app::ApplicationContext caseContext; + + arm::app::Profiler profiler{&platform, "object_detection"}; + caseContext.Set("profiler", profiler); + caseContext.Set("platform", platform); + caseContext.Set("model", model); + caseContext.Set("imgIndex", 0); + + REQUIRE(arm::app::ObjectDetectionHandler(caseContext, 0, false)); + + auto results = caseContext.Get>("results"); + +} + + +TEST_CASE("Inference run all images") +{ + printf("Entering Inference run all images \n"); + hal_platform platform; + data_acq_module data_acq; + data_psn_module data_psn; + platform_timer timer; + + /* Initialise the HAL and platform. */ + hal_init(&platform, &data_acq, &data_psn, &timer); + hal_platform_init(&platform); + + /* Model wrapper object. */ + arm::app::YoloFastestModel model; + + /* Load the model. */ + REQUIRE(model.Init()); + + /* Instantiate application context. */ + arm::app::ApplicationContext caseContext; + + arm::app::Profiler profiler{&platform, "object_detection"}; + caseContext.Set("profiler", profiler); + caseContext.Set("platform", platform); + caseContext.Set("model", model); + caseContext.Set("imgIndex", 0); + + REQUIRE(arm::app::ObjectDetectionHandler(caseContext, 0, true)); + +} + + +TEST_CASE("List all images") +{ +printf("Entering List all images \n"); + hal_platform platform; + data_acq_module data_acq; + data_psn_module data_psn; + platform_timer timer; + + /* Initialise the HAL and platform. */ + hal_init(&platform, &data_acq, &data_psn, &timer); + hal_platform_init(&platform); + + /* Model wrapper object. */ + arm::app::YoloFastestModel model; + + /* Load the model. */ + REQUIRE(model.Init()); + + /* Instantiate application context. */ + arm::app::ApplicationContext caseContext; + + caseContext.Set("platform", platform); + caseContext.Set("model", model); + + REQUIRE(arm::app::ListFilesHandler(caseContext)); +} diff --git a/tests/use_case/object_detection/include/ExpectedObjectDetectionResults.hpp b/tests/use_case/object_detection/include/ExpectedObjectDetectionResults.hpp new file mode 100644 index 0000000..09edc00 --- /dev/null +++ b/tests/use_case/object_detection/include/ExpectedObjectDetectionResults.hpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef EXPECTED_OBJECT_DETECTION_RESULTS_H +#define EXPECTED_OBJECT_DETECTION_RESULTS_H + +#include "DetectionResult.hpp" +#include + +void get_expected_ut_results(std::vector> &expected_results); + + +#endif /* EXPECTED_OBJECT_DETECTION_RESULTS_H */ -- cgit v1.2.1