Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export Tensorflow graphs from Python for use in C++

Exactly how should python models be exported for use in c++?

I'm trying to do something similar to this tutorial: https://www.tensorflow.org/versions/r0.8/tutorials/image_recognition/index.html

I'm trying to import my own TF model in the c++ API in stead of the inception one. I adjusted input size and the paths, but strange errors keep popping up. I spent all day reading stack overflow and other forums but to no avail.

I've tried two methods for exporting the graph.

Method 1: metagraph.

...loading inputs, setting up the model, etc....

sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())


for i in range(num_steps):  
  x_batch, y_batch = batch(50)  
  if i%10 == 0:
        train_accuracy = accuracy.eval(feed_dict={
        x:x_batch, y_: y_batch, keep_prob: 1.0})
        print("step %d, training accuracy %g"%(i, train_accuracy))
  train_step.run(feed_dict={x: x_batch, y_: y_batch, keep_prob: 0.5})

print("test accuracy %g"%accuracy.eval(feed_dict={
    x: features_test, y_: labels_test, keep_prob: 1.0}))

saver = tf.train.Saver(tf.all_variables())
checkpoint = 
   '/home/sander/tensorflow/tensorflow/examples/cat_face/data/model.ckpt'
    saver.save(sess, checkpoint)

   tf.train.export_meta_graph(filename=
   '/home/sander/tensorflow/tensorflow/examples/cat_face/data/cat_graph.pb',  
    meta_info_def=None,
    graph_def=sess.graph_def,
    saver_def=saver.restore(sess, checkpoint),
    collection_list=None, as_text=False)

Method 1 yields the following error when trying to run the program:

[libprotobuf ERROR 
google/protobuf/src/google/protobuf/wire_format_lite.cc:532] String field 
'tensorflow.NodeDef.op' contains invalid UTF-8 data when parsing a protocol 
buffer. Use the 'bytes' type if you intend to send raw bytes. 
E tensorflow/examples/cat_face/main.cc:281] Not found: Failed to load 
compute graph at 'tensorflow/examples/cat_face/data/cat_graph.pb'

I also tried another method of exporting the graph:

Method 2: write_graph:

tf.train.write_graph(sess.graph_def, 
'/home/sander/tensorflow/tensorflow/examples/cat_face/data/', 
'cat_graph.pb', as_text=False)

This version actually seems to load something, but I get an error about variables not being initialized:

Running model failed: Failed precondition: Attempting to use uninitialized  
value weight1
[[Node: weight1/read = Identity[T=DT_FLOAT, _class=["loc:@weight1"], 
_device="/job:localhost/replica:0/task:0/cpu:0"](weight1)]]
like image 339
Sander Avatar asked Apr 17 '16 22:04

Sander


1 Answers

At first, you need to graph definition to file by using following command

with tf.Session() as sess:
//Build network here 
tf.train.write_graph(sess.graph.as_graph_def(), "C:\\output\\", "mymodel.pb")

Then, save your model by using saver

saver = tf.train.Saver(tf.global_variables()) 
saver.save(sess, "C:\\output\\mymodel.ckpt")

Then, you will have 2 files at your output, mymodel.ckpt, mymodel.pb

Download freeze_graph.py from here and run following command in C:\output\. Change output node name if it is different for you.

python freeze_graph.py --input_graph mymodel.pb --input_checkpoint mymodel.ckpt --output_node_names softmax/Reshape_1 --output_graph mymodelforc.pb

You can use mymodelforc.pb directly from C.

You can use following C code to load the proto file

#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/cc/ops/image_ops.h"

Session* session;
NewSession(SessionOptions(), &session);

GraphDef graph_def;
ReadBinaryProto(Env::Default(), "C:\\output\\mymodelforc.pb", &graph_def);

session->Create(graph_def);

Now you can use session for inference.

You can apply inference parameter as following:

// Same dimension and type as input of your network
tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({ 1, height, width, channel }));
std::vector<tensorflow::Tensor> finalOutput;

// Fill input tensor with your input data

std::string InputName = "input"; // Your input placeholder's name
std::string OutputName = "softmax/Reshape_1"; // Your output placeholder's name

session->Run({ { InputName, input_tensor } }, { OutputName }, {}, &finalOutput);

// finalOutput will contain the inference output that you search for
like image 93
Deniz Beker Avatar answered Sep 22 '22 15:09

Deniz Beker