1 /* Copyright 2016 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 /// SavedModel loading functions and SavedModelBundle struct. 17 18 #ifndef TENSORFLOW_CC_SAVED_MODEL_LOADER_H_ 19 #define TENSORFLOW_CC_SAVED_MODEL_LOADER_H_ 20 21 #include <string> 22 #include <unordered_set> 23 24 #include "tensorflow/core/lib/core/status.h" 25 #include "tensorflow/core/protobuf/meta_graph.pb.h" 26 #include "tensorflow/core/public/session.h" 27 28 namespace tensorflow { 29 30 /// SavedModel representation once the SavedModel is loaded from storage. 31 struct SavedModelBundle { 32 std::unique_ptr<Session> session; 33 MetaGraphDef meta_graph_def; 34 35 /// A TensorFlow Session does not Close itself on destruction. To avoid 36 /// resource leaks, we explicitly call Close on Sessions that we create. ~SavedModelBundleSavedModelBundle37 ~SavedModelBundle() { 38 if (session) { 39 session->Close().IgnoreError(); 40 } 41 } 42 43 SavedModelBundle() = default; 44 }; 45 46 /// Loads a SavedModel from the specified export directory. The meta graph def 47 /// to be loaded is identified by the supplied tags, corresponding exactly to 48 /// the set of tags used at SavedModel build time. Returns a SavedModel bundle 49 /// with a session and the requested meta graph def, if found. 50 Status LoadSavedModel(const SessionOptions& session_options, 51 const RunOptions& run_options, const string& export_dir, 52 const std::unordered_set<string>& tags, 53 SavedModelBundle* const bundle); 54 55 /// Checks whether the provided directory could contain a SavedModel. Note that 56 /// the method does not load any data by itself. If the method returns `false`, 57 /// the export directory definitely does not contain a SavedModel. If the method 58 /// returns `true`, the export directory may contain a SavedModel but provides 59 /// no guarantee that it can be loaded. 60 bool MaybeSavedModelDirectory(const string& export_dir); 61 62 } // namespace tensorflow 63 64 #endif // TENSORFLOW_CC_SAVED_MODEL_LOADER_H_ 65