基於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

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