• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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/cc/saved_model/reader.h"
17 
18 #include <unordered_set>
19 
20 #include "absl/memory/memory.h"
21 #include "tensorflow/cc/saved_model/constants.h"
22 #include "tensorflow/core/lib/io/path.h"
23 #include "tensorflow/core/lib/strings/str_util.h"
24 #include "tensorflow/core/lib/strings/strcat.h"
25 #include "tensorflow/core/platform/env.h"
26 #include "tensorflow/core/protobuf/saved_model.pb.h"
27 
28 namespace tensorflow {
29 namespace {
30 
ReadSavedModel(const string & export_dir,SavedModel * saved_model_proto)31 Status ReadSavedModel(const string& export_dir, SavedModel* saved_model_proto) {
32   LOG(INFO) << "Reading SavedModel from: " << export_dir;
33 
34   const string saved_model_pb_path =
35       io::JoinPath(export_dir, kSavedModelFilenamePb);
36   if (Env::Default()->FileExists(saved_model_pb_path).ok()) {
37     return ReadBinaryProto(Env::Default(), saved_model_pb_path,
38                            saved_model_proto);
39   }
40   const string saved_model_pbtxt_path =
41       io::JoinPath(export_dir, kSavedModelFilenamePbTxt);
42   if (Env::Default()->FileExists(saved_model_pbtxt_path).ok()) {
43     return ReadTextProto(Env::Default(), saved_model_pbtxt_path,
44                          saved_model_proto);
45   }
46   return Status(error::Code::NOT_FOUND,
47                 "Could not find SavedModel .pb or .pbtxt at supplied export "
48                 "directory path: " +
49                     export_dir);
50 }
51 
FindMetaGraphDef(const std::unordered_set<string> & tags,SavedModel * saved_model_proto,MetaGraphDef * meta_graph_def)52 Status FindMetaGraphDef(const std::unordered_set<string>& tags,
53                         SavedModel* saved_model_proto,
54                         MetaGraphDef* meta_graph_def) {
55   LOG(INFO) << "Reading meta graph with tags { " << absl::StrJoin(tags, " ")
56             << " }";
57   for (MetaGraphDef& graph_def : *saved_model_proto->mutable_meta_graphs()) {
58     // Get tags from the graph_def.
59     std::unordered_set<string> graph_tags;
60     for (const string& tag : graph_def.meta_info_def().tags()) {
61       graph_tags.insert(tag);
62     }
63     // Match with the set of tags provided.
64     if (graph_tags == tags) {
65       *meta_graph_def = std::move(graph_def);
66       return Status::OK();
67     }
68   }
69   return Status(
70       error::Code::NOT_FOUND,
71       strings::StrCat(
72           "Could not find meta graph def matching supplied tags: { ",
73           absl::StrJoin(tags, " "),
74           " }. To inspect available tag-sets in the SavedModel, please "
75           "use the SavedModel CLI: `saved_model_cli`"));
76 }
77 
78 }  // namespace
79 
ReadMetaGraphDefFromSavedModel(const string & export_dir,const std::unordered_set<string> & tags,MetaGraphDef * const meta_graph_def)80 Status ReadMetaGraphDefFromSavedModel(const string& export_dir,
81                                       const std::unordered_set<string>& tags,
82                                       MetaGraphDef* const meta_graph_def) {
83   SavedModel saved_model_proto;
84   TF_RETURN_IF_ERROR(ReadSavedModel(export_dir, &saved_model_proto));
85   TF_RETURN_IF_ERROR(
86       FindMetaGraphDef(tags, &saved_model_proto, meta_graph_def));
87   return Status::OK();
88 }
89 
ReadSavedModelDebugInfoIfPresent(const string & export_dir,std::unique_ptr<GraphDebugInfo> * debug_info_proto)90 Status ReadSavedModelDebugInfoIfPresent(
91     const string& export_dir,
92     std::unique_ptr<GraphDebugInfo>* debug_info_proto) {
93   LOG(INFO) << "Reading SavedModel debug info (if present) from: "
94             << export_dir;
95 
96   const string debug_info_pb_path =
97       io::JoinPath(export_dir, "debug", "saved_model_debug_info.pb");
98   if (Env::Default()->FileExists(debug_info_pb_path).ok()) {
99     GraphDebugInfo debug_info;
100     TF_RETURN_IF_ERROR(
101         ReadBinaryProto(Env::Default(), debug_info_pb_path, &debug_info));
102     *debug_info_proto =
103         absl::make_unique<GraphDebugInfo>(std::move(debug_info));
104   }
105   return Status::OK();
106 }
107 
108 }  // namespace tensorflow
109