Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [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. |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [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 | */ |
| 17 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 18 | #include "ImageUtils.hpp" |
| 19 | #include "TestData_vww.hpp" |
| 20 | #include "VisualWakeWordModel.hpp" |
| 21 | #include "TensorFlowLiteMicro.hpp" |
| 22 | |
Isabella Gottardi | 5cbcd9e | 2021-10-15 10:17:33 +0100 | [diff] [blame] | 23 | #include <catch.hpp> |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 24 | |
| 25 | bool RunInference(arm::app::Model& model, const int8_t* imageData) |
| 26 | { |
| 27 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 28 | REQUIRE(inputTensor); |
| 29 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 30 | const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ? |
Isabella Gottardi | 5cbcd9e | 2021-10-15 10:17:33 +0100 | [diff] [blame] | 31 | inputTensor->bytes : |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 32 | IFM_0_DATA_SIZE; |
Isabella Gottardi | 5cbcd9e | 2021-10-15 10:17:33 +0100 | [diff] [blame] | 33 | |
| 34 | memcpy(inputTensor->data.data, imageData, copySz); |
| 35 | |
| 36 | if(model.IsDataSigned()){ |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 37 | arm::app::image::ConvertImgToInt8(inputTensor->data.data, copySz); |
Isabella Gottardi | 5cbcd9e | 2021-10-15 10:17:33 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 40 | return model.RunInference(); |
| 41 | } |
| 42 | |
| 43 | template<typename T> |
| 44 | void TestInference(int imageIdx,arm::app::Model& model) { |
| 45 | |
| 46 | auto image = test::get_ifm_data_array(imageIdx); |
| 47 | auto goldenFV = test::get_ofm_data_array(imageIdx); |
| 48 | |
| 49 | REQUIRE(RunInference(model, image)); |
| 50 | |
| 51 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
| 52 | |
| 53 | REQUIRE(outputTensor); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 54 | REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 55 | auto tensorData = tflite::GetTensorData<T>(outputTensor); |
| 56 | REQUIRE(tensorData); |
| 57 | |
| 58 | for (size_t i = 0; i < outputTensor->bytes; i++) { |
| 59 | auto testVal = static_cast<int>(tensorData[i]); |
| 60 | auto goldenVal = static_cast<int>(goldenFV[i]); |
| 61 | CHECK(testVal == goldenVal); |
| 62 | } |
| 63 | } |