blob: 30ce19f8807214ba0b0724469ddfc470c80259bb [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtoned35a6f2022-02-14 11:55:35 +00002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 */
alexander3c798932021-03-26 21:42:19 +000017#include "ImageUtils.hpp"
18#include "MobileNetModel.hpp"
19#include "TensorFlowLiteMicro.hpp"
20#include "TestData_img_class.hpp"
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010021#include "BufAttributes.hpp"
alexander3c798932021-03-26 21:42:19 +000022
23#include <catch.hpp>
24
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010025namespace arm {
26 namespace app {
27 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
28 } /* namespace app */
29} /* namespace arm */
30
31extern uint8_t* GetModelPointer();
32extern size_t GetModelLen();
33
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010034using namespace test;
alexander3c798932021-03-26 21:42:19 +000035
Richard Burton0d110592021-08-12 17:26:30 +010036bool RunInference(arm::app::Model& model, const int8_t imageData[])
alexander3c798932021-03-26 21:42:19 +000037{
38 TfLiteTensor* inputTensor = model.GetInputTensor(0);
39 REQUIRE(inputTensor);
40
Richard Burton00553462021-11-10 16:27:14 +000041 const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ?
alexander3c798932021-03-26 21:42:19 +000042 inputTensor->bytes :
Richard Burton00553462021-11-10 16:27:14 +000043 IFM_0_DATA_SIZE;
alexander3c798932021-03-26 21:42:19 +000044 memcpy(inputTensor->data.data, imageData, copySz);
45
46 if(model.IsDataSigned()){
Richard Burtoned35a6f2022-02-14 11:55:35 +000047 arm::app::image::ConvertImgToInt8(inputTensor->data.data, copySz);
alexander3c798932021-03-26 21:42:19 +000048 }
49
50 return model.RunInference();
51}
52
53template<typename T>
54void TestInference(int imageIdx, arm::app::Model& model, T tolerance) {
55 auto image = get_ifm_data_array(imageIdx);
56 auto goldenFV = get_ofm_data_array(imageIdx);
57
58 REQUIRE(RunInference(model, image));
59
60 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
61
62 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000063 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000064 auto tensorData = tflite::GetTensorData<T>(outputTensor);
65 REQUIRE(tensorData);
66
67 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010068 REQUIRE(static_cast<int>(tensorData[i]) == Approx(static_cast<int>((T)goldenFV[i])).epsilon(tolerance));
alexander3c798932021-03-26 21:42:19 +000069 }
70}
71
72
73TEST_CASE("Running inference with TensorFlow Lite Micro and MobileNeV2 Uint8", "[MobileNetV2]")
74{
75 SECTION("Executing inferences sequentially")
76 {
77 arm::app::MobileNetModel model{};
78
79 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010080 REQUIRE(model.Init(arm::app::tensorArena,
81 sizeof(arm::app::tensorArena),
82 GetModelPointer(),
83 GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000084 REQUIRE(model.IsInited());
85
Richard Burton00553462021-11-10 16:27:14 +000086 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +000087 TestInference<uint8_t>(i, model, 1);
88 }
89 }
90
Richard Burton00553462021-11-10 16:27:14 +000091 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +000092 DYNAMIC_SECTION("Executing inference with re-init")
93 {
94 arm::app::MobileNetModel model{};
95
96 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010097 REQUIRE(model.Init(arm::app::tensorArena,
98 sizeof(arm::app::tensorArena),
99 GetModelPointer(),
100 GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000101 REQUIRE(model.IsInited());
102
103 TestInference<uint8_t>(i, model, 1);
104 }
105 }
106}