blob: 82bffc66ad09afd44811d7d7ce5bcab5f0e2891c [file] [log] [blame]
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01001/*
Kshitij Sisodia2ea46232022-12-19 16:37:33 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates
3 * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01004 *
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"
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000019#include "TensorFlowLiteMicro.hpp"
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010020#include "TestData_vww.hpp"
21#include "VisualWakeWordModel.hpp"
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010022
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
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000030 const size_t copySz =
31 inputTensor->bytes < IFM_0_DATA_SIZE ? inputTensor->bytes : IFM_0_DATA_SIZE;
Isabella Gottardi5cbcd9e2021-10-15 10:17:33 +010032
33 memcpy(inputTensor->data.data, imageData, copySz);
34
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000035 if (model.IsDataSigned()) {
Richard Burtoned35a6f2022-02-14 11:55:35 +000036 arm::app::image::ConvertImgToInt8(inputTensor->data.data, copySz);
Isabella Gottardi5cbcd9e2021-10-15 10:17:33 +010037 }
38
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010039 return model.RunInference();
40}
41
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000042template <typename T>
43void TestInference(int imageIdx, arm::app::Model& model)
44{
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010045
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000046 auto image = test::GetIfmDataArray(imageIdx);
47 auto goldenFV = test::GetOfmDataArray(imageIdx);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010048
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++) {
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000059 auto testVal = static_cast<int>(tensorData[i]);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010060 auto goldenVal = static_cast<int>(goldenFV[i]);
61 CHECK(testVal == goldenVal);
62 }
63}