alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 17 | #include "ImageUtils.hpp" |
| 18 | #include "MobileNetModel.hpp" |
| 19 | #include "TensorFlowLiteMicro.hpp" |
| 20 | #include "TestData_img_class.hpp" |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 21 | #include "BufAttributes.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 22 | |
| 23 | #include <catch.hpp> |
| 24 | |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 25 | namespace arm { |
| 26 | namespace app { |
| 27 | static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE; |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 28 | namespace img_class { |
| 29 | extern uint8_t* GetModelPointer(); |
| 30 | extern size_t GetModelLen(); |
| 31 | } /* namespace img_class */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 32 | } /* namespace app */ |
| 33 | } /* namespace arm */ |
| 34 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 35 | using namespace test; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 36 | |
Richard Burton | 0d11059 | 2021-08-12 17:26:30 +0100 | [diff] [blame] | 37 | bool RunInference(arm::app::Model& model, const int8_t imageData[]) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 38 | { |
| 39 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 40 | REQUIRE(inputTensor); |
| 41 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 42 | const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ? |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 43 | inputTensor->bytes : |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 44 | IFM_0_DATA_SIZE; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | memcpy(inputTensor->data.data, imageData, copySz); |
| 46 | |
| 47 | if(model.IsDataSigned()){ |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 48 | arm::app::image::ConvertImgToInt8(inputTensor->data.data, copySz); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | return model.RunInference(); |
| 52 | } |
| 53 | |
| 54 | template<typename T> |
| 55 | void 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 Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 64 | REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 65 | auto tensorData = tflite::GetTensorData<T>(outputTensor); |
| 66 | REQUIRE(tensorData); |
| 67 | |
| 68 | for (size_t i = 0; i < outputTensor->bytes; i++) { |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 69 | REQUIRE(static_cast<int>(tensorData[i]) == Approx(static_cast<int>((T)goldenFV[i])).epsilon(tolerance)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | |
| 74 | TEST_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 Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 81 | REQUIRE(model.Init(arm::app::tensorArena, |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 82 | sizeof(arm::app::tensorArena), |
| 83 | arm::app::img_class::GetModelPointer(), |
| 84 | arm::app::img_class::GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 85 | REQUIRE(model.IsInited()); |
| 86 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 87 | for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 88 | TestInference<uint8_t>(i, model, 1); |
| 89 | } |
| 90 | } |
| 91 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 92 | for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 93 | DYNAMIC_SECTION("Executing inference with re-init") |
| 94 | { |
| 95 | arm::app::MobileNetModel model{}; |
| 96 | |
| 97 | REQUIRE_FALSE(model.IsInited()); |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 98 | REQUIRE(model.Init(arm::app::tensorArena, |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 99 | sizeof(arm::app::tensorArena), |
| 100 | arm::app::img_class::GetModelPointer(), |
| 101 | arm::app::img_class::GetModelLen())); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 102 | REQUIRE(model.IsInited()); |
| 103 | |
| 104 | TestInference<uint8_t>(i, model, 1); |
| 105 | } |
| 106 | } |
| 107 | } |