Not-throwing constructor.
35 tensorflow::SessionOptions options;
36 tensorflow::ConfigProto& config = options.config;
37 config.set_inter_op_parallelism_threads(1);
38 config.set_intra_op_parallelism_threads(1);
39 config.set_use_per_session_threads(
false);
41 auto status = tensorflow::NewSession(options, &
fSession);
43 std::cout << status.ToString() << std::endl;
49 fBundle =
new tensorflow::SavedModelBundle();
50 status = tensorflow::LoadSavedModel(tensorflow::SessionOptions(),
51 tensorflow::RunOptions(),
53 {tensorflow::kSavedModelTagServe},
55 std::cout <<
"tf_graph loaded SavedModelBundle with status: " << status.ToString() << std::endl;
56 if (!status.ok())
return;
58 auto sig_map =
fBundle->meta_graph_def.signature_def();
59 std::string sig_def =
"serving_default";
60 bool has_default_key =
false;
61 std::vector<std::string> sig_map_keys;
62 for (
auto const& p : sig_map) {
63 if (p.first == sig_def) has_default_key =
true;
64 sig_map_keys.push_back(p.first);
66 auto model_def = sig_map.at((has_default_key) ? sig_def : sig_map_keys.back());
69 std::cout <<
"tf_graph inputs:" << std::endl;
70 for (
auto const& p : model_def.inputs()) {
72 std::cout <<
"tf_graph InputName: " <<
fInputNames.back() << std::endl;
73 std::cout <<
" key: " << p.first <<
" value: " << p.second.name() << std::endl;
78 if (outputs.empty()) {
79 std::cout <<
"tf_graph using all outputs:" << std::endl;
80 for (
auto const& p : model_def.outputs()) {
82 std::cout <<
" key: " << p.first <<
" value: " << p.second.name() << std::endl;
87 std::cout <<
"tf_graph using selected outputs:" << std::endl;
88 for (
const auto& s : outputs) {
89 for (
auto const& p : model_def.outputs()) {
92 std::cout <<
" key: " << p.first <<
" value: " << p.second.name() << std::endl;
98 std::cout <<
"tf_graph did not find outputs in SaveModelBundle." << std::endl;
104 tensorflow::GraphDef graph_def;
106 std::cout <<
"tf_graph loaded ProtoBuf graph with status: " << status.ToString() << std::endl;
107 if (!status.ok())
return;
109 size_t ng = graph_def.node().size();
110 for (
int i = 0; i <
n_inputs; ++i) {
115 if (outputs.empty()) {
117 fOutputNames.push_back(graph_def.node()[ng - i].name());
122 std::string last, current, basename, name;
123 for (
size_t n = 0;
n < ng; ++
n) {
124 name = graph_def.node()[
n].name();
125 auto pos = name.find(
"/");
126 if (pos != std::string::npos) { basename = name.substr(0, pos); }
132 for (
const auto& s : outputs) {
133 if (name.find(s) != std::string::npos) {
139 if (!last.empty() && (basename != current)) {
fOutputNames.push_back(last); }
147 std::cout <<
"Output nodes not found in the graph." << std::endl;
150 status =
fSession->Create(graph_def);
152 std::cout << status.ToString() << std::endl;
std::vector< std::string > fOutputNames
tensorflow::SavedModelBundle * fBundle
tensorflow::Session * fSession
std::vector< std::string > fInputNames