Skip to content

Building: TensorFlow Support

Christopher Canel edited this page Sep 5, 2018 · 1 revision

1. Compile libtensorflow_cc.so.

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
./configure
bazel build --config=monolithic --config=opt //tensorflow:libtensorflow_cc.so
# If you want fast, use this command instead
# Note that in some cases, using MKL will cause significantly worse performance.
bazel build --config=monolithic --config=mkl --copt=-mfma --copt=-mavx --copt=-mavx2 --copt=-msse4.1 --copt=-msse4.2 //tensorflow:libtensorflow_cc.so

2. Configure SAF with the following cmake flags. Note that Caffe and TensorFlow can both be enabled at the same time (not shown here).

cmake -DUSE_TENSORFLOW=yes -DTENSORFLOW_HOME=</path/to/tensorflow> ..

Optional Step 1. Re-Generate TensorFlow Protobufs.

If your installed version of libprotobuf does not match the version used by the TensorFlow build system, then you need to manually regenerate all of the protobuf header/source pairs or upgrade your installed version of Protobuf to match what TensorFlow is currently using. The following script recompiles the TensorFlow Protobuf files.

protofiles=`find tensorflow -type f -name "*.proto"`

rm -rf bazel-genfiles/tensorflow/

for protofile in ${protofiles[@]}; do
    echo $protofile
    protoc $protofile  --cpp_out=./bazel-genfiles/
done

Optional Step 2. Build the TensorFlow pip package (to compile the TensorFlow protobufs)

This step is not necessary for viscloud/master, but is necessary if you want to use any TensorFlow functionality not included in libtensorflow_cc.so (Note: if you are forking viscloud/master and run into linker issues in this case, please contact Thomas-Kim)

bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

Note: If you have issues with opencv and/or libjpeg, see the following github issues:

https://github.com/tensorflow/tensorflow/issues/14267#issuecomment-351780041 https://github.com/tensorflow/tensorflow/issues/1924

Note: The following TensorFlow tags are verified to work. The build process for newer commits may vary.
  • r1.2
  • r1.3
  • r1.5 with bazel 0.8.0 (9/10 Dentists recommend this version)
  • Commit hash: 3438981ca7b659e57fb1e15152a1f9fd99b5d6bc or later
Note: SAF only supports models in binary proto format with weights "frozen" in.

To convert a model proto + checkpoint into a frozen graph:

python /path/to/tensorflow/tensorflow/python/tools/freeze_graph.py --input_meta_graph /path/to/model.ckpt.meta --input_checkpoint /path/to/model.ckpt --output_node_names "output node names" --output_graph /path/to/output/graph.pb --input_binary <true/false>