blob: f0e5c02e0c44bb746b5e2965f911c0217b72de7f [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 "DsCnnModel.hpp"
18#include "hal.h"
19#include "TestData_kws.hpp"
20#include "TensorFlowLiteMicro.hpp"
21
22#include <catch.hpp>
23#include <random>
24
25namespace arm {
26namespace app {
27namespace kws {
28bool RunInference(arm::app::Model& model, const int8_t vec[])
29{
30 TfLiteTensor* inputTensor = model.GetInputTensor(0);
31 REQUIRE(inputTensor);
32
33 const size_t copySz = inputTensor->bytes < IFM_DATA_SIZE ?
34 inputTensor->bytes :
35 IFM_DATA_SIZE;
36 memcpy(inputTensor->data.data, vec, copySz);
37
38 return model.RunInference();
39}
40
41bool RunInferenceRandom(arm::app::Model& model)
42{
43 TfLiteTensor* inputTensor = model.GetInputTensor(0);
44 REQUIRE(inputTensor);
45
46 std::random_device rndDevice;
47 std::mt19937 mersenneGen{rndDevice()};
48 std::uniform_int_distribution<short> dist {-128, 127};
49
50 auto gen = [&dist, &mersenneGen](){
51 return dist(mersenneGen);
52 };
53
54 std::vector<int8_t> randomAudio(inputTensor->bytes);
55 std::generate(std::begin(randomAudio), std::end(randomAudio), gen);
56
57 REQUIRE(RunInference(model, randomAudio.data()));
58 return true;
59}
60
61template<typename T>
62void TestInference(const T* input_goldenFV, const T* output_goldenFV, arm::app::Model& model)
63{
64 REQUIRE(RunInference(model, input_goldenFV));
65
66 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
67
68 REQUIRE(outputTensor);
69 REQUIRE(outputTensor->bytes == OFM_DATA_SIZE);
70 auto tensorData = tflite::GetTensorData<T>(outputTensor);
71 REQUIRE(tensorData);
72
73 for (size_t i = 0; i < outputTensor->bytes; i++) {
74 REQUIRE((int)tensorData[i] == (int)((T)output_goldenFV[i]));
75 }
76}
77
78TEST_CASE("Running random inference with Tflu and DsCnnModel Int8", "[DS_CNN]")
79{
80 arm::app::DsCnnModel model{};
81
82 REQUIRE_FALSE(model.IsInited());
83 REQUIRE(model.Init());
84 REQUIRE(model.IsInited());
85
86 REQUIRE(RunInferenceRandom(model));
87}
88
89TEST_CASE("Running inference with Tflu and DsCnnModel Uint8", "[DS_CNN]")
90{
91 for (uint32_t i = 0 ; i < NUMBER_OF_FM_FILES; ++i) {
92 const int8_t* input_goldenFV = get_ifm_data_array(i);
93 const int8_t* output_goldenFV = get_ofm_data_array(i);
94
95 DYNAMIC_SECTION("Executing inference with re-init")
96 {
97 arm::app::DsCnnModel 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}
108
109} //namespace
110} //namespace
111} //namespace