blob: ad1731b3529f47d136fdb2c59b0c7a3fc0fb2082 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
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#include "DsCnnModel.hpp"
18#include "hal.h"
19#include "TestData_kws.hpp"
20#include "TensorFlowLiteMicro.hpp"
21
22#include <catch.hpp>
23#include <random>
24
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010025namespace test {
alexander3c798932021-03-26 21:42:19 +000026namespace kws {
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010027
28bool RunInference(arm::app::Model& model, const int8_t vec[]) {
alexander3c798932021-03-26 21:42:19 +000029 TfLiteTensor* inputTensor = model.GetInputTensor(0);
30 REQUIRE(inputTensor);
31
Richard Burton00553462021-11-10 16:27:14 +000032 const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ?
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010033 inputTensor->bytes :
Richard Burton00553462021-11-10 16:27:14 +000034 IFM_0_DATA_SIZE;
alexander3c798932021-03-26 21:42:19 +000035 memcpy(inputTensor->data.data, vec, copySz);
36
37 return model.RunInference();
38}
39
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010040bool RunInferenceRandom(arm::app::Model& model) {
alexander3c798932021-03-26 21:42:19 +000041 TfLiteTensor* inputTensor = model.GetInputTensor(0);
42 REQUIRE(inputTensor);
43
44 std::random_device rndDevice;
45 std::mt19937 mersenneGen{rndDevice()};
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010046 std::uniform_int_distribution<short> dist{-128, 127};
alexander3c798932021-03-26 21:42:19 +000047
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010048 auto gen = [&dist, &mersenneGen]() {
49 return dist(mersenneGen);
50 };
alexander3c798932021-03-26 21:42:19 +000051
52 std::vector<int8_t> randomAudio(inputTensor->bytes);
53 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
54
55 REQUIRE(RunInference(model, randomAudio.data()));
56 return true;
57}
58
59template<typename T>
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010060void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model) {
alexander3c798932021-03-26 21:42:19 +000061 REQUIRE(RunInference(model, input_goldenFV));
62
63 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
64
65 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000066 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000067 auto tensorData = tflite::GetTensorData<T>(outputTensor);
68 REQUIRE(tensorData);
69
70 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010071 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>((T) output_goldenFV[i]));
alexander3c798932021-03-26 21:42:19 +000072 }
73}
74
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010075TEST_CASE("Running random inference with Tflu and DsCnnModel Int8", "[DS_CNN]") {
alexander3c798932021-03-26 21:42:19 +000076 arm::app::DsCnnModel model{};
77
78 REQUIRE_FALSE(model.IsInited());
79 REQUIRE(model.Init());
80 REQUIRE(model.IsInited());
81
82 REQUIRE(RunInferenceRandom(model));
83}
84
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010085TEST_CASE("Running inference with Tflu and DsCnnModel Uint8", "[DS_CNN]") {
Richard Burton00553462021-11-10 16:27:14 +000086 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES);
87 for (uint32_t i = 0; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +000088 const int8_t* input_goldenFV = get_ifm_data_array(i);
89 const int8_t* output_goldenFV = get_ofm_data_array(i);
90
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010091 DYNAMIC_SECTION("Executing inference with re-init") {
alexander3c798932021-03-26 21:42:19 +000092 arm::app::DsCnnModel model{};
93
94 REQUIRE_FALSE(model.IsInited());
95 REQUIRE(model.Init());
96 REQUIRE(model.IsInited());
97
98 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
99
100 }
101 }
102}
103
104} //namespace
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100105} //namespace