tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
 
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 <string>
#include <vector>
 
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/lite/tools/accuracy/utils.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/util/command_line_flags.h"
 
namespace {
tensorflow::string* g_test_model_file = nullptr;
}
 
namespace tensorflow {
namespace metrics {
namespace utils {
namespace {
 
TEST(UtilsTest, GetTFLiteModelInfoReturnsCorrectly) {
  ASSERT_TRUE(g_test_model_file != nullptr);
  string test_model_file = *g_test_model_file;
  ASSERT_FALSE(test_model_file.empty());
  // Passed graph has 4 inputs : a,b,c,d and 2 outputs x,y
  //  x = a+b+c, y=b+c+d
  // Input and outputs have shape : {1,8,8,3}
  ModelInfo model_info;
  auto status = GetTFliteModelInfo(test_model_file, &model_info);
  TF_CHECK_OK(status);
  ASSERT_EQ(4, model_info.input_shapes.size());
  ASSERT_EQ(4, model_info.input_types.size());
 
  for (int i = 0; i < 4; i++) {
    const TensorShape& shape = model_info.input_shapes[i];
    DataType dataType = model_info.input_types[i];
    EXPECT_TRUE(shape.IsSameSize({1, 8, 8, 3}));
    EXPECT_EQ(DT_FLOAT, dataType);
  }
}
 
TEST(UtilsTest, GetTFliteModelInfoIncorrectFile) {
  ModelInfo model_info;
  auto status = GetTFliteModelInfo("non_existent_file", &model_info);
  EXPECT_FALSE(status.ok());
}
 
}  // namespace
}  // namespace utils
}  // namespace metrics
}  // namespace tensorflow
 
int main(int argc, char** argv) {
  g_test_model_file = new tensorflow::string();
  const std::vector<tensorflow::Flag> flag_list = {
      tensorflow::Flag("test_model_file", g_test_model_file,
                       "Path to test tflite model file."),
  };
  const bool parse_result = tensorflow::Flags::Parse(&argc, argv, flag_list);
  CHECK(parse_result) << "Required test_model_file";
  ::tensorflow::port::InitMain(argv[0], &argc, &argv);
  return RUN_ALL_TESTS();
}