Skip to content

Commit

Permalink
add run model with multi input layers
Browse files Browse the repository at this point in the history
  • Loading branch information
rangsimanketkaew committed Oct 26, 2022
1 parent 5077478 commit 1449a23
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ meaning that it lacks information and a tutorial about how to use TensorFlow API
2. [TensorFlow Serving](./tensorflow_serving.md)
3. [Tutorial: Introduction to TensorFlow Tensor](./tensorflow_tensor.md)
4. [Tutorial: load saved model](./load_model_tensorflow_cpp.md)
5. [Code: load and run model with multiple input layer](./run_model_multi_input.cpp)
99 changes: 99 additions & 0 deletions run_model_multi_input.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Load and run a saved TensorFlow's multiple-input model
g++ -Wall -fPIC -D_GLIBCXX_USE_CXX11_ABI=1 run_model.cpp -o run_model.o \
-I/usr/local/tensorflow-2.11/include/ -L/usr/local/tensorflow-2.11/lib \
-ltensorflow_cc -ltensorflow_framework
Rangsiman Ketkaew
*/

#include <tensorflow/cc/saved_model/loader.h>
#include <tensorflow/cc/saved_model/tag_constants.h>

using namespace tensorflow;

int main()
{

// load the whole folder
std::string export_dir = "./model/";

// Load
SavedModelBundle model_bundle;
SessionOptions session_options = SessionOptions();
RunOptions run_options = RunOptions();
Status status = LoadSavedModel(session_options, run_options, export_dir, {kSavedModelTagServe}, &model_bundle);

if (!status.ok())
{
std::cerr << "Failed: " << status;
}

auto sig_map = model_bundle.GetSignatures();
auto model_def = sig_map.at("serving_default");

for (auto const &p : sig_map)
{
std::cout << "key: " << p.first.c_str() << std::endl;
}
for (auto const &p : model_def.inputs())
{
std::cout << "key: " << p.first.c_str() << " " << p.second.name().c_str() << std::endl;
}
for (auto const &p : model_def.outputs())
{
std::cout << "key: " << p.first.c_str() << " " << p.second.name().c_str() << std::endl;
}

auto input_tensor_1_name = "serving_default_args_0:0";
auto input_tensor_2_name = "serving_default_args_0_1:0";
auto output_tensor_1_name = "StatefulPartitionedCall:0";
auto output_tensor_2_name = "StatefulPartitionedCall:1";

// Create output placeholder tensors
// I use the size of input that exactly matchs the size of input from Python model
// Use saved_model_cli to check
tensorflow::Tensor input_1(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 394}));
tensorflow::Tensor input_2(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 99}));

auto input_mat_1 = input_1.matrix<float>();
// assign random value
for (unsigned i = 0; i < 394; ++i)
input_mat_1(0, i) = 1.0;
auto input_mat_2 = input_2.matrix<float>();
// assign random value
for (unsigned i = 0; i < 99; ++i)
input_mat_2(0, i) = 2.0;

typedef std::vector<std::pair<std::string, tensorflow::Tensor>> tensor_dict;
tensor_dict feed_dict = {
{input_tensor_1_name, input_1},
{input_tensor_2_name, input_2}};

// Create output placeholder tensors for results
std::vector<tensorflow::Tensor> outputs;
std::vector<std::string> output_names = {output_tensor_1_name, output_tensor_2_name};
// Running inference
tensorflow::Status status_run = model_bundle.session->Run(feed_dict,
output_names,
{},
&outputs);
// Check if session is successfully loaded
if (!status_run.ok())
{
std::cerr << "Failed: " << status_run;
}
else
{
std::cout << "Passed: " << status_run << std::endl;
}

std::cout << "input 1 " << input_1.DebugString() << std::endl;
std::cout << "input 2 " << input_2.DebugString() << std::endl;
std::cout << "output " << outputs[0].DebugString() << std::endl;
std::cout << "dense/kernel:0 " << outputs[1].DebugString() << std::endl;
// std::cout << "dense/bias:0 " << outputs[2].DebugString() << std::endl;

return 0;
}
4 changes: 4 additions & 0 deletions session.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
Test TensorFlow sesssion
*/

#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/public/session.h>

Expand Down

0 comments on commit 1449a23

Please sign in to comment.