blob: 41ecc3c18ea8df8a3f16de399b9b3e266f31c582 [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 "TestData_kws.hpp"
19#include "TensorFlowLiteMicro.hpp"
20
21#include <catch.hpp>
22#include <random>
23
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010024using namespace test;
25
alexander3c798932021-03-26 21:42:19 +000026bool RunInference(arm::app::Model& model, const int8_t vec[])
27{
28 TfLiteTensor* inputTensor = model.GetInputTensor(0);
29 REQUIRE(inputTensor);
30
Richard Burton00553462021-11-10 16:27:14 +000031 const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ?
alexander3c798932021-03-26 21:42:19 +000032 inputTensor->bytes :
Richard Burton00553462021-11-10 16:27:14 +000033 IFM_0_DATA_SIZE;
alexander3c798932021-03-26 21:42:19 +000034 memcpy(inputTensor->data.data, vec, copySz);
35
36 return model.RunInference();
37}
38
39bool RunInferenceRandom(arm::app::Model& model)
40{
41 TfLiteTensor* inputTensor = model.GetInputTensor(0);
42 REQUIRE(inputTensor);
43
44 std::random_device rndDevice;
45 std::mt19937 mersenneGen{rndDevice()};
46 std::uniform_int_distribution<short> dist {-128, 127};
47
48 auto gen = [&dist, &mersenneGen](){
49 return dist(mersenneGen);
50 };
51
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>
60void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
61{
62 REQUIRE(RunInference(model, input_goldenFV));
63
64 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
65
66 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000067 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000068 auto tensorData = tflite::GetTensorData<T>(outputTensor);
69 REQUIRE(tensorData);
70
71 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010072 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +000073 }
74}
75
Kshitij Sisodia76a15802021-12-24 11:05:11 +000076TEST_CASE("Running random inference with TensorFlow Lite Micro and MicroNetKwsModel Int8", "[MicroNetKws]")
alexander3c798932021-03-26 21:42:19 +000077{
Kshitij Sisodia76a15802021-12-24 11:05:11 +000078 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +000079
80 REQUIRE_FALSE(model.IsInited());
81 REQUIRE(model.Init());
82 REQUIRE(model.IsInited());
83
84 REQUIRE(RunInferenceRandom(model));
85}
86
Kshitij Sisodia76a15802021-12-24 11:05:11 +000087TEST_CASE("Running inference with TensorFlow Lite Micro and MicroNetKwsModel int8", "[MicroNetKws]")
alexander3c798932021-03-26 21:42:19 +000088{
Richard Burton00553462021-11-10 16:27:14 +000089 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES);
90 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +000091 const int8_t* input_goldenFV = get_ifm_data_array(i);;
92 const int8_t* output_goldenFV = get_ofm_data_array(i);
93
alexander31ae9f02022-02-10 16:15:54 +000094 DYNAMIC_SECTION("Executing inference with re-init " << i)
alexander3c798932021-03-26 21:42:19 +000095 {
Kshitij Sisodia76a15802021-12-24 11:05:11 +000096 arm::app::MicroNetKwsModel model{};
alexander3c798932021-03-26 21:42:19 +000097
98 REQUIRE_FALSE(model.IsInited());
99 REQUIRE(model.Init());
100 REQUIRE(model.IsInited());
101
102 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
103
104 }
105 }
106}