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