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