马春杰杰 Exit Reader Mode

[mcj]使用C ++运行Tensorflow模型

如果您还在阅读,那么我假设您需要弄清楚如何在C ++的生产环境中运行Tensorflow。

2.2.1 编译tensorflow

对于我的项目,我想要一个可以链接和部署的Tensorflow共享库。这就是我们将在此项目中构建的内容,并使用它构建label_image示例。对于tensorflow c++接口来说,只能通过源码进行编译安装,不能通过pip安装。

要在C ++中运行我们的模型,我们首先需要获取Tensorflow源代码树。该指令在这里,但我们将在下面走通他们。

(tensorflow) $ ~/Development/keras-to-tensorflow $ git clone https://github.com/tensorflow/tensorflow
Cloning into 'tensorflow'...
remote: Counting objects: 223667, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 223667 (delta 2), reused 8 (delta 0), pack-reused 223652
Receiving objects: 100% (223667/223667), 116.84 MiB | 3.17 MiB/s, done.
Resolving deltas: 100% (173085/173085), done.
Checking connectivity... done.
(tensorflow) $ ~/Development/keras-to-tensorflow $ cd tensorflow/
(tensorflow) $ ~/Development/keras-to-tensorflow/tensorflow $ git checkout r1.1
Branch r1.1 set up to track remote branch r1.1 from origin.
Switched to a new branch 'r1.12'
(tensorflow) $ ~/Development/keras-to-tensorflow/tensorflow $ ls
ACKNOWLEDGMENTS  BUILD              LICENSE       tensorflow   WORKSPACE
ADOPTERS.md      configure          models.BUILD  third_party
AUTHORS          CONTRIBUTING.md    README.md     tools
bower.BUILD      ISSUE_TEMPLATE.md  RELEASE.md    util
(tensorflow) $ ~/Development/keras-to-tensorflow/tensorflow $

查看tensorflow的要求,我们选择bazel0.15.0,要求如下:

bazel的安装说明如下:
【mcj】bazel安装指定版本

验证以下bazel的版本信息:
bazel help

现在我们已经安装了所有东西,我们可以配置和构建。确保您位于顶级tensorflow目录中。我使用了所有默认配置选项。执行此操作时,配置工具将下载一堆依赖项 – 这需要一两分钟。

(tensorflow) $ ~/Development/keras-to-tensorflow/tensorflow $ ./configure
Please specify the location of python. [Default is /home/bitwise/anaconda3/envs/tensorflow/bin/python]:
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N]
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]
No XLA support will be enabled for TensorFlow
Found possible Python library paths:
  /home/bitwise/anaconda3/envs/tensorflow/lib/python3.6/site-packages
Please input the desired Python library path to use.  Default is [/home/bitwise/anaconda3/envs/tensorflow/lib/python3.6/site-packages]

Using python library path: /home/bitwise/anaconda3/envs/tensorflow/lib/python3.6/site-packages
Do you wish to build TensorFlow with OpenCL support? [y/N]
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N]
No CUDA support will be enabled for TensorFlow
Configuration finished
INFO: Starting clean (this may take a while). Consider using --expunge_async if the clean takes more than several minutes.
............
WARNING: ~/.cache/bazel/_bazel_bitwise/7f7ca38846ebb2cb18e80e7c35ca353a/external/bazel_tools/tools/build_defs/pkg/pkg.bzl:196:9: pkg_tar: renaming non-dict `files` attribute to `srcs`
WARNING: ~/.cache/bazel/_bazel_bitwise/7f7ca38846ebb2cb18e80e7c35ca353a/external/bazel_tools/tools/build_defs/pkg/pkg.bzl:196:9: pkg_tar: renaming non-dict `files` attribute to `srcs`
WARNING: ~/.cache/bazel/_bazel_bitwise/7f7ca38846ebb2cb18e80e7c35ca353a/external/bazel_tools/tools/build_defs/pkg/pkg.bzl:196:9: pkg_tar: renaming non-dict `files` attribute to `srcs`
WARNING: ~/.cache/bazel/_bazel_bitwise/7f7ca38846ebb2cb18e80e7c35ca353a/external/bazel_tools/tools/build_defs/pkg/pkg.bzl:196:9: pkg_tar: renaming non-dict `files` attribute to `srcs`
Building: no action running

接下来进行build,如下:
bazel build --jobs=6 --verbose_failures -c opt --copt=-mavx --copt=-mfpmath=both --copt=-msse4.2 //tensorflow:libtensorflow_cc.so