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/experimental/micro/examples/micro_speech/recognize_commands.h" 17 18 #include <limits> 19 20 RecognizeCommands::RecognizeCommands(tflite::ErrorReporter* error_reporter, 21 int32_t average_window_duration_ms, 22 uint8_t detection_threshold, 23 int32_t suppression_ms, 24 int32_t minimum_count) 25 : error_reporter_(error_reporter), 26 average_window_duration_ms_(average_window_duration_ms), 27 detection_threshold_(detection_threshold), 28 suppression_ms_(suppression_ms), 29 minimum_count_(minimum_count), 30 previous_results_(error_reporter) { 31 previous_top_label_ = "silence"; 32 previous_top_label_time_ = std::numeric_limits<int32_t>::min(); 33 } 34 35 TfLiteStatus RecognizeCommands::ProcessLatestResults( 36 const TfLiteTensor* latest_results, const int32_t current_time_ms, 37 const char** found_command, uint8_t* score, bool* is_new_command) { 38 if ((latest_results->dims->size != 2) || 39 (latest_results->dims->data[0] != 1) || 40 (latest_results->dims->data[1] != kCategoryCount)) { 41 error_reporter_->Report( 42 "The results for recognition should contain %d elements, but there are " 43 "%d in an %d-dimensional shape", 44 kCategoryCount, latest_results->dims->data[1], 45 latest_results->dims->size); 46 return kTfLiteError; 47 } 48 49 if (latest_results->type != kTfLiteUInt8) { 50 error_reporter_->Report( 51 "The results for recognition should be uint8 elements, but are %d", 52 latest_results->type); 53 return kTfLiteError; 54 } 55 56 if ((!previous_results_.empty()) && 57 (current_time_ms < previous_results_.front().time_)) { 58 error_reporter_->Report( 59 "Results must be fed in increasing time order, but received a " 60 "timestamp of %d that was earlier than the previous one of %d", 61 current_time_ms, previous_results_.front().time_); 62 return kTfLiteError; 63 } 64 65 // Add the latest results to the head of the queue. 66 previous_results_.push_back({current_time_ms, latest_results->data.uint8}); 67 68 // Prune any earlier results that are too old for the averaging window. 69 const int64_t time_limit = current_time_ms - average_window_duration_ms_; 70 while ((!previous_results_.empty()) && 71 previous_results_.front().time_ < time_limit) { 72 previous_results_.pop_front(); 73 } 74 75 // If there are too few results, assume the result will be unreliable and 76 // bail. 77 const int64_t how_many_results = previous_results_.size(); 78 const int64_t earliest_time = previous_results_.front().time_; 79 const int64_t samples_duration = current_time_ms - earliest_time; 80 if ((how_many_results < minimum_count_) || 81 (samples_duration < (average_window_duration_ms_ / 4))) { 82 *found_command = previous_top_label_; 83 *score = 0; 84 *is_new_command = false; 85 return kTfLiteOk; 86 } 87 88 // Calculate the average score across all the results in the window. 89 int32_t average_scores[kCategoryCount]; 90 for (int offset = 0; offset < previous_results_.size(); ++offset) { 91 PreviousResultsQueue::Result previous_result = 92 previous_results_.from_front(offset); 93 const uint8_t* scores = previous_result.scores_; 94 for (int i = 0; i < kCategoryCount; ++i) { 95 if (offset == 0) { 96 average_scores[i] = scores[i]; 97 } else { 98 average_scores[i] += scores[i]; 99 } 100 } 101 } 102 for (int i = 0; i < kCategoryCount; ++i) { 103 average_scores[i] /= how_many_results; 104 } 105 106 // Find the current highest scoring category. 107 int current_top_index = 0; 108 int32_t current_top_score = 0; 109 for (int i = 0; i < kCategoryCount; ++i) { 110 if (average_scores[i] > current_top_score) { 111 current_top_score = average_scores[i]; 112 current_top_index = i; 113 } 114 } 115 const char* current_top_label = kCategoryLabels[current_top_index]; 116 117 // If we've recently had another label trigger, assume one that occurs too 118 // soon afterwards is a bad result. 119 int64_t time_since_last_top; 120 if ((previous_top_label_ == kCategoryLabels[0]) || 121 (previous_top_label_time_ == std::numeric_limits<int32_t>::min())) { 122 time_since_last_top = std::numeric_limits<int32_t>::max(); 123 } else { 124 time_since_last_top = current_time_ms - previous_top_label_time_; 125 } 126 if ((current_top_score > detection_threshold_) && 127 (current_top_label != previous_top_label_) && 128 (time_since_last_top > suppression_ms_)) { 129 previous_top_label_ = current_top_label; 130 previous_top_label_time_ = current_time_ms; 131 *is_new_command = true; 132 } else { 133 *is_new_command = false; 134 } 135 *found_command = current_top_label; 136 *score = current_top_score; 137 138 return kTfLiteOk; 139 } 140