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 | |
18 | #include "tensorflow/core/platform/env.h" |
19 | |
20 | namespace tensorflow { |
21 | namespace graph_transforms { |
22 | |
23 | Status LoadTextOrBinaryGraphFile(const string& file_name, GraphDef* graph_def) { |
24 | string file_data; |
25 | Status load_file_status = |
26 | ReadFileToString(Env::Default(), file_name, &file_data); |
27 | if (!load_file_status.ok()) { |
28 | errors::AppendToMessage(&load_file_status, " (for file " , file_name, ")" ); |
29 | return load_file_status; |
30 | } |
31 | // Try to load in binary format first, and then try ascii if that fails. |
32 | Status load_status = ReadBinaryProto(Env::Default(), file_name, graph_def); |
33 | if (!load_status.ok()) { |
34 | if (protobuf::TextFormat::ParseFromString(file_data, graph_def)) { |
35 | load_status = OkStatus(); |
36 | } else { |
37 | errors::AppendToMessage(&load_status, |
38 | " (both text and binary parsing failed for file " , |
39 | file_name, ")" ); |
40 | } |
41 | } |
42 | return load_status; |
43 | } |
44 | |
45 | } // namespace graph_transforms |
46 | } // namespace tensorflow |
47 | |