Home | History | Annotate | Download | only in speech_commands
      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/examples/speech_commands/recognize_commands.h"
     17 
     18 #include "tensorflow/core/framework/tensor_testutil.h"
     19 #include "tensorflow/core/lib/core/status_test_util.h"
     20 #include "tensorflow/core/platform/env.h"
     21 #include "tensorflow/core/platform/test.h"
     22 
     23 namespace tensorflow {
     24 
     25 TEST(RecognizeCommandsTest, Basic) {
     26   RecognizeCommands recognize_commands({"_silence_", "a", "b"});
     27 
     28   Tensor results(DT_FLOAT, {3});
     29   test::FillValues<float>(&results, {1.0f, 0.0f, 0.0f});
     30 
     31   string found_command;
     32   float score;
     33   bool is_new_command;
     34   TF_EXPECT_OK(recognize_commands.ProcessLatestResults(
     35       results, 0, &found_command, &score, &is_new_command));
     36 }
     37 
     38 TEST(RecognizeCommandsTest, FindCommands) {
     39   RecognizeCommands recognize_commands({"_silence_", "a", "b"}, 1000, 0.2f);
     40 
     41   Tensor results(DT_FLOAT, {3});
     42 
     43   test::FillValues<float>(&results, {0.0f, 1.0f, 0.0f});
     44   bool has_found_new_command = false;
     45   string new_command;
     46   for (int i = 0; i < 10; ++i) {
     47     string found_command;
     48     float score;
     49     bool is_new_command;
     50     int64 current_time_ms = 0 + (i * 100);
     51     TF_EXPECT_OK(recognize_commands.ProcessLatestResults(
     52         results, current_time_ms, &found_command, &score, &is_new_command));
     53     if (is_new_command) {
     54       EXPECT_FALSE(has_found_new_command);
     55       has_found_new_command = true;
     56       new_command = found_command;
     57     }
     58   }
     59   EXPECT_TRUE(has_found_new_command);
     60   EXPECT_EQ("a", new_command);
     61 
     62   test::FillValues<float>(&results, {0.0f, 0.0f, 1.0f});
     63   has_found_new_command = false;
     64   new_command = "";
     65   for (int i = 0; i < 10; ++i) {
     66     string found_command;
     67     float score;
     68     bool is_new_command;
     69     int64 current_time_ms = 1000 + (i * 100);
     70     TF_EXPECT_OK(recognize_commands.ProcessLatestResults(
     71         results, current_time_ms, &found_command, &score, &is_new_command));
     72     if (is_new_command) {
     73       EXPECT_FALSE(has_found_new_command);
     74       has_found_new_command = true;
     75       new_command = found_command;
     76     }
     77   }
     78   EXPECT_TRUE(has_found_new_command);
     79   EXPECT_EQ("b", new_command);
     80 }
     81 
     82 TEST(RecognizeCommandsTest, BadInputLength) {
     83   RecognizeCommands recognize_commands({"_silence_", "a", "b"}, 1000, 0.2f);
     84 
     85   Tensor bad_results(DT_FLOAT, {2});
     86   test::FillValues<float>(&bad_results, {1.0f, 0.0f});
     87 
     88   string found_command;
     89   float score;
     90   bool is_new_command;
     91   EXPECT_FALSE(recognize_commands
     92                    .ProcessLatestResults(bad_results, 0, &found_command, &score,
     93                                          &is_new_command)
     94                    .ok());
     95 }
     96 
     97 TEST(RecognizeCommandsTest, BadInputTimes) {
     98   RecognizeCommands recognize_commands({"_silence_", "a", "b"}, 1000, 0.2f);
     99 
    100   Tensor results(DT_FLOAT, {3});
    101   test::FillValues<float>(&results, {1.0f, 0.0f, 0.0f});
    102 
    103   string found_command;
    104   float score;
    105   bool is_new_command;
    106   TF_EXPECT_OK(recognize_commands.ProcessLatestResults(
    107       results, 100, &found_command, &score, &is_new_command));
    108   EXPECT_FALSE(recognize_commands
    109                    .ProcessLatestResults(results, 0, &found_command, &score,
    110                                          &is_new_command)
    111                    .ok());
    112 }
    113 
    114 }  // namespace tensorflow
    115