blob: 698382f02bcefc5dad53cffec13d7afc574d79a4 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 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 "MobileNetModel.hpp"
20#include "TensorFlowLiteMicro.hpp"
21#include "TestData_img_class.hpp"
22
23#include <catch.hpp>
24
25
26bool RunInference(arm::app::Model& model, const uint8_t imageData[])
27{
28 TfLiteTensor* inputTensor = model.GetInputTensor(0);
29 REQUIRE(inputTensor);
30
31 const size_t copySz = inputTensor->bytes < IFM_DATA_SIZE ?
32 inputTensor->bytes :
33 IFM_DATA_SIZE;
34 memcpy(inputTensor->data.data, imageData, copySz);
35
36 if(model.IsDataSigned()){
37 convertImgIoInt8(inputTensor->data.data, copySz);
38 }
39
40 return model.RunInference();
41}
42
43template<typename T>
44void TestInference(int imageIdx, arm::app::Model& model, T tolerance) {
45 auto image = get_ifm_data_array(imageIdx);
46 auto goldenFV = get_ofm_data_array(imageIdx);
47
48 REQUIRE(RunInference(model, image));
49
50 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
51
52 REQUIRE(outputTensor);
53 REQUIRE(outputTensor->bytes == OFM_DATA_SIZE);
54 auto tensorData = tflite::GetTensorData<T>(outputTensor);
55 REQUIRE(tensorData);
56
57 for (size_t i = 0; i < outputTensor->bytes; i++) {
58 REQUIRE((int)tensorData[i] == Approx((int)((T)goldenFV[i])).epsilon(tolerance));
59 }
60}
61
62
63TEST_CASE("Running inference with TensorFlow Lite Micro and MobileNeV2 Uint8", "[MobileNetV2]")
64{
65 SECTION("Executing inferences sequentially")
66 {
67 arm::app::MobileNetModel model{};
68
69 REQUIRE_FALSE(model.IsInited());
70 REQUIRE(model.Init());
71 REQUIRE(model.IsInited());
72
73 for (uint32_t i = 0 ; i < NUMBER_OF_FM_FILES; ++i) {
74 TestInference<uint8_t>(i, model, 1);
75 }
76 }
77
78 for (uint32_t i = 0 ; i < NUMBER_OF_FM_FILES; ++i) {
79 DYNAMIC_SECTION("Executing inference with re-init")
80 {
81 arm::app::MobileNetModel model{};
82
83 REQUIRE_FALSE(model.IsInited());
84 REQUIRE(model.Init());
85 REQUIRE(model.IsInited());
86
87 TestInference<uint8_t>(i, model, 1);
88 }
89 }
90}