alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 25 | using namespace test; |
| 26 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 27 | bool RunInference(arm::app::Model& model, const int8_t vec[], const size_t copySz) |
| 28 | { |
| 29 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 30 | REQUIRE(inputTensor); |
| 31 | |
| 32 | memcpy(inputTensor->data.data, vec, copySz); |
| 33 | |
| 34 | return model.RunInference(); |
| 35 | } |
| 36 | |
| 37 | bool RunInferenceRandom(arm::app::Model& model) |
| 38 | { |
| 39 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 40 | REQUIRE(inputTensor); |
| 41 | |
| 42 | std::random_device rndDevice; |
| 43 | std::mt19937 mersenneGen{rndDevice()}; |
| 44 | std::uniform_int_distribution<short> dist {-128, 127}; |
| 45 | |
| 46 | auto gen = [&dist, &mersenneGen](){ |
| 47 | return dist(mersenneGen); |
| 48 | }; |
| 49 | |
| 50 | std::vector<int8_t> randomAudio(inputTensor->bytes); |
| 51 | std::generate(std::begin(randomAudio), std::end(randomAudio), gen); |
| 52 | |
| 53 | REQUIRE(RunInference(model, randomAudio.data(), inputTensor->bytes)); |
| 54 | return true; |
| 55 | } |
| 56 | |
Richard Burton | 0d11059 | 2021-08-12 17:26:30 +0100 | [diff] [blame] | 57 | TEST_CASE("Running random inference with TensorFlow Lite Micro and Wav2LetterModel Int8", "[Wav2Letter]") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 58 | { |
| 59 | arm::app::Wav2LetterModel model{}; |
| 60 | |
| 61 | REQUIRE_FALSE(model.IsInited()); |
| 62 | REQUIRE(model.Init()); |
| 63 | REQUIRE(model.IsInited()); |
| 64 | |
| 65 | REQUIRE(RunInferenceRandom(model)); |
| 66 | } |
| 67 | |
| 68 | template<typename T> |
| 69 | void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model) |
| 70 | { |
| 71 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 72 | REQUIRE(inputTensor); |
| 73 | |
| 74 | REQUIRE(RunInference(model, input_goldenFV, inputTensor->bytes)); |
| 75 | |
| 76 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
| 77 | |
| 78 | REQUIRE(outputTensor); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame^] | 79 | REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 80 | auto tensorData = tflite::GetTensorData<T>(outputTensor); |
| 81 | REQUIRE(tensorData); |
| 82 | |
| 83 | for (size_t i = 0; i < outputTensor->bytes; i++) { |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 84 | REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>(((T)output_goldenFV[i]))); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Richard Burton | 0d11059 | 2021-08-12 17:26:30 +0100 | [diff] [blame] | 88 | TEST_CASE("Running inference with Tflu and Wav2LetterModel Int8", "[Wav2Letter]") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 89 | { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame^] | 90 | REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_IFM_FILES); |
| 91 | for (uint32_t i = 0 ; i < NUMBER_OF_IFM_FILES; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | auto input_goldenFV = get_ifm_data_array(i);; |
| 93 | auto output_goldenFV = get_ofm_data_array(i); |
| 94 | |
| 95 | DYNAMIC_SECTION("Executing inference with re-init") |
| 96 | { |
| 97 | arm::app::Wav2LetterModel model{}; |
| 98 | |
| 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 | } |