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 | */ |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame^] | 17 | #include "MicroNetKwsModel.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 18 | #include "hal.h" |
| 19 | #include "TestData_kws.hpp" |
| 20 | #include "TensorFlowLiteMicro.hpp" |
| 21 | |
| 22 | #include <catch.hpp> |
| 23 | #include <random> |
| 24 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 25 | namespace test { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 26 | namespace kws { |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 27 | |
| 28 | bool RunInference(arm::app::Model& model, const int8_t vec[]) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 30 | REQUIRE(inputTensor); |
| 31 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 32 | const size_t copySz = inputTensor->bytes < IFM_0_DATA_SIZE ? |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 33 | inputTensor->bytes : |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 34 | IFM_0_DATA_SIZE; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 35 | memcpy(inputTensor->data.data, vec, copySz); |
| 36 | |
| 37 | return model.RunInference(); |
| 38 | } |
| 39 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 40 | bool RunInferenceRandom(arm::app::Model& model) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 41 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 42 | REQUIRE(inputTensor); |
| 43 | |
| 44 | std::random_device rndDevice; |
| 45 | std::mt19937 mersenneGen{rndDevice()}; |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 46 | std::uniform_int_distribution<short> dist{-128, 127}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 47 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 48 | auto gen = [&dist, &mersenneGen]() { |
| 49 | return dist(mersenneGen); |
| 50 | }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | |
| 59 | template<typename T> |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 60 | void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 61 | REQUIRE(RunInference(model, input_goldenFV)); |
| 62 | |
| 63 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
| 64 | |
| 65 | REQUIRE(outputTensor); |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 66 | REQUIRE(outputTensor->bytes == OFM_0_DATA_SIZE); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | auto tensorData = tflite::GetTensorData<T>(outputTensor); |
| 68 | REQUIRE(tensorData); |
| 69 | |
| 70 | for (size_t i = 0; i < outputTensor->bytes; i++) { |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 71 | REQUIRE(static_cast<int>(tensorData[i]) == static_cast<int>((T) output_goldenFV[i])); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame^] | 75 | TEST_CASE("Running random inference with Tflu and MicroNetKwsModel Int8", "[MicroNetKws]") { |
| 76 | arm::app::MicroNetKwsModel model{}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 77 | |
| 78 | REQUIRE_FALSE(model.IsInited()); |
| 79 | REQUIRE(model.Init()); |
| 80 | REQUIRE(model.IsInited()); |
| 81 | |
| 82 | REQUIRE(RunInferenceRandom(model)); |
| 83 | } |
| 84 | |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame^] | 85 | TEST_CASE("Running inference with Tflu and MicroNetKwsModel Int8", "[MicroNetKws]") { |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 86 | REQUIRE(NUMBER_OF_IFM_FILES == NUMBER_OF_OFM_FILES); |
| 87 | for (uint32_t i = 0; i < NUMBER_OF_IFM_FILES; ++i) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 88 | const int8_t* input_goldenFV = get_ifm_data_array(i); |
| 89 | const int8_t* output_goldenFV = get_ofm_data_array(i); |
| 90 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 91 | DYNAMIC_SECTION("Executing inference with re-init") { |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame^] | 92 | arm::app::MicroNetKwsModel model{}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 93 | |
| 94 | REQUIRE_FALSE(model.IsInited()); |
| 95 | REQUIRE(model.Init()); |
| 96 | REQUIRE(model.IsInited()); |
| 97 | |
| 98 | TestInference<int8_t>(input_goldenFV, output_goldenFV, model); |
| 99 | |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | } //namespace |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 105 | } //namespace |