blob: e6ae5736ea50c131525bbd19bb87a55473e868d3 [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 */
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"
24#include "DetectionUseCaseUtils.hpp"
25#include "ExpectedObjectDetectionResults.hpp"
26
27#include <catch.hpp>
28
29
30bool RunInference(arm::app::Model& model, const uint8_t imageData[])
31{
32 TfLiteTensor* inputTensor = model.GetInputTensor(0);
33 REQUIRE(inputTensor);
34
35 const size_t copySz = inputTensor->bytes < (INPUT_IMAGE_WIDTH*INPUT_IMAGE_HEIGHT) ?
36 inputTensor->bytes :
37 (INPUT_IMAGE_WIDTH*INPUT_IMAGE_HEIGHT);
38
39 arm::app::RgbToGrayscale(imageData,inputTensor->data.uint8,INPUT_IMAGE_WIDTH,INPUT_IMAGE_HEIGHT);
40
41 if(model.IsDataSigned()){
42 convertImgIoInt8(inputTensor->data.data, copySz);
43 }
44
45 return model.RunInference();
46}
47
48template<typename T>
49void TestInference(int imageIdx, arm::app::Model& model, T tolerance) {
50
51 info("Entering TestInference for image %d \n", imageIdx);
52
53 std::vector<arm::app::DetectionResult> results;
54 auto image = get_img_array(imageIdx);
55
56 REQUIRE(RunInference(model, image));
57
58
59 TfLiteTensor* output_arr[2] = {nullptr,nullptr};
60 output_arr[0] = model.GetOutputTensor(0);
61 output_arr[1] = model.GetOutputTensor(1);
62
63 for (int i =0; i < 2; i++) {
64 REQUIRE(output_arr[i]);
65 REQUIRE(tflite::GetTensorData<T>(output_arr[i]));
66 }
67
68 RunPostProcessing(NULL,output_arr,results);
69
70 info("Got %ld boxes \n",results.size());
71
72 std::vector<std::vector<arm::app::DetectionResult>> expected_results;
73 get_expected_ut_results(expected_results);
74
75 /*validate got the same number of boxes */
76 REQUIRE(results.size() == expected_results[imageIdx].size());
77
78
79 for (int i=0; i < (int)results.size(); i++) {
80
81 info("%" PRIu32 ") (%f) -> %s {x=%d,y=%d,w=%d,h=%d}\n", (int)i,
82 results[i].m_normalisedVal, "Detection box:",
83 results[i].m_x0, results[i].m_y0, results[i].m_w, results[i].m_h );
84
85 /*validate confidence and box dimensions */
86 REQUIRE(fabs(results[i].m_normalisedVal - expected_results[imageIdx][i].m_normalisedVal) < 0.1);
87 REQUIRE(static_cast<int>(results[i].m_x0) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_x0)).epsilon(tolerance));
88 REQUIRE(static_cast<int>(results[i].m_y0) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_y0)).epsilon(tolerance));
89 REQUIRE(static_cast<int>(results[i].m_w) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_w)).epsilon(tolerance));
90 REQUIRE(static_cast<int>(results[i].m_h) == Approx(static_cast<int>((T)expected_results[imageIdx][i].m_h)).epsilon(tolerance));
91 }
92
93
94}
95
96
97TEST_CASE("Running inference with TensorFlow Lite Micro and YoloFastest", "[YoloFastest]")
98{
99 SECTION("Executing inferences sequentially")
100 {
101 arm::app::YoloFastestModel model{};
102
103 REQUIRE_FALSE(model.IsInited());
104 REQUIRE(model.Init());
105 REQUIRE(model.IsInited());
106
107 for (uint32_t i = 0 ; i < NUMBER_OF_FILES; ++i) {
108 TestInference<uint8_t>(i, model, 1);
109 }
110 }
111
112 for (uint32_t i = 0 ; i < NUMBER_OF_FILES; ++i) {
113 DYNAMIC_SECTION("Executing inference with re-init")
114 {
115 arm::app::YoloFastestModel model{};
116
117 REQUIRE_FALSE(model.IsInited());
118 REQUIRE(model.Init());
119 REQUIRE(model.IsInited());
120
121 TestInference<uint8_t>(i, model, 1);
122 }
123 }
124}