blob: 4d3092e1fbf9f65cedaab78be5e65da4c2196a56 [file] [log] [blame]
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01003 * 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áin8f958872021-09-15 09:32:30 +010018#include "ImageUtils.hpp"
19#include "TestData_vww.hpp"
20#include "VisualWakeWordModel.hpp"
21#include "TensorFlowLiteMicro.hpp"
22
Isabella Gottardi5cbcd9e2021-10-15 10:17:33 +010023#include <catch.hpp>
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010024
25bool RunInference(arm::app::Model& model, const int8_t* imageData)
26{
27 TfLiteTensor* inputTensor = model.GetInputTensor(0);
28 REQUIRE(inputTensor);
29
Richard Burton00553462021-11-10 16:27:14 +000030 const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ?
Isabella Gottardi5cbcd9e2021-10-15 10:17:33 +010031 inputTensor->bytes :
Richard Burton00553462021-11-10 16:27:14 +000032 IFM_0_DATA_SIZE;
Isabella Gottardi5cbcd9e2021-10-15 10:17:33 +010033
34 memcpy(inputTensor->data.data, imageData, copySz);
35
36 if(model.IsDataSigned()){
Richard Burtoned35a6f2022-02-14 11:55:35 +000037 arm::app::image::ConvertImgToInt8(inputTensor->data.data, copySz);
Isabella Gottardi5cbcd9e2021-10-15 10:17:33 +010038 }
39
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010040 return model.RunInference();
41}
42
43template<typename T>
44void 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 Burton00553462021-11-10 16:27:14 +000054 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010055 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}