blob: e6e775327d1227220af76df717ca17400fc9402a [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 */
Kshitij Sisodia76a15802021-12-24 11:05:11 +000017#include "MicroNetKwsModel.hpp"
alexander3c798932021-03-26 21:42:19 +000018#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 +010025using namespace test;
26
alexander3c798932021-03-26 21:42:19 +000027bool RunInference(arm::app::Model& model, const int8_t vec[])
28{
29 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 ?
alexander3c798932021-03-26 21:42:19 +000033 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
40bool RunInferenceRandom(arm::app::Model& model)
41{
42 TfLiteTensor* inputTensor = model.GetInputTensor(0);
43 REQUIRE(inputTensor);
44
45 std::random_device rndDevice;
46 std::mt19937 mersenneGen{rndDevice()};
47 std::uniform_int_distribution<short> dist {-128, 127};
48
49 auto gen = [&dist, &mersenneGen](){
50 return dist(mersenneGen);
51 };
52
53 std::vector<int8_t> randomAudio(inputTensor->bytes);
54 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
55
56 REQUIRE(RunInference(model, randomAudio.data()));
57 return true;
58}
59
60template<typename T>
61void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
62{
63 REQUIRE(RunInference(model, input_goldenFV));
64
65 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
66
67 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000068 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000069 auto tensorData = tflite::GetTensorData<T>(outputTensor);
70 REQUIRE(tensorData);
71
72 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010073 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +000074 }
75}
76
Kshitij Sisodia76a15802021-12-24 11:05:11 +000077TEST_CASE("Running random inference with TensorFlow Lite Micro and MicroNetKwsModel Int8", "[MicroNetKws]")
alexander3c798932021-03-26 21:42:19 +000078{
Kshitij Sisodia76a15802021-12-24 11:05:11 +000079 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +000080
81 REQUIRE_FALSE(model.IsInited());
82 REQUIRE(model.Init());
83 REQUIRE(model.IsInited());
84
85 REQUIRE(RunInferenceRandom(model));
86}
87
Kshitij Sisodia76a15802021-12-24 11:05:11 +000088TEST_CASE("Running inference with TensorFlow Lite Micro and MicroNetKwsModel int8", "[MicroNetKws]")
alexander3c798932021-03-26 21:42:19 +000089{
Richard Burton00553462021-11-10 16:27:14 +000090 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES);
91 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +000092 const int8_t* input_goldenFV = get_ifm_data_array(i);;
93 const int8_t* output_goldenFV = get_ofm_data_array(i);
94
95 DYNAMIC_SECTION("Executing inference with re-init")
96 {
Kshitij Sisodia76a15802021-12-24 11:05:11 +000097 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +000098
99 REQUIRE_FALSE(model.IsInited());
100 REQUIRE(model.Init());
101 REQUIRE(model.IsInited());
102
103 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
104
105 }
106 }
107}