2017-04-13 132 views
1

我想使用TensorFlow服務於自定義模型(無預先訓練的起點)。TensorFlow服務於自定義模型

我已經通過了成立以來TensorFlow服務教程的預Kubernetes部分做到了,用多克:http://tensorflow.github.io/serving/serving_inception

我明白了(大致)的巴澤爾編譯的核心是如何一切正常。但我想了解如何從tensorflow_serving.apis生成predict_pb2的作品,以便我可以在我自己的自定義模型中交換。

需要明確的是,這是在inception_client.pymain目前的樣子:

def main(_): 
    host, port = FLAGS.server.split(':') 
    channel = implementations.insecure_channel(host, int(port)) 
    stub = prediction_service_pb2.beta_create_PredictionService_stub(channel) 
    # Send request 
    with open(FLAGS.image, 'rb') as f: 
    # See prediction_service.proto for gRPC request/response details. 
    data = f.read() 
    request = predict_pb2.PredictRequest() 
    request.model_spec.name = 'inception' 
    request.model_spec.signature_name = 'predict_images' 
    request.inputs['images'].CopyFrom(
     tf.contrib.util.make_tensor_proto(data, shape=[1])) 
    result = stub.Predict(request, 10.0) # 10 secs timeout 
    print(result) 

https://github.com/tensorflow/serving/blob/65f50621a192004ab5ae68e75818e94930a6778b/tensorflow_serving/example/inception_client.py#L38-L52

這是我很難解包和調試什麼predict_pb2.PredictRequest()做,因爲它是巴澤爾產生。但我想重新指出一個完全不同的,保存的模型,其自己的.pb文件等。

我該如何引用不同的保存模型?

回答

4

PredictionService,定義爲here,是gRPC API服務定義,它定義了服務器將響應的RPC函數。從這個原型中,bazel/protoc可以生成將在服務器和客戶機中鏈接的代碼(您提到的predict_pb2)。

服務器擴展了自動生成的服務here併爲每個功能提供了一個實現。

Python客戶端使用提供的predict_pb2並使用它構建請求並使用正確的API發送RPC。

predict_pb2.PredictRequest()是一個PredictRequest原型,定義爲here,它是Predict()API調用的請求類型(請參閱上面鏈接的PredictService原型定義)。代碼的這部分只是建立一個請求,並且result = stub.Predict(request, 10.0)是實際發送請求的地方。

爲了使用不同的模型,您只需要將ModelSpec的模型名稱更改爲您的模型。在上面的例子中,服務器加載了名爲「初始」的iception模型,所以客戶端用request.model_spec.name = 'inception'查詢它。要改用你的模型,你只需要將名稱改爲你的模型名稱即可。請注意,您可能還需要將signature_name更改爲您的自定義名稱,或者將其完全移除以使用默認簽名(假設已定義)。