blob: e210c336f1bd69115165c0e15dd8cdfbafb25fc8 [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
32 const size_t copySz = inputTensor->bytes < IFM_DATA_SIZE ?
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010033 inputTensor->bytes :
34 IFM_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);
66 REQUIRE(outputTensor->bytes == OFM_DATA_SIZE);
67 auto tensorData = tflite::GetTensorData<T>(outputTensor);
68 REQUIRE(tensorData);
69
70 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010071 REQUIRE((int) tensorData[i] == (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]") {
86 for (uint32_t i = 0; i < NUMBER_OF_FM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +000087 const int8_t* input_goldenFV = get_ifm_data_array(i);
88 const int8_t* output_goldenFV = get_ofm_data_array(i);
89
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010090 DYNAMIC_SECTION("Executing inference with re-init") {
alexander3c798932021-03-26 21:42:19 +000091 arm::app::DsCnnModel model{};
92
93 REQUIRE_FALSE(model.IsInited());
94 REQUIRE(model.Init());
95 REQUIRE(model.IsInited());
96
97 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
98
99 }
100 }
101}
102
103} //namespace
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100104} //namespace