blob: 1fa409289ef1a50e83121ed085a2039f02874f50 [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 "hal.h"
18#include "TensorFlowLiteMicro.hpp"
19#include "Wav2LetterModel.hpp"
20#include "TestData_asr.hpp"
21
22#include <catch.hpp>
23#include <random>
24
25bool RunInference(arm::app::Model& model, const int8_t vec[], const size_t copySz)
26{
27 TfLiteTensor* inputTensor = model.GetInputTensor(0);
28 REQUIRE(inputTensor);
29
30 memcpy(inputTensor->data.data, vec, copySz);
31
32 return model.RunInference();
33}
34
35bool RunInferenceRandom(arm::app::Model& model)
36{
37 TfLiteTensor* inputTensor = model.GetInputTensor(0);
38 REQUIRE(inputTensor);
39
40 std::random_device rndDevice;
41 std::mt19937 mersenneGen{rndDevice()};
42 std::uniform_int_distribution<short> dist {-128, 127};
43
44 auto gen = [&dist, &mersenneGen](){
45 return dist(mersenneGen);
46 };
47
48 std::vector<int8_t> randomAudio(inputTensor->bytes);
49 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
50
51 REQUIRE(RunInference(model, randomAudio.data(), inputTensor->bytes));
52 return true;
53}
54
55/* Skip this test, Wav2LetterModel if not Vela optimized but only from ML-zoo will fail. */
56TEST_CASE("Running random inference with TensorFlow Lite Micro and Wav2LetterModel Int8", "[Wav2Letter][.]")
57{
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);
78 REQUIRE(outputTensor->bytes == OFM_DATA_SIZE);
79 auto tensorData = tflite::GetTensorData<T>(outputTensor);
80 REQUIRE(tensorData);
81
82 for (size_t i = 0; i < outputTensor->bytes; i++) {
83 REQUIRE((int)tensorData[i] == (int)((T)output_goldenFV[i]));
84 }
85}
86
87TEST_CASE("Running inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter][.]")
88{
89 for (uint32_t i = 0 ; i < NUMBER_OF_FM_FILES; ++i) {
90 auto input_goldenFV = get_ifm_data_array(i);;
91 auto output_goldenFV = get_ofm_data_array(i);
92
93 DYNAMIC_SECTION("Executing inference with re-init")
94 {
95 arm::app::Wav2LetterModel model{};
96
97 REQUIRE_FALSE(model.IsInited());
98 REQUIRE(model.Init());
99 REQUIRE(model.IsInited());
100
101 TestInference<int8_t>(input_goldenFV, output_goldenFV, model);
102
103 }
104 }
105}