1、安装grpc
gRPC 的安装:
$ pip install grpcio
安装 ProtoBuf 相关的 python 依赖库:
$ pip install protobuf
安装 python grpc 的 protobuf 编译工具:
$ pip install grpcio-tools
2、在serving目录运行脚本,生成*_pb2.py文件
# run at root of tensorflow_serving repo TARGET_DIR="$1" python -m grpc.tools.protoc \
-I . -I ./tensorflow \
--python_out "$TARGET_DIR" \
tensorflow_serving/servables/tensorflow/saved_model_bundle_source_adapter.proto \
tensorflow_serving/servables/tensorflow/session_bundle_config.proto \
tensorflow_serving/config/platform_config.proto pushd $TARGET_DIR touch tensorflow_serving/__init__.py
touch tensorflow_serving/config/__init__.py
touch tensorflow_serving/servables/__init__.py
touch tensorflow_serving/servables/tensorflow/__init__.py popd
sh gen-tf-serving-proto-py.sh /tmp
3、将生成的*_pb2.py文件cp出来
cp -r /tmp/tensorflow_serving .
4、在当前目录运行gen-platform-config.py
# -*- coding: utf-8 -*- import tensorflow as tf from tensorflow_serving.config import platform_config_pb2
from tensorflow_serving.servables.tensorflow import session_bundle_config_pb2
from tensorflow_serving.servables.tensorflow import saved_model_bundle_source_adapter_pb2 session_config = tf.ConfigProto()
# config whatever you want
session_config.gpu_options.allow_growth = True
session_config.gpu_options.per_process_gpu_memory_fraction = 0.4 legacy_config=session_bundle_config_pb2.SessionBundleConfig(session_config=session_config)
adapter = saved_model_bundle_source_adapter_pb2.SavedModelBundleSourceAdapterConfig(legacy_config=legacy_config) config_map = platform_config_pb2.PlatformConfigMap()
config_map.platform_configs['tensorflow'].source_adapter_config.Pack(adapter) print(config_map)
5、生成platform_config_file.cfg文件
platform_configs {
key: "tensorflow"
value {
source_adapter_config {
[type.googleapis.com/tensorflow.serving.SavedModelBundleSourceAdapterConfig] {
legacy_config {
session_config {
gpu_options {
per_process_gpu_memory_fraction: 0.4
allow_growth: true
}
}
}
}
}
}
}
6、运行tf_serving时添加参数--platform_config_file=./conf/platform_config_file.cfg
7、若同时需要配置batching_parameters_file,则需要将batching参数写入到platform_config_file.cfg内
platform_configs {
key: "tensorflow"
value {
source_adapter_config {
[type.googleapis.com/tensorflow.serving.SavedModelBundleSourceAdapterConfig] {
legacy_config {
batching_parameters {
max_batch_size { value: }
batch_timeout_micros { value: }
max_enqueued_batches { value: }
num_batch_threads { value: }
}
session_config {
allow_soft_placement: true
gpu_options {
per_process_gpu_memory_fraction: 0.4
allow_growth: true
}
}
}
}
}
}
}
详细信息参照:https://github.com/tensorflow/serving/issues/342
我运行后生成的cfg文件为
platform_configs {
key: "tensorflow"
value {
source_adapter_config {
type_url: "type.googleapis.com/tensorflow.serving.SavedModelBundleSourceAdapterConfig"
value: "\302>\017\022\r2\013\t\232\231\231\231\231\231\331? \001"
}
}
}
并不能生成清晰的text格式的配置文件,目前还未找到原因