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/accuracy_utils.h"
17
18 #include "tensorflow/core/framework/tensor_testutil.h"
19 #include "tensorflow/core/lib/core/status_test_util.h"
20 #include "tensorflow/core/lib/io/path.h"
21 #include "tensorflow/core/lib/strings/str_util.h"
22 #include "tensorflow/core/platform/env.h"
23 #include "tensorflow/core/platform/test.h"
24
25 namespace tensorflow {
26
TEST(AccuracyUtilsTest,ReadGroundTruthFile)27 TEST(AccuracyUtilsTest, ReadGroundTruthFile) {
28 string file_name = tensorflow::io::JoinPath(tensorflow::testing::TmpDir(),
29 "ground_truth.txt");
30 string file_data = "a,10\nb,12\n";
31 TF_ASSERT_OK(WriteStringToFile(Env::Default(), file_name, file_data));
32
33 std::vector<std::pair<string, int64>> ground_truth;
34 TF_ASSERT_OK(ReadGroundTruthFile(file_name, &ground_truth));
35 ASSERT_EQ(2, ground_truth.size());
36 EXPECT_EQ("a", ground_truth[0].first);
37 EXPECT_EQ(10, ground_truth[0].second);
38 EXPECT_EQ("b", ground_truth[1].first);
39 EXPECT_EQ(12, ground_truth[1].second);
40 }
41
TEST(AccuracyUtilsTest,CalculateAccuracyStats)42 TEST(AccuracyUtilsTest, CalculateAccuracyStats) {
43 StreamingAccuracyStats stats;
44 CalculateAccuracyStats({{"a", 1000}, {"b", 9000}},
45 {{"a", 1200}, {"b", 5000}, {"a", 8700}}, 10000, 500,
46 &stats);
47 EXPECT_EQ(2, stats.how_many_ground_truth_words);
48 EXPECT_EQ(2, stats.how_many_ground_truth_matched);
49 EXPECT_EQ(1, stats.how_many_false_positives);
50 EXPECT_EQ(1, stats.how_many_correct_words);
51 EXPECT_EQ(1, stats.how_many_wrong_words);
52 }
53
TEST(AccuracyUtilsTest,PrintAccuracyStats)54 TEST(AccuracyUtilsTest, PrintAccuracyStats) {
55 StreamingAccuracyStats stats;
56 PrintAccuracyStats(stats);
57 }
58
59 } // namespace tensorflow
60