How to use transform_graph to optimize Tensorflow

2019-08-19 08:27发布

问题:

I used to use the optimize_for_inference library in optimizing frozen Tensorflow models. However, I have read from different sources that Tensorflow no longer supports it.

I came across transform_graph, and its documentation is found here: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/graph_transforms#strip_unused_nodes

At first, I ran into errors and found out that I need to install Tensorflow from source (https://www.tensorflow.org/install/install_sources#install_the_pip_package) instead of using PIP.

I already re-installed Tensorflow from source, and ran this code in bash (/tensorflor/tensorflow dir):

bazel build tensorflow/tools/graph_transforms:transform_graph
bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
--in_graph=tensorflow_inception_graph.pb \
--out_graph=optimized_inception_graph.pb \
--inputs='Mul' \
--outputs='softmax' \
--transforms='
  strip_unused_nodes(type=float, shape="1,299,299,3")
  fold_constants(ignore_errors=true)
  fold_batch_norms
  fold_old_batch_norms
  round_weights(num_steps=256)'

And ran to again this error:

-bash: bazel-bin/tensorflow/tools/graph_transforms/transform_graph: No such file or directory

What seems to be the problem?

回答1:

That is weird.

The code I write below is to install and using the transform_graph in CentOS7.

yum install epel-release yum update yum install patch curl https://copr.fedorainfracloud.org/coprs/vbatts/bazel/repo/epel-7/vbatts-bazel-epel-7.repo -o /etc/yum.repos.d/vbatts-bazel-epel-7.repo yum install bazel

curl -L -O https://github.com/tensorflow/tensorflow/archive/v1.8.0.tar.gz

cd tensorflow-1.8.0

./configure # interactive!

bazel build tensorflow/tools/graph_transforms:summarize_graph bazel-bin/tensorflow/tools/graph_transforms/summarize_graph

After you install the Tensorflow by source code and finish the configure, the bazel codes should be working.

The error from you environment is occurred when you didn't finish the install the Tensorflow by source code, or you ran the script in wrong path.

Please check the configure step, path of the Tensorflow root.