先决条件
安装 bazel
brew install automake
brew install bazel
检查是否安装成功
$ bazel version
Starting local Bazel server and connecting to it...
Build label: 0.15.2-homebrew
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Jul 17 13:06:32 2018 (1531832792)
Build timestamp: 1531832792
Build timestamp as int: 1531832792
注意: Mac 上 0.15.0-homebrew
版本存在 bug, 详情参见 Bazel 0.15.0 breaks C++ linking on MacOS
编译安装 TensorFlow Serving
cd /opt
git clone --recurse-submodules https://github.com/tensorflow/serving
cd serving
bazel build -c opt //tensorflow_serving/model_servers:tensorflow_model_server
创建软连接
cd /usr/local/bin
ln -s /opt/serving/bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server .
部署模型
tensorflow_model_server --port=9000 --model_name=mnist --model_base_path=/tmp/review
客户端调用
用 pip 安装 tensorflowservingclient 包
# encoding=utf-8
import tensorflow as tf
from grpc.beta import implementations
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
channel = implementations.insecure_channel('localhost', 9000)
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
x_data = [0.9993293, 1., 0.76159416, 0.76159416, 0.2, 0.99558765,
0.4522293, 0.58917197, 0.00318471, 0.07961783, 0.01273885, 0.05732484,
0.01592357, 0.99997974, 0.25882353, 0.04117647, 0.93110961, 0.3125,
0.3359375, 0.484375, 0.9140625, 0.596875]
request = predict_pb2.PredictRequest()
request.model_spec.name = 'mnist'
request.inputs['x'].CopyFrom(tf.contrib.util.make_tensor_proto(x_data, shape=[1, 22]))
print(stub.Predict(request, 10.0))