blob: 53c92ab3ccb82ebfca6059e947bf746714537c94 [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 */
alexander3c798932021-03-26 21:42:19 +000017#include "TensorFlowLiteMicro.hpp"
18#include "Wav2LetterModel.hpp"
19#include "TestData_asr.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[], const size_t copySz)
27{
28 TfLiteTensor* inputTensor = model.GetInputTensor(0);
29 REQUIRE(inputTensor);
30
31 memcpy(inputTensor->data.data, vec, copySz);
32
33 return model.RunInference();
34}
35
36bool RunInferenceRandom(arm::app::Model& model)
37{
38 TfLiteTensor* inputTensor = model.GetInputTensor(0);
39 REQUIRE(inputTensor);
40
41 std::random_device rndDevice;
42 std::mt19937 mersenneGen{rndDevice()};
43 std::uniform_int_distribution<short> dist {-128, 127};
44
45 auto gen = [&dist, &mersenneGen](){
46 return dist(mersenneGen);
47 };
48
49 std::vector<int8_t> randomAudio(inputTensor->bytes);
50 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
51
52 REQUIRE(RunInference(model, randomAudio.data(), inputTensor->bytes));
53 return true;
54}
55
Richard Burton0d110592021-08-12 17:26:30 +010056TEST_CASE("Running random inference with TensorFlow Lite Micro and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +000057{
58 arm::app::Wav2LetterModel model{};
59
60 REQUIRE_FALSE(model.IsInited());
61 REQUIRE(model.Init());
62 REQUIRE(model.IsInited());
63
64 REQUIRE(RunInferenceRandom(model));
65}
66
67template<typename T>
68void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
69{
70 TfLiteTensor* inputTensor = model.GetInputTensor(0);
71 REQUIRE(inputTensor);
72
73 REQUIRE(RunInference(model, input_goldenFV, inputTensor->bytes));
74
75 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
76
77 REQUIRE(outputTensor);
Richard Burton00553462021-11-10 16:27:14 +000078 REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE);
alexander3c798932021-03-26 21:42:19 +000079 auto tensorData = tflite::GetTensorData<T>(outputTensor);
80 REQUIRE(tensorData);
81
82 for (size_t i = 0; i < outputTensor->bytes; i++) {
Isabella Gottardi79d41542021-10-20 15:52:32 +010083 REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i])));
alexander3c798932021-03-26 21:42:19 +000084 }
85}
86
Richard Burton0d110592021-08-12 17:26:30 +010087TEST_CASE("Running inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter]")
alexander3c798932021-03-26 21:42:19 +000088{
Richard Burton00553462021-11-10 16:27:14 +000089 REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_IFM_FILES);
90 for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) {
alexander3c798932021-03-26 21:42:19 +000091 auto input_goldenFV = get_ifm_data_array(i);;
92 auto output_goldenFV = get_ofm_data_array(i);
93
94 DYNAMIC_SECTION("Executing inference with re-init")
95 {
96 arm::app::Wav2LetterModel model{};
97
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}