Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022 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 "hal.h" |
| 18 | #include "ImageUtils.hpp" |
| 19 | #include "YoloFastestModel.hpp" |
| 20 | #include "TensorFlowLiteMicro.hpp" |
| 21 | #include "DetectorPostProcessing.hpp" |
| 22 | #include "InputFiles.hpp" |
| 23 | #include "UseCaseCommonUtils.hpp" |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 24 | |
| 25 | #include <catch.hpp> |
| 26 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 27 | void GetExpectedResults(std::vector<std::vector<arm::app::object_detection::DetectionResult>> &expected_results) |
| 28 | { |
| 29 | /* Img1 |
| 30 | 0) (0.999246) -> Detection box: {x=89,y=17,w=41,h=56} |
| 31 | 1) (0.995367) -> Detection box: {x=27,y=81,w=48,h=53} |
| 32 | */ |
| 33 | expected_results.push_back({ |
| 34 | arm::app::object_detection::DetectionResult(0.99,89,17,41,56), |
| 35 | arm::app::object_detection::DetectionResult(0.99,27,81,48,53) |
| 36 | }); |
| 37 | /* Img2 |
| 38 | 0) (0.998107) -> Detection box: {x=87,y=35,w=53,h=64} |
| 39 | */ |
| 40 | expected_results.push_back({ |
| 41 | arm::app::object_detection::DetectionResult(0.99,87,35,53,64) |
| 42 | }); |
| 43 | /* Img3 |
| 44 | 0) (0.999244) -> Detection box: {x=105,y=73,w=58,h=66} |
| 45 | 1) (0.985984) -> Detection box: {x=34,y=40,w=70,h=95} |
| 46 | */ |
| 47 | expected_results.push_back({ |
| 48 | arm::app::object_detection::DetectionResult(0.99,105,73,58,66), |
| 49 | arm::app::object_detection::DetectionResult(0.98,34,40,70,95) |
| 50 | }); |
| 51 | /* Img4 |
| 52 | 0) (0.993294) -> Detection box: {x=22,y=43,w=39,h=53} |
| 53 | 1) (0.992021) -> Detection box: {x=63,y=60,w=38,h=45} |
| 54 | */ |
| 55 | expected_results.push_back({ |
| 56 | arm::app::object_detection::DetectionResult(0.99,22,43,39,53), |
| 57 | arm::app::object_detection::DetectionResult(0.99,63,60,38,45) |
| 58 | }); |
| 59 | } |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 60 | |
| 61 | bool RunInference(arm::app::Model& model, const uint8_t imageData[]) |
| 62 | { |
| 63 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 64 | REQUIRE(inputTensor); |
| 65 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 66 | const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ? |
| 67 | inputTensor->bytes : IMAGE_DATA_SIZE; |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 68 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 69 | image::RgbToGrayscale(imageData,inputTensor->data.uint8,copySz); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 70 | |
| 71 | if(model.IsDataSigned()){ |
| 72 | convertImgIoInt8(inputTensor->data.data, copySz); |
| 73 | } |
| 74 | |
| 75 | return model.RunInference(); |
| 76 | } |
| 77 | |
| 78 | template<typename T> |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 79 | void TestInferenceDetectionResults(int imageIdx, arm::app::Model& model, T tolerance) { |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 80 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 81 | std::vector<arm::app::object_detection::DetectionResult> results; |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 82 | auto image = get_img_array(imageIdx); |
| 83 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 84 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
| 85 | auto nCols = inputShape->data[arm::app::YoloFastestModel::ms_inputColsIdx]; |
| 86 | auto nRows = inputShape->data[arm::app::YoloFastestModel::ms_inputRowsIdx]; |
| 87 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 88 | REQUIRE(RunInference(model, image)); |
| 89 | |
| 90 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 91 | std::vector<TfLiteTensor*> output_arr{model.GetOutputTensor(0), model.GetOutputTensor(1)}; |
| 92 | for (size_t i =0; i < output_arr.size(); i++) { |
| 93 | REQUIRE(output_arr[i]); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 94 | REQUIRE(tflite::GetTensorData<T>(output_arr[i])); |
| 95 | } |
| 96 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 97 | arm::app::object_detection::DetectorPostprocessing postp; |
| 98 | postp.RunPostProcessing( |
| 99 | nullptr, |
| 100 | nRows, |
| 101 | nCols, |
| 102 | output_arr[0], |
| 103 | output_arr[1], |
| 104 | results); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 105 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 106 | std::vector<std::vector<arm::app::object_detection::DetectionResult>> expected_results; |
| 107 | GetExpectedResults(expected_results); |
| 108 | |
| 109 | /* Validate got the same number of boxes */ |
| 110 | REQUIRE(results.size() == expected_results[imageIdx].size()); |
| 111 | |
| 112 | |
| 113 | for (int i=0; i < (int)results.size(); i++) { |
| 114 | /* Validate confidence and box dimensions */ |
| 115 | REQUIRE(std::abs(results[i].m_normalisedVal - expected_results[imageIdx][i].m_normalisedVal) < 0.1); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 116 | REQUIRE(static_cast<int>(results[i].m_x0) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_x0)).epsilon(tolerance)); |
| 117 | REQUIRE(static_cast<int>(results[i].m_y0) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_y0)).epsilon(tolerance)); |
| 118 | REQUIRE(static_cast<int>(results[i].m_w) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_w)).epsilon(tolerance)); |
| 119 | REQUIRE(static_cast<int>(results[i].m_h) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_h)).epsilon(tolerance)); |
| 120 | } |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
| 124 | TEST_CASE("Running inference with TensorFlow Lite Micro and YoloFastest", "[YoloFastest]") |
| 125 | { |
| 126 | SECTION("Executing inferences sequentially") |
| 127 | { |
| 128 | arm::app::YoloFastestModel model{}; |
| 129 | |
| 130 | REQUIRE_FALSE(model.IsInited()); |
| 131 | REQUIRE(model.Init()); |
| 132 | REQUIRE(model.IsInited()); |
| 133 | |
| 134 | for (uint32_t i = 0 ; i < NUMBER_OF_FILES; ++i) { |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 135 | TestInferenceDetectionResults<uint8_t>(i, model, 1); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | for (uint32_t i = 0 ; i < NUMBER_OF_FILES; ++i) { |
| 140 | DYNAMIC_SECTION("Executing inference with re-init") |
| 141 | { |
| 142 | arm::app::YoloFastestModel model{}; |
| 143 | |
| 144 | REQUIRE_FALSE(model.IsInited()); |
| 145 | REQUIRE(model.Init()); |
| 146 | REQUIRE(model.IsInited()); |
| 147 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 148 | TestInferenceDetectionResults<uint8_t>(i, model, 1); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |