tensorflow serving 編寫配置文件platform_config_file的方法


1、安裝grpc

gRPC 的安裝:

        $ pip install grpcio

安裝 ProtoBuf 相關的 python 依賴庫:

          $ pip install protobuf

安裝 python grpc 的 protobuf 編譯工具:

         $ pip install grpcio-tools

2、在serving目錄運行腳本,生成*_pb2.py文件
 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>\017\022\r2\013\t\232\231\231\231\231\231\331? \001"
7     }
8   }
9 }

並不能生成清晰的text格式的配置文件,目前還未找到原因


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM