blob: eb92904135b10dcdbfd9238d13f84b30df71d91b [file] [log] [blame]
Michael Levit06fcf752022-01-12 11:53:46 +02001/*
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 */
alexander31ae9f02022-02-10 16:15:54 +000017#include "log_macros.h"
Michael Levit06fcf752022-01-12 11:53:46 +020018#include "ImageUtils.hpp"
19#include "YoloFastestModel.hpp"
20#include "TensorFlowLiteMicro.hpp"
21#include "DetectorPostProcessing.hpp"
22#include "InputFiles.hpp"
23#include "UseCaseCommonUtils.hpp"
Michael Levit06fcf752022-01-12 11:53:46 +020024
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010025namespace arm {
26 namespace app {
27 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
Liam Barry213a5432022-05-09 17:06:19 +010028 namespace object_detection {
29 extern uint8_t* GetModelPointer();
30 extern size_t GetModelLen();
31 } /* namespace object_detection */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010032 } /* namespace app */
33} /* namespace arm */
34
Michael Levit06fcf752022-01-12 11:53:46 +020035#include <catch.hpp>
36
Isabella Gottardi3107aa22022-01-27 16:39:37 +000037void GetExpectedResults(std::vector<std::vector<arm::app::object_detection::DetectionResult>> &expected_results)
38{
39 /* Img1
40 0) (0.999246) -> Detection box: {x=89,y=17,w=41,h=56}
41 1) (0.995367) -> Detection box: {x=27,y=81,w=48,h=53}
42 */
43 expected_results.push_back({
44 arm::app::object_detection::DetectionResult(0.99,89,17,41,56),
45 arm::app::object_detection::DetectionResult(0.99,27,81,48,53)
46 });
47 /* Img2
48 0) (0.998107) -> Detection box: {x=87,y=35,w=53,h=64}
49 */
50 expected_results.push_back({
51 arm::app::object_detection::DetectionResult(0.99,87,35,53,64)
52 });
53 /* Img3
54 0) (0.999244) -> Detection box: {x=105,y=73,w=58,h=66}
55 1) (0.985984) -> Detection box: {x=34,y=40,w=70,h=95}
56 */
57 expected_results.push_back({
58 arm::app::object_detection::DetectionResult(0.99,105,73,58,66),
59 arm::app::object_detection::DetectionResult(0.98,34,40,70,95)
60 });
61 /* Img4
62 0) (0.993294) -> Detection box: {x=22,y=43,w=39,h=53}
63 1) (0.992021) -> Detection box: {x=63,y=60,w=38,h=45}
64 */
65 expected_results.push_back({
66 arm::app::object_detection::DetectionResult(0.99,22,43,39,53),
67 arm::app::object_detection::DetectionResult(0.99,63,60,38,45)
68 });
69}
Michael Levit06fcf752022-01-12 11:53:46 +020070
71bool RunInference(arm::app::Model& model, const uint8_t imageData[])
72{
73 TfLiteTensor* inputTensor = model.GetInputTensor(0);
74 REQUIRE(inputTensor);
75
Isabella Gottardi3107aa22022-01-27 16:39:37 +000076 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
77 inputTensor->bytes : IMAGE_DATA_SIZE;
Michael Levit06fcf752022-01-12 11:53:46 +020078
Richard Burtoned35a6f2022-02-14 11:55:35 +000079 arm::app::image::RgbToGrayscale(imageData,inputTensor->data.uint8,copySz);
Michael Levit06fcf752022-01-12 11:53:46 +020080
81 if(model.IsDataSigned()){
Richard Burtoned35a6f2022-02-14 11:55:35 +000082 arm::app::image::ConvertImgToInt8(inputTensor->data.data, copySz);
Michael Levit06fcf752022-01-12 11:53:46 +020083 }
84
85 return model.RunInference();
86}
87
88template<typename T>
Isabella Gottardi3107aa22022-01-27 16:39:37 +000089void TestInferenceDetectionResults(int imageIdx, arm::app::Model& model, T tolerance) {
Michael Levit06fcf752022-01-12 11:53:46 +020090
Isabella Gottardi3107aa22022-01-27 16:39:37 +000091 std::vector<arm::app::object_detection::DetectionResult> results;
Michael Levit06fcf752022-01-12 11:53:46 +020092 auto image = get_img_array(imageIdx);
93
Isabella Gottardi3107aa22022-01-27 16:39:37 +000094 TfLiteIntArray* inputShape = model.GetInputShape(0);
95 auto nCols = inputShape->data[arm::app::YoloFastestModel::ms_inputColsIdx];
96 auto nRows = inputShape->data[arm::app::YoloFastestModel::ms_inputRowsIdx];
97
Michael Levit06fcf752022-01-12 11:53:46 +020098 REQUIRE(RunInference(model, image));
99
100
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000101 std::vector<TfLiteTensor*> output_arr{model.GetOutputTensor(0), model.GetOutputTensor(1)};
102 for (size_t i =0; i < output_arr.size(); i++) {
103 REQUIRE(output_arr[i]);
Michael Levit06fcf752022-01-12 11:53:46 +0200104 REQUIRE(tflite::GetTensorData<T>(output_arr[i]));
105 }
106
Richard Burtonef904972022-04-27 17:24:36 +0100107 arm::app::DetectorPostProcess postp{output_arr[0], output_arr[1], results, nRows, nCols};
108 postp.DoPostProcess();
Michael Levit06fcf752022-01-12 11:53:46 +0200109
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000110 std::vector<std::vector<arm::app::object_detection::DetectionResult>> expected_results;
111 GetExpectedResults(expected_results);
112
113 /* Validate got the same number of boxes */
114 REQUIRE(results.size() == expected_results[imageIdx].size());
115
116
117 for (int i=0; i < (int)results.size(); i++) {
118 /* Validate confidence and box dimensions */
119 REQUIRE(std::abs(results[i].m_normalisedVal - expected_results[imageIdx][i].m_normalisedVal) < 0.1);
Michael Levit06fcf752022-01-12 11:53:46 +0200120 REQUIRE(static_cast<int>(results[i].m_x0) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_x0)).epsilon(tolerance));
121 REQUIRE(static_cast<int>(results[i].m_y0) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_y0)).epsilon(tolerance));
122 REQUIRE(static_cast<int>(results[i].m_w) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_w)).epsilon(tolerance));
123 REQUIRE(static_cast<int>(results[i].m_h) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_h)).epsilon(tolerance));
124 }
Michael Levit06fcf752022-01-12 11:53:46 +0200125}
126
127
128TEST_CASE("Running inference with TensorFlow Lite Micro and YoloFastest", "[YoloFastest]")
129{
130 SECTION("Executing inferences sequentially")
131 {
132 arm::app::YoloFastestModel model{};
133
134 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100135 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100136 sizeof(arm::app::tensorArena),
137 arm::app::object_detection::GetModelPointer(),
138 arm::app::object_detection::GetModelLen()));
Michael Levit06fcf752022-01-12 11:53:46 +0200139 REQUIRE(model.IsInited());
140
141 for (uint32_t i = 0 ; i < NUMBER_OF_FILES; ++i) {
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000142 TestInferenceDetectionResults<uint8_t>(i, model, 1);
Michael Levit06fcf752022-01-12 11:53:46 +0200143 }
144 }
145
146 for (uint32_t i = 0 ; i < NUMBER_OF_FILES; ++i) {
147 DYNAMIC_SECTION("Executing inference with re-init")
148 {
149 arm::app::YoloFastestModel model{};
150
151 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +0100152 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +0100153 sizeof(arm::app::tensorArena),
154 arm::app::object_detection::GetModelPointer(),
155 arm::app::object_detection::GetModelLen()));
Michael Levit06fcf752022-01-12 11:53:46 +0200156 REQUIRE(model.IsInited());
157
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000158 TestInferenceDetectionResults<uint8_t>(i, model, 1);
Michael Levit06fcf752022-01-12 11:53:46 +0200159 }
160 }
161}