1、安装grpc
gRPC 的安装:
$ pip install grpcio
安装 ProtoBuf 相关的 python 依赖库:
$ pip install protobuf
安装 python grpc 的 protobuf 编译工具:
$ pip install grpcio-tools
1 # run at root of tensorflow_serving repo 2 3 TARGET_DIR="$1" 4 5 python -m grpc.tools.protoc 6 -I . -I ./tensorflow 7 --python_out "$TARGET_DIR" 8 tensorflow_serving/servables/tensorflow/saved_model_bundle_source_adapter.proto 9 tensorflow_serving/servables/tensorflow/session_bundle_config.proto 10 tensorflow_serving/config/platform_config.proto 11 12 pushd $TARGET_DIR 13 14 touch tensorflow_serving/__init__.py 15 touch tensorflow_serving/config/__init__.py 16 touch tensorflow_serving/servables/__init__.py 17 touch tensorflow_serving/servables/tensorflow/__init__.py 18 19 popd
sh gen-tf-serving-proto-py.sh /tmp
3、将生成的*_pb2.py文件cp出来
cp -r /tmp/tensorflow_serving .
4、在当前目录运行gen-platform-config.py
1 # -*- coding: utf-8 -*- 2 3 4 import tensorflow as tf 5 6 from tensorflow_serving.config import platform_config_pb2 7 from tensorflow_serving.servables.tensorflow import session_bundle_config_pb2 8 from tensorflow_serving.servables.tensorflow import saved_model_bundle_source_adapter_pb2 9 10 11 session_config = tf.ConfigProto() 12 # config whatever you want 13 session_config.gpu_options.allow_growth = True 14 session_config.gpu_options.per_process_gpu_memory_fraction = 0.4 15 16 legacy_config=session_bundle_config_pb2.SessionBundleConfig(session_config=session_config) 17 adapter = saved_model_bundle_source_adapter_pb2.SavedModelBundleSourceAdapterConfig(legacy_config=legacy_config) 18 19 config_map = platform_config_pb2.PlatformConfigMap() 20 config_map.platform_configs['tensorflow'].source_adapter_config.Pack(adapter) 21 22 print(config_map)
5、生成platform_config_file.cfg文件
1 platform_configs { 2 key: "tensorflow" 3 value { 4 source_adapter_config { 5 [type.googleapis.com/tensorflow.serving.SavedModelBundleSourceAdapterConfig] { 6 legacy_config { 7 session_config { 8 gpu_options { 9 per_process_gpu_memory_fraction: 0.4 10 allow_growth: true 11 } 12 } 13 } 14 } 15 } 16 } 17 }
6、运行tf_serving时添加参数--platform_config_file=./conf/platform_config_file.cfg
7、若同时需要配置batching_parameters_file,则需要将batching参数写入到platform_config_file.cfg内
1 platform_configs { 2 key: "tensorflow" 3 value { 4 source_adapter_config { 5 [type.googleapis.com/tensorflow.serving.SavedModelBundleSourceAdapterConfig] { 6 legacy_config { 7 batching_parameters { 8 max_batch_size { value: 1000000 } 9 batch_timeout_micros { value: 200000000 } 10 max_enqueued_batches { value: 1000000 } 11 num_batch_threads { value: 36 } 12 } 13 session_config { 14 allow_soft_placement: true 15 gpu_options { 16 per_process_gpu_memory_fraction: 0.4 17 allow_growth: true 18 } 19 } 20 } 21 } 22 } 23 } 24 }
详细信息参照:https://github.com/tensorflow/serving/issues/342
我运行后生成的cfg文件为
1 platform_configs { 2 key: "tensorflow" 3 value { 4 source_adapter_config { 5 type_url: "type.googleapis.com/tensorflow.serving.SavedModelBundleSourceAdapterConfig" 6 value: "302>