1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/lite/micro/examples/micro_speech/micro_features/micro_features_generator.h"
17
18 #include "tensorflow/lite/c/common.h"
19 #include "tensorflow/lite/micro/examples/micro_speech/micro_features/no_feature_data_slice.h"
20 #include "tensorflow/lite/micro/examples/micro_speech/micro_features/yes_feature_data_slice.h"
21 #include "tensorflow/lite/micro/examples/micro_speech/no_30ms_sample_data.h"
22 #include "tensorflow/lite/micro/examples/micro_speech/yes_30ms_sample_data.h"
23 #include "tensorflow/lite/micro/micro_error_reporter.h"
24 #include "tensorflow/lite/micro/testing/micro_test.h"
25
26 // This is a test-only API, not exposed in any public headers, so declare it.
27 void SetMicroFeaturesNoiseEstimates(const uint32_t* estimate_presets);
28
29 TF_LITE_MICRO_TESTS_BEGIN
30
TF_LITE_MICRO_TEST(TestMicroFeaturesGeneratorYes)31 TF_LITE_MICRO_TEST(TestMicroFeaturesGeneratorYes) {
32 tflite::MicroErrorReporter micro_error_reporter;
33
34 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk,
35 InitializeMicroFeatures(µ_error_reporter));
36
37 // The micro features pipeline retains state from previous calls to help
38 // estimate the background noise. Unfortunately this makes it harder to
39 // exactly reproduce results in a test environment, so use a known snapshot
40 // of the parameters at the point that the golden feature values were
41 // created.
42 const uint32_t yes_estimate_presets[] = {
43 1062898, 2644477, 1257642, 1864718, 412722, 725703, 395721, 474082,
44 173046, 255856, 158966, 153736, 69181, 199100, 144493, 227740,
45 110573, 164330, 79666, 144650, 122947, 476799, 398553, 497493,
46 322152, 1140005, 566716, 690605, 308902, 347481, 109891, 170457,
47 73901, 100975, 42963, 72325, 34183, 20207, 6640, 9468,
48 };
49 SetMicroFeaturesNoiseEstimates(yes_estimate_presets);
50
51 int8_t yes_calculated_data[g_yes_feature_data_slice_size];
52 size_t num_samples_read;
53 TfLiteStatus yes_status = GenerateMicroFeatures(
54 µ_error_reporter, g_yes_30ms_sample_data,
55 g_yes_30ms_sample_data_size, g_yes_feature_data_slice_size,
56 yes_calculated_data, &num_samples_read);
57 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, yes_status);
58
59 for (int i = 0; i < g_yes_feature_data_slice_size; ++i) {
60 const int expected = g_yes_feature_data_slice[i];
61 const int actual = yes_calculated_data[i];
62 TF_LITE_MICRO_EXPECT_EQ(expected, actual);
63 if (expected != actual) {
64 TF_LITE_REPORT_ERROR(µ_error_reporter,
65 "Expected value %d but found %d", expected, actual);
66 }
67 }
68 }
69
TF_LITE_MICRO_TEST(TestMicroFeaturesGeneratorNo)70 TF_LITE_MICRO_TEST(TestMicroFeaturesGeneratorNo) {
71 tflite::MicroErrorReporter micro_error_reporter;
72
73 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk,
74 InitializeMicroFeatures(µ_error_reporter));
75 // As we did for the previous features, set known good noise state
76 // parameters.
77 const uint32_t no_estimate_presets[] = {
78 2563964, 1909393, 559801, 538670, 203643, 175959, 75088, 139491,
79 59691, 95307, 43865, 129263, 52517, 80058, 51330, 100731,
80 76674, 76262, 15497, 22598, 13778, 21460, 8946, 17806,
81 10023, 18810, 8002, 10842, 7578, 9983, 6267, 10759,
82 8946, 18488, 9691, 39785, 9939, 17835, 9671, 18512,
83 };
84 SetMicroFeaturesNoiseEstimates(no_estimate_presets);
85
86 int8_t no_calculated_data[g_no_feature_data_slice_size];
87 size_t num_samples_read;
88 TfLiteStatus no_status = GenerateMicroFeatures(
89 µ_error_reporter, g_no_30ms_sample_data, g_no_30ms_sample_data_size,
90 g_no_feature_data_slice_size, no_calculated_data, &num_samples_read);
91 TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, no_status);
92
93 for (size_t i = 0; i < g_no_feature_data_slice_size; ++i) {
94 const int expected = g_no_feature_data_slice[i];
95 const int actual = no_calculated_data[i];
96 TF_LITE_MICRO_EXPECT_EQ(expected, actual);
97 if (expected != actual) {
98 TF_LITE_REPORT_ERROR(µ_error_reporter,
99 "Expected value %d but found %d", expected, actual);
100 }
101 }
102 }
103
104 TF_LITE_MICRO_TESTS_END
105