基于TensorFlow C++ API 的 gRPC 服务

版本对应关系:

bazel 0.15
tensorflow 1.10
cuda 9.2
cudnn 7.3.1
protobuf 3.6.0
nvidia driver

<https://docs.bazel.build/versions/master/install-ubuntu.html>
1. download the Bazel binary installer named bazel-<version>-installer-linux-x86_64.sh from the Bazel releases page on GitHub(https://github.com/bazelbuild/bazel/releases).
1) wget https://github.com/bazelbuild/bazel/releases/download/0.15.0/bazel-0.15.0-installer-linux-x86_64.sh
2) bash bazel-0.15.0-installer-linux-x86_64.sh

<https://developer.nvidia.com/cuda-downloads>
2. download cuda
1)wget 
2)sudo sh cuda_9.2.148_396.37_linux.run 需要安装driver和cuda

<https://developer.nvidia.com/rdp/cudnn-archive#a-collapse731-92>
3. download cudnn (libcudnn7_7.3.1.20-1+cuda9.2_amd64.deb)
1)注册账号
2)下载“Runtime Library”和“Developer Library”连个deb安装包
3)将上述连个安装包先后安装 sudo dpkg -i libcudnn7-dev_7.3.1.20-1+cuda9.2_amd64.deb

4. download TensorFlow
1)git clone https://github.com/tensorflow/tensorflow.git -b r1.10 --depth=1
2)在文件“tensorflow/tensorflow/workspace.bzl”中查看eigen、nsync、protobuf的下载地址,并直接下载这些项目

5. download and install eigen
1)wget https://mirror.bazel.build/bitbucket.org/eigen/eigen/get/fd6845384b86.tar.gz
2)解压 tar -xvf fd6845384b86.tar.gz
3)mkdir build && cd build && cmake ..
4)make
5)make install
6)将生成目录添加到CPP路径

export CPLUS_INCLUDE_PATH="/usr/local/include/eigen3/:$CPLUS_INCLUDE_PATH"

6. download and install nsync
1)wget https://mirror.bazel.build/github.com/google/nsync/archive/1.20.0.tar.gz
2)解压 tar -xvf 1.20.0.tar.gz
3)将目录“nsync-1.20.0/public”添加到CPP路径

export CPLUS_INCLUDE_PATH="$HOME/nsync-1.20.0/public/:$CPLUS_INCLUDE_PATH"

7. download and install protobuf
1)wget https://mirror.bazel.build/github.com/google/protobuf/archive/v3.6.0.tar.gz
2)sudo apt-get install autoconf automake libtool #安装相关工具
3)./autogen.sh
4)./configure --prefix=$HOME/output/protobuf
5)make
6)make install prefix=$HOME/output/protobuf

export CPLUS_INCLUDE_PATH="$HOME/output/protobuf/include/google:$CPLUS_INCLUDE_PATH"

8. install tensorflow
1)./configure #指定cuda和cudnn的版本,注意gcc指定gcc-5版本,不能太高
2)bazel build //tensorflow:libtensorflow_cc.so #可能需要科学联网

9. test tensorflow
1)将上个步骤“在bazel-bin/tensorflow下libtensorflow_cc.so和libtensorflow_framework.so”两个编译好的动态库提取出来;
2)下载git代码“https://github.com/zhangcliff/tensorflow-c-mnist”
3)修改CMakLists.txt中的头文件目录,将步骤1的两个动态库添加到lib文件夹;
4)在build目录下,直接cmake ..,然后make,根据错误提示进行tf.cpp代码调整

10. install grpc
1)git clone https://github.com/grpc/grpc.git --depth=1
2)vim .gitmodules (将protobuf的branch指定为3.6.0)
3)git submodule update --init
4)make
5)sudo make install prefix=$HOME/output/grpc
备注:注:gRpc 源码中的 Makefile 文件中会自动检测当前系统是否已经安装了 protoc,如果没有安装,那么就会自动从其项目中的第三方库源码目录中进行安装。

11. download tensorflow_serving
1)git clone https://github.com/tensorflow/serving.git --depth=1

12. get 'grpc.pb.h' and 'pb.h' file

protoc -I=$SRC_DIR --cpp_out=$DST_DIR /path/to/file.proto
# 上面的命令会生成xxx.pb.h 和 xxx.pb.cc两个C++文件。 (数据定义文件)

protoc -I=$SRC_DIR --grpc_out=$DST_DIR --plugin=protoc-gen-grpc=/path/to/grpc_cpp_plugin  /path/to/file.proto
# 上面的命令会生成xxx.grpc.pb.h 和 xxx.grpc.pb.cc两个C++文件。 (接口文件)

举例:

xupeng@ubantu16:/home/alpha_cpp/Output_linux/include/proto_use$ protoc -I=/home/alpha_cpp/Output_linux/include/ --cpp_out=protoc_output /home/alpha_cpp/Output_linux/include/tensorflow_serving/apis/predict.proto

xupeng@ubantu16:/home/alpha_cpp/Output_linux/include/proto_use$ protoc -I=/home/alpha_cpp/Output_linux/include/ --grpc_out=protoc_output --plugin=protoc-gen-grpc=/home/xupeng/output/bin/grpc_cpp_plugin /home/alpha_cpp/Output_linux/include/tensorflow_serving/apis/predict.proto

参考:

https://www.jianshu.com/p/725c45353c9d

https://tensorflow.google.cn/install/source#docker_linux_builds

http://doc.oschina.net/grpc?t=57966

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章