blob: 6cc1190c2621779e7e659edea5b5c2652771953c [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;
Liam Barry213a5432022-05-09 17:06:19 +010028 namespace img_class {
29 extern uint8_t* GetModelPointer();
30 extern size_t GetModelLen();
31 } /* namespace img_class */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010032 } /* namespace app */
33} /* namespace arm */
34
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010035using namespace test;
alexander3c798932021-03-26 21:42:19 +000036
Richard Burton0d110592021-08-12 17:26:30 +010037bool RunInference(arm::app::Model& model, const int8_t imageData[])
alexander3c798932021-03-26 21:42:19 +000038{
39 TfLiteTensor* inputTensor = model.GetInputTensor(0);
40 REQUIRE(inputTensor);
41
Richard Burton00553462021-11-10 16:27:14 +000042 const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ?
alexander3c798932021-03-26 21:42:19 +000043 inputTensor->bytes :
Richard Burton00553462021-11-10 16:27:14 +000044 IFM_0_DATA_SIZE;
alexander3c798932021-03-26 21:42:19 +000045 memcpy(inputTensor->data.data, imageData, copySz);
46
47 if(model.IsDataSigned()){
Richard Burtoned35a6f2022-02-14 11:55:35 +000048 arm::app::image::ConvertImgToInt8(inputTensor->data.data, copySz);
alexander3c798932021-03-26 21:42:19 +000049 }
50
51 return model.RunInference();
52}
53
54template<typename T>
55void TestInference(int imageIdx, arm::app::Model& model, T tolerance) {
56 auto image = get_ifm_data_array(imageIdx);
57 auto goldenFV = get_ofm_data_array(imageIdx);
58
59 REQUIRE(RunInference(model, image));
60
61 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
62
63 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000064 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000065 auto tensorData = tflite::GetTensorData<T>(outputTensor);
66 REQUIRE(tensorData);
67
68 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010069 REQUIRE(static_cast<int>(tensorData[i]) == Approx(static_cast<int>((T)goldenFV[i])).epsilon(tolerance));
alexander3c798932021-03-26 21:42:19 +000070 }
71}
72
73
74TEST_CASE("Running inference with TensorFlow Lite Micro and MobileNeV2 Uint8", "[MobileNetV2]")
75{
76 SECTION("Executing inferences sequentially")
77 {
78 arm::app::MobileNetModel model{};
79
80 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010081 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +010082 sizeof(arm::app::tensorArena),
83 arm::app::img_class::GetModelPointer(),
84 arm::app::img_class::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +000085 REQUIRE(model.IsInited());
86
Richard Burton00553462021-11-10 16:27:14 +000087 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +000088 TestInference<uint8_t>(i, model, 1);
89 }
90 }
91
Richard Burton00553462021-11-10 16:27:14 +000092 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +000093 DYNAMIC_SECTION("Executing inference with re-init")
94 {
95 arm::app::MobileNetModel model{};
96
97 REQUIRE_FALSE(model.IsInited());
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010098 REQUIRE(model.Init(arm::app::tensorArena,
Liam Barry213a5432022-05-09 17:06:19 +010099 sizeof(arm::app::tensorArena),
100 arm::app::img_class::GetModelPointer(),
101 arm::app::img_class::GetModelLen()));
alexander3c798932021-03-26 21:42:19 +0000102 REQUIRE(model.IsInited());
103
104 TestInference<uint8_t>(i, model, 1);
105 }
106 }
107}