Home | History | Annotate | Download | only in graph_transforms
      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/tools/graph_transforms/file_utils.h"
     17 #include "tensorflow/cc/ops/const_op.h"
     18 #include "tensorflow/cc/ops/image_ops.h"
     19 #include "tensorflow/cc/ops/nn_ops.h"
     20 #include "tensorflow/cc/ops/standard_ops.h"
     21 #include "tensorflow/core/framework/tensor_testutil.h"
     22 #include "tensorflow/core/lib/core/status_test_util.h"
     23 #include "tensorflow/core/lib/io/path.h"
     24 #include "tensorflow/core/platform/test.h"
     25 #include "tensorflow/core/platform/test_benchmark.h"
     26 #include "tensorflow/core/util/equal_graph_def.h"
     27 
     28 namespace tensorflow {
     29 namespace graph_transforms {
     30 
     31 class FileUtilsTest : public ::testing::Test {
     32  protected:
     33   void TestLoadTextOrBinaryGraphFile() {
     34     using namespace ::tensorflow::ops;  // NOLINT(build/namespaces)
     35     const int width = 10;
     36 
     37     auto root = tensorflow::Scope::NewRootScope();
     38     Tensor a_data(DT_FLOAT, TensorShape({width}));
     39     test::FillIota<float>(&a_data, 1.0f);
     40     Output a_const = Const(root.WithOpName("a"), Input::Initializer(a_data));
     41     GraphDef graph_def;
     42     TF_ASSERT_OK(root.ToGraphDef(&graph_def));
     43 
     44     const string text_file =
     45         io::JoinPath(testing::TmpDir(), "text_graph.pbtxt");
     46     TF_ASSERT_OK(WriteTextProto(Env::Default(), text_file, graph_def));
     47 
     48     const string binary_file =
     49         io::JoinPath(testing::TmpDir(), "binary_graph.pb");
     50     TF_ASSERT_OK(WriteBinaryProto(Env::Default(), binary_file, graph_def));
     51 
     52     const string bogus_file = io::JoinPath(testing::TmpDir(), "bogus_graph.pb");
     53     TF_ASSERT_OK(
     54         WriteStringToFile(Env::Default(), bogus_file, "Not a !{ proto..."));
     55 
     56     GraphDef text_graph_def;
     57     TF_EXPECT_OK(LoadTextOrBinaryGraphFile(text_file, &text_graph_def));
     58     string text_diff;
     59     EXPECT_TRUE(EqualGraphDef(text_graph_def, graph_def, &text_diff))
     60         << text_diff;
     61 
     62     GraphDef binary_graph_def;
     63     TF_EXPECT_OK(LoadTextOrBinaryGraphFile(binary_file, &binary_graph_def));
     64     string binary_diff;
     65     EXPECT_TRUE(EqualGraphDef(binary_graph_def, graph_def, &binary_diff))
     66         << binary_diff;
     67 
     68     GraphDef no_graph_def;
     69     EXPECT_FALSE(
     70         LoadTextOrBinaryGraphFile("____non_existent_file_____", &no_graph_def)
     71             .ok());
     72 
     73     GraphDef bogus_graph_def;
     74     EXPECT_FALSE(LoadTextOrBinaryGraphFile(bogus_file, &bogus_graph_def).ok());
     75   }
     76 };
     77 
     78 TEST_F(FileUtilsTest, TestLoadTextOrBinaryGraphFile) {
     79   TestLoadTextOrBinaryGraphFile();
     80 }
     81 
     82 }  // namespace graph_transforms
     83 }  // namespace tensorflow
     84