-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicro_speech_pico.ino
227 lines (198 loc) · 8.05 KB
/
micro_speech_pico.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
Modified by IzK, June 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <TensorFlowLiteMicro.h>
// Disable profiling the code execution
#undef PROFILE_MICRO_SPEECH
// Disable reduced/striped error messages
#undef TF_LITE_STRIP_ERROR_STRINGS
#include "main_functions.h"
#include "audio_provider.h"
#include "command_responder.h"
#include "feature_provider.h"
#include "micro_model_settings.h"
#include "micro_speech_quantized_model_data.h"
#include "recognize_commands.h"
#include "tensorflow/lite/core/c/common.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_log.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/schema/schema_generated.h"
// Globals, used for compatibility with Arduino-style sketches.
namespace {
const tflite::Model* model = nullptr;
tflite::MicroInterpreter* interpreter = nullptr;
TfLiteTensor* model_input = nullptr;
FeatureProvider* feature_provider = nullptr;
RecognizeCommands* recognizer = nullptr;
int32_t previous_time = 0;
// Create an area of memory to use for input, output, and intermediate arrays.
// The size of this will depend on the model you're using, and may need to be
// determined by experimentation.
constexpr int kTensorArenaSize = 30 * 1024;
// Keep aligned to 16 bytes for CMSIS
alignas(16) uint8_t tensor_arena[kTensorArenaSize];
int8_t feature_buffer[kFeatureElementCount];
int8_t* model_input_buffer = nullptr;
using MicroAudioOpResolver = tflite::MicroMutableOpResolver<4>;
} // namespace
// Pull in only the operation implementations we need.
// This relies on a complete list of all the ops needed by this graph.
TfLiteStatus RegisterOps(MicroAudioOpResolver& micro_op_resolver) {
TF_LITE_ENSURE_STATUS(micro_op_resolver.AddDepthwiseConv2D());
TF_LITE_ENSURE_STATUS(micro_op_resolver.AddFullyConnected());
TF_LITE_ENSURE_STATUS(micro_op_resolver.AddSoftmax());
TF_LITE_ENSURE_STATUS(micro_op_resolver.AddReshape());
return kTfLiteOk;
}
// An easier approach is to just use the AllOpsResolver, but this will
// incur some penalty in code space for op implementations that are not
// needed by this graph.
// tflite::AllOpsResolver resolver;
// The name of this function is important for Arduino compatibility.
void setup() {
// IzK: This is defined in "tensorflow/lite/micro/system_setup.cpp" and simply initializes the Serial port
tflite::InitializeTarget(115200);
// Map the model into a usable data structure. This doesn't involve any
// copying or parsing, it's a very lightweight operation.
model = tflite::GetModel(g_micro_speech_quantized_model_data);
if (model->version() != TFLITE_SCHEMA_VERSION) {
MicroPrintf(
"Model provided is schema version %d not equal "
"to supported version %d.",
model->version(), TFLITE_SCHEMA_VERSION);
return;
}
//else{
// MicroPrintf("Model schema version: %d", model->version());
//}
static MicroAudioOpResolver micro_op_resolver;
RegisterOps(micro_op_resolver);
// Build an interpreter to run the model with.
static tflite::MicroInterpreter static_interpreter(
model, micro_op_resolver, tensor_arena, kTensorArenaSize);
interpreter = &static_interpreter;
// Allocate memory from the tensor_arena for the model's tensors.
TfLiteStatus allocate_status = interpreter->AllocateTensors();
if (allocate_status != kTfLiteOk) {
MicroPrintf("AllocateTensors() failed");
return;
}
// Get information about the memory area to use for the model's input.
model_input = interpreter->input(0);
if ((model_input->dims->size != 2) || (model_input->dims->data[0] != 1) ||
(model_input->dims->data[1] !=
(kFeatureSliceCount * kFeatureSliceSize)) ||
(model_input->type != kTfLiteInt8)) {
MicroPrintf("Bad input tensor parameters in model");
return;
}
model_input_buffer = model_input->data.int8;
// Prepare to access the audio spectrograms from a microphone or other source
// that will provide the inputs to the neural network.
// NOLINTNEXTLINE(runtime-global-variables)
static FeatureProvider static_feature_provider(kFeatureElementCount,
feature_buffer);
feature_provider = &static_feature_provider;
static RecognizeCommands static_recognizer;
recognizer = &static_recognizer;
previous_time = 0;
// Set everything up to start receiving audio
TfLiteStatus init_status = InitAudioRecording();
if (init_status != kTfLiteOk) {
MicroPrintf("InitAudioRecording failed");
return;
}
MicroPrintf("Initialization complete");
}
// The name of this function is important for Arduino compatibility.
void loop() {
#ifdef PROFILE_MICRO_SPEECH
const uint32_t prof_start = millis();
static uint32_t prof_count = 0;
static uint32_t prof_sum = 0;
static uint32_t prof_min = std::numeric_limits<uint32_t>::max();
static uint32_t prof_max = 0;
#endif // PROFILE_MICRO_SPEECH
// Fetch the spectrogram for the current time.
const int32_t current_time = LatestAudioTimestamp();
int how_many_new_slices = 0;
TfLiteStatus feature_status = feature_provider->PopulateFeatureData(
previous_time, current_time, &how_many_new_slices);
if (feature_status != kTfLiteOk) {
MicroPrintf("Feature generation failed");
//return;
}
//else {
// MicroPrintf("Feature generation succeded");
//}
previous_time += how_many_new_slices * kFeatureSliceStrideMs;
// If no new audio samples have been received since last time, don't bother
// running the network model.
if (how_many_new_slices == 0) {
MicroPrintf("No new audio samples received");
//return;
}
//else {
// MicroPrintf("%d audio samples received", how_many_new_slices);
//}
// Copy feature buffer to input tensor
for (int i = 0; i < kFeatureElementCount; i++) {
model_input_buffer[i] = feature_buffer[i];
}
// Run the model on the spectrogram input and make sure it succeeds.
TfLiteStatus invoke_status = interpreter->Invoke();
if (invoke_status != kTfLiteOk) {
MicroPrintf("Invoke failed");
//return;
}
//else {
// MicroPrintf("Invoke succeded");
//}
// Obtain a pointer to the output tensor
TfLiteTensor* output = interpreter->output(0);
// Determine whether a command was recognized based on the output of inference
const char* found_command = nullptr;
uint8_t score = 0;
bool is_new_command = false;
TfLiteStatus process_status = recognizer->ProcessLatestResults(
output, current_time, &found_command, &score, &is_new_command);
if (process_status != kTfLiteOk) {
MicroPrintf("RecognizeCommands::ProcessLatestResults() failed");
//return;
}
//else {
// MicroPrintf("RecognizeCommands::ProcessLatestResults() succeded: %d", is_new_command);
//}
// Do something based on the recognized command. The default implementation
// just prints to the error console, but you should replace this with your
// own function for a real application.
RespondToCommand(current_time, found_command, score, is_new_command);
#ifdef PROFILE_MICRO_SPEECH
const uint32_t prof_end = millis();
if (++prof_count > 10) {
uint32_t elapsed = prof_end - prof_start;
prof_sum += elapsed;
if (elapsed < prof_min) {
prof_min = elapsed;
}
if (elapsed > prof_max) {
prof_max = elapsed;
}
if (prof_count % 300 == 0) {
MicroPrintf("## time: min %dms max %dms avg %dms", prof_min, prof_max,
prof_sum / prof_count);
}
}
#endif // PROFILE_MICRO_SPEECH
}