1 /* Copyright 2017 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/recognize_commands.h"
17 
18 #include "tensorflow/lite/micro/test_helpers.h"
19 #include "tensorflow/lite/micro/testing/micro_test.h"
20 
21 TF_LITE_MICRO_TESTS_BEGIN
22 
TF_LITE_MICRO_TEST(PreviousResultsQueueBasic)23 TF_LITE_MICRO_TEST(PreviousResultsQueueBasic) {
24   tflite::MicroErrorReporter micro_error_reporter;
25 
26   PreviousResultsQueue queue(&micro_error_reporter);
27   TF_LITE_MICRO_EXPECT_EQ(0, queue.size());
28 
29   int8_t scores_a[4] = {0, 0, 0, 1};
30   queue.push_back({0, scores_a});
31   TF_LITE_MICRO_EXPECT_EQ(1, queue.size());
32   TF_LITE_MICRO_EXPECT_EQ(0, queue.front().time_);
33   TF_LITE_MICRO_EXPECT_EQ(0, queue.back().time_);
34 
35   int8_t scores_b[4] = {0, 0, 1, 0};
36   queue.push_back({1, scores_b});
37   TF_LITE_MICRO_EXPECT_EQ(2, queue.size());
38   TF_LITE_MICRO_EXPECT_EQ(0, queue.front().time_);
39   TF_LITE_MICRO_EXPECT_EQ(1, queue.back().time_);
40 
41   PreviousResultsQueue::Result pop_result = queue.pop_front();
42   TF_LITE_MICRO_EXPECT_EQ(0, pop_result.time_);
43   TF_LITE_MICRO_EXPECT_EQ(1, queue.size());
44   TF_LITE_MICRO_EXPECT_EQ(1, queue.front().time_);
45   TF_LITE_MICRO_EXPECT_EQ(1, queue.back().time_);
46 
47   int8_t scores_c[4] = {0, 1, 0, 0};
48   queue.push_back({2, scores_c});
49   TF_LITE_MICRO_EXPECT_EQ(2, queue.size());
50   TF_LITE_MICRO_EXPECT_EQ(1, queue.front().time_);
51   TF_LITE_MICRO_EXPECT_EQ(2, queue.back().time_);
52 }
53 
TF_LITE_MICRO_TEST(PreviousResultsQueuePushPop)54 TF_LITE_MICRO_TEST(PreviousResultsQueuePushPop) {
55   tflite::MicroErrorReporter micro_error_reporter;
56 
57   PreviousResultsQueue queue(&micro_error_reporter);
58   TF_LITE_MICRO_EXPECT_EQ(0, queue.size());
59 
60   for (int i = 0; i < 123; ++i) {
61     int8_t scores[4] = {0, 0, 0, 1};
62     queue.push_back({i, scores});
63     TF_LITE_MICRO_EXPECT_EQ(1, queue.size());
64     TF_LITE_MICRO_EXPECT_EQ(i, queue.front().time_);
65     TF_LITE_MICRO_EXPECT_EQ(i, queue.back().time_);
66 
67     PreviousResultsQueue::Result pop_result = queue.pop_front();
68     TF_LITE_MICRO_EXPECT_EQ(i, pop_result.time_);
69     TF_LITE_MICRO_EXPECT_EQ(0, queue.size());
70   }
71 }
72 
TF_LITE_MICRO_TEST(RecognizeCommandsTestBasic)73 TF_LITE_MICRO_TEST(RecognizeCommandsTestBasic) {
74   tflite::MicroErrorReporter micro_error_reporter;
75 
76   RecognizeCommands recognize_commands(&micro_error_reporter);
77 
78   const int8_t result_data[] = {127, -128, -128, -128};
79   const int result_dims[] = {2, 1, 4};
80   TfLiteTensor results = tflite::testing::CreateQuantizedTensor(
81       result_data, tflite::testing::IntArrayFromInts(result_dims), -128.0f,
82       127.0f);
83 
84   const char* found_command;
85   uint8_t score;
86   bool is_new_command;
87   TF_LITE_MICRO_EXPECT_EQ(
88       kTfLiteOk, recognize_commands.ProcessLatestResults(
89                      &results, 0, &found_command, &score, &is_new_command));
90 }
91 
TF_LITE_MICRO_TEST(RecognizeCommandsTestFindCommands)92 TF_LITE_MICRO_TEST(RecognizeCommandsTestFindCommands) {
93   tflite::MicroErrorReporter micro_error_reporter;
94 
95   RecognizeCommands recognize_commands(&micro_error_reporter, 1000, 51);
96 
97   const int8_t yes_data[] = {-128, -128, 127, -128};
98   const int yes_dims[] = {2, 1, 4};
99   TfLiteTensor yes_results = tflite::testing::CreateQuantizedTensor(
100       yes_data, tflite::testing::IntArrayFromInts(yes_dims), -128.0f, 127.0f);
101 
102   bool has_found_new_command = false;
103   const char* new_command;
104   for (int i = 0; i < 10; ++i) {
105     const char* found_command;
106     uint8_t score;
107     bool is_new_command;
108     int32_t current_time_ms = 0 + (i * 100);
109     TF_LITE_MICRO_EXPECT_EQ(
110         kTfLiteOk, recognize_commands.ProcessLatestResults(
111                        &yes_results, current_time_ms, &found_command, &score,
112                        &is_new_command));
113     if (is_new_command) {
114       TF_LITE_MICRO_EXPECT(!has_found_new_command);
115       has_found_new_command = true;
116       new_command = found_command;
117     }
118   }
119   TF_LITE_MICRO_EXPECT(has_found_new_command);
120   if (has_found_new_command) {
121     TF_LITE_MICRO_EXPECT_EQ(0, tflite::testing::TestStrcmp("yes", new_command));
122   }
123 
124   const int8_t no_data[] = {-128, -128, -128, 127};
125   const int no_dims[] = {2, 1, 4};
126   TfLiteTensor no_results = tflite::testing::CreateQuantizedTensor(
127       no_data, tflite::testing::IntArrayFromInts(no_dims), -128.0f, 127.0f);
128   has_found_new_command = false;
129   new_command = "";
130   uint8_t score;
131   for (int i = 0; i < 10; ++i) {
132     const char* found_command;
133     bool is_new_command;
134     int32_t current_time_ms = 1000 + (i * 100);
135     TF_LITE_MICRO_EXPECT_EQ(
136         kTfLiteOk, recognize_commands.ProcessLatestResults(
137                        &no_results, current_time_ms, &found_command, &score,
138                        &is_new_command));
139     if (is_new_command) {
140       TF_LITE_MICRO_EXPECT(!has_found_new_command);
141       has_found_new_command = true;
142       new_command = found_command;
143     }
144   }
145   TF_LITE_MICRO_EXPECT(has_found_new_command);
146   if (has_found_new_command) {
147     TF_LITE_MICRO_EXPECT_EQ(231, score);
148     TF_LITE_MICRO_EXPECT_EQ(0, tflite::testing::TestStrcmp("no", new_command));
149   }
150 }
151 
TF_LITE_MICRO_TEST(RecognizeCommandsTestBadInputLength)152 TF_LITE_MICRO_TEST(RecognizeCommandsTestBadInputLength) {
153   tflite::MicroErrorReporter micro_error_reporter;
154 
155   RecognizeCommands recognize_commands(&micro_error_reporter, 1000, 51);
156 
157   const int8_t bad_data[] = {-128, -128, 127};
158   const int bad_dims[] = {2, 1, 3};
159   TfLiteTensor bad_results = tflite::testing::CreateQuantizedTensor(
160       bad_data, tflite::testing::IntArrayFromInts(bad_dims), -128.0f, 127.0f);
161 
162   const char* found_command;
163   uint8_t score;
164   bool is_new_command;
165   TF_LITE_MICRO_EXPECT_NE(
166       kTfLiteOk, recognize_commands.ProcessLatestResults(
167                      &bad_results, 0, &found_command, &score, &is_new_command));
168 }
169 
TF_LITE_MICRO_TEST(RecognizeCommandsTestBadInputTimes)170 TF_LITE_MICRO_TEST(RecognizeCommandsTestBadInputTimes) {
171   tflite::MicroErrorReporter micro_error_reporter;
172 
173   RecognizeCommands recognize_commands(&micro_error_reporter, 1000, 51);
174 
175   const int8_t result_data[] = {-128, -128, 127, -128};
176   const int result_dims[] = {2, 1, 4};
177   TfLiteTensor results = tflite::testing::CreateQuantizedTensor(
178       result_data, tflite::testing::IntArrayFromInts(result_dims), -128.0f,
179       127.0f);
180 
181   const char* found_command;
182   uint8_t score;
183   bool is_new_command;
184   TF_LITE_MICRO_EXPECT_EQ(
185       kTfLiteOk, recognize_commands.ProcessLatestResults(
186                      &results, 100, &found_command, &score, &is_new_command));
187   TF_LITE_MICRO_EXPECT_NE(
188       kTfLiteOk, recognize_commands.ProcessLatestResults(
189                      &results, 0, &found_command, &score, &is_new_command));
190 }
191 
TF_LITE_MICRO_TEST(RecognizeCommandsTestTooFewInputs)192 TF_LITE_MICRO_TEST(RecognizeCommandsTestTooFewInputs) {
193   tflite::MicroErrorReporter micro_error_reporter;
194 
195   RecognizeCommands recognize_commands(&micro_error_reporter, 1000, 51);
196 
197   const int8_t result_data[] = {-128, -128, 127, -128};
198   const int result_dims[] = {2, 1, 4};
199   TfLiteTensor results = tflite::testing::CreateQuantizedTensor(
200       result_data, tflite::testing::IntArrayFromInts(result_dims), -128.0f,
201       127.0f);
202 
203   const char* found_command;
204   uint8_t score;
205   bool is_new_command;
206   TF_LITE_MICRO_EXPECT_EQ(
207       kTfLiteOk, recognize_commands.ProcessLatestResults(
208                      &results, 100, &found_command, &score, &is_new_command));
209   TF_LITE_MICRO_EXPECT_EQ(0, score);
210   TF_LITE_MICRO_EXPECT_EQ(false, is_new_command);
211 }
212 
213 TF_LITE_MICRO_TESTS_END
214