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