Home » Android » android – Add weights to .pb file exported by TensorFlow

android – Add weights to .pb file exported by TensorFlow

Posted by: admin May 14, 2020 Leave a comment

Questions:

My project uses Python to train a MLP on TensorFlow and then I export the graph and the weights in that way:

tf.train.write_graph(sess.graph_def, "./", "inp.txt", True) 
saver.save(sess, 'variables/model.ckpt', global_step=1)

Now, although it is fine to use both files to import it back to Python it seems impossible to use it for Android or C++ since it cannot inport the checkpoint .ckpt.

Right now, I’m using the script freeze_graph.py provided by google to join both files into one by doing:

bazel-bin/tensorflow/python/tools/freeze_graph --input_graph=inp.txt --input_checkpoint=variables/model.ckpt-1 --output_graph=newoutput.pb --output_node_names=output

My question is, is there a way to use another function instead of tf.train.write_graph to export it with the weights included?

How to&Answers:

At the moment I’m sorry to tell there is no way.

As discussed previously on Github (have a look), TensorFlow team does not currently address this problem.

Can’t say anything for now.

pltrdy

Answer:

Currently there is no way to do that, unfortunately.

It would be really good to have a way to do that directly. I mean, it would be great to have something to do it a single shot way instead of having to generate two files and then run another script to convert those.

It is specially bad for those who use hybrid graphics on a laptop.

Anyways, as TensorFlow group said:

We do not have plans to support outputting .pb files directly. If you
are worried about too many checkpoint files taking up space, you can
limit the max_to_keep to 1.

Answer:

Currently, freezegraph is the only way to address this issue.

For me it doesn’t work quite well because I have to install it on many computers daily since people keep messsing things up and freezegraph forces me to install it from source.

They have to create another way to do it. A built-in way. Specially if they want us to use to for android.