8static const char *
const TAG =
"micro_wake_word";
11namespace micro_wake_word {
16 " Probability cutoff: %.2f\n"
17 " Sliding window size: %d",
18 this->
wake_word_.c_str(), this->probability_cutoff_ / 255.0f, this->sliding_window_size_);
24 " Probability cutoff: %.2f\n"
25 " Sliding window size: %d",
35 ESP_LOGE(TAG,
"Could not allocate the streaming model's tensor arena.");
43 ESP_LOGE(TAG,
"Could not allocate the streaming model's variable tensor arena.");
46 this->
ma_ = tflite::MicroAllocator::Create(this->
var_arena_, STREAMING_MODEL_VARIABLE_ARENA_SIZE);
47 this->
mrv_ = tflite::MicroResourceVariables::Create(this->
ma_, 20);
50 const tflite::Model *model = tflite::GetModel(this->
model_start_);
51 if (model->version() != TFLITE_SCHEMA_VERSION) {
52 ESP_LOGE(TAG,
"Streaming model's schema is not supported");
60 if (this->
interpreter_->AllocateTensors() != kTfLiteOk) {
61 ESP_LOGE(TAG,
"Failed to allocate tensors for the streaming model");
68 if ((input->dims->size != 3) || (input->dims->data[0] != 1) ||
69 (input->dims->data[2] != PREPROCESSOR_FEATURE_SIZE)) {
70 ESP_LOGE(TAG,
"Streaming model tensor input dimensions has improper dimensions.");
74 if (input->type != kTfLiteInt8) {
75 ESP_LOGE(TAG,
"Streaming model tensor input is not int8.");
81 if ((output->dims->size != 2) || (output->dims->data[0] != 1) || (output->dims->data[1] != 1)) {
82 ESP_LOGE(TAG,
"Streaming model tensor output dimension is not 1x1.");
85 if (output->type != kTfLiteUInt8) {
86 ESP_LOGE(TAG,
"Streaming model tensor output is not uint8.");
131 uint8_t stride = this->
interpreter_->input(0)->dims->data[1];
135 (int8_t *) (tflite::GetTensorData<int8_t>(input)) + PREPROCESSOR_FEATURE_SIZE * this->
current_stride_step_,
136 features, PREPROCESSOR_FEATURE_SIZE);
139 if (this->current_stride_step_ >= stride) {
140 TfLiteStatus invoke_status = this->
interpreter_->Invoke();
141 if (invoke_status != kTfLiteOk) {
142 ESP_LOGW(TAG,
"Streaming interpreter invoke failed");
171 size_t sliding_window_average_size,
const std::string &wake_word,
size_t tensor_arena_size,
172 bool default_enabled,
bool internal_only) {
218 return detection_event;
231 return detection_event;
234VADModel::VADModel(
const uint8_t *model_start, uint8_t default_probability_cutoff,
size_t sliding_window_size,
235 size_t tensor_arena_size) {
253 return detection_event;
265 return detection_event;
269 if (op_resolver.AddCallOnce() != kTfLiteOk)
271 if (op_resolver.AddVarHandle() != kTfLiteOk)
273 if (op_resolver.AddReshape() != kTfLiteOk)
275 if (op_resolver.AddReadVariable() != kTfLiteOk)
277 if (op_resolver.AddStridedSlice() != kTfLiteOk)
279 if (op_resolver.AddConcatenation() != kTfLiteOk)
281 if (op_resolver.AddAssignVariable() != kTfLiteOk)
283 if (op_resolver.AddConv2D() != kTfLiteOk)
285 if (op_resolver.AddMul() != kTfLiteOk)
287 if (op_resolver.AddAdd() != kTfLiteOk)
289 if (op_resolver.AddMean() != kTfLiteOk)
291 if (op_resolver.AddFullyConnected() != kTfLiteOk)
293 if (op_resolver.AddLogistic() != kTfLiteOk)
295 if (op_resolver.AddQuantize() != kTfLiteOk)
297 if (op_resolver.AddDepthwiseConv2D() != kTfLiteOk)
299 if (op_resolver.AddAveragePool2D() != kTfLiteOk)
301 if (op_resolver.AddMaxPool2D() != kTfLiteOk)
303 if (op_resolver.AddPad() != kTfLiteOk)
305 if (op_resolver.AddPack() != kTfLiteOk)
307 if (op_resolver.AddSplitV() != kTfLiteOk)
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
An STL allocator that uses SPI or internal RAM.
void deallocate(T *p, size_t n)
tflite::MicroAllocator * ma_
bool load_model_()
Allocates tensor and variable arenas and sets up the model interpreter.
uint8_t current_stride_step_
size_t sliding_window_size_
std::unique_ptr< tflite::MicroInterpreter > interpreter_
uint8_t default_probability_cutoff_
tflite::MicroMutableOpResolver< 20 > streaming_op_resolver_
bool register_streaming_ops_(tflite::MicroMutableOpResolver< 20 > &op_resolver)
Returns true if successfully registered the streaming model's TensorFlow operations.
void reset_probabilities()
Sets all recent_streaming_probabilities to 0 and resets the ignore window count.
size_t tensor_arena_size_
std::vector< uint8_t > recent_streaming_probabilities_
bool unprocessed_probability_status_
tflite::MicroResourceVariables * mrv_
bool perform_streaming_inference(const int8_t features[PREPROCESSOR_FEATURE_SIZE])
uint8_t probability_cutoff_
void unload_model()
Destroys the TFLite interpreter and frees the tensor and variable arenas' memory.
const uint8_t * model_start_
DetectionEvent determine_detected() override
Checks for voice activity by comparing the max probability in the sliding window with the probability...
VADModel(const uint8_t *model_start, uint8_t default_probability_cutoff, size_t sliding_window_size, size_t tensor_arena_size)
void log_model_config() override
void enable() override
Enable the model and save to flash. The next performing_streaming_inference call will load it.
DetectionEvent determine_detected() override
Checks for the wake word by comparing the mean probability in the sliding window with the probability...
void log_model_config() override
ESPPreferenceObject pref_
WakeWordModel(const std::string &id, const uint8_t *model_start, uint8_t default_probability_cutoff, size_t sliding_window_average_size, const std::string &wake_word, size_t tensor_arena_size, bool default_enabled, bool internal_only)
Constructs a wake word model object.
void disable() override
Disable the model and save to flash. The next performing_streaming_inference call will unload it.
Providing packet encoding functions for exchanging data with a remote host.
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
ESPPreferences * global_preferences
std::unique_ptr< T > make_unique(Args &&...args)
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
uint8_t average_probability