【问题标题】:Saving a custom tf.estimator trained model for tensorflow serving为 TensorFlow 服务保存自定义的 tf.estimator 训练模型
【发布时间】:2019-03-22 05:37:29
【问题描述】:

如果我有一个使用自定义估算器的 tensorflow 模型,我将如何保存该模型以便将其部署到生产环境中。

https://colab.research.google.com/github/google-research/bert/blob/master/predicting_movie_reviews_with_bert_on_tf_hub.ipynb#scrollTo=JIhejfpyJ8Bx

我正在使用的模型与这个相似,并且想知道如何在模型经过训练后保存它。已尝试使用 Savedmodel 并使用检查点进行恢复,但两者均不成功(无法针对此示例进行调整)

【问题讨论】:

    标签: python tensorflow tensorflow-serving tensorflow-estimator


    【解决方案1】:

    一种方法是通过 gRPC。 TF 那里有一些不那么简单的文档:https://www.tensorflow.org/tfx/serving/serving_basic 最难的一点实际上是保存你的模型,之后通过 docker 托管它有相当多的文档。 最后,您可以使用 gRPC 客户端进行推断,即https://github.com/epigramai/tfserving-python-predict-client

    为此,您需要先保存模型。像这样的东西,你需要为你的例子稍微调整一下:

      def save_serving_model(self,estimator):
          feature_placeholder = {'sentence': tf.placeholder('string', [1], name='sentence_placeholder')}
    
          # The build_raw_serving_input_receiver_fn doesn't serialize inputs so avoids confusion with bytes and strings. You can simply pass a string.
          serving_input_fn = tf.estimator.export.build_raw_serving_input_receiver_fn(feature_placeholder)
    
          # Save the model
          estimator.export_savedmodel("./TEST_Dir", serving_input_fn)
    
    

    这会将模型保存在TEST_Dir 中。 作为一个快速测试,你可以这样做:

    saved_model_cli run --dir /path/to/mode/ --tag_set serve --signature_def predict --input_exprs="sentence=['This API is a little tricky']"
    

    下一步是托管此模型,或“服务”它。我这样做的方式是通过 docker,即像

    这样的命令
    docker run -p 8500:8500 \
    --mount type=bind,source=/tmp/mnist,target=/models/mnist \
    -e MODEL_NAME=mnist -t tensorflow/serving &
    
    

    最后,您可以使用 predict 客户端(通过 gRPC)向您的服务器传递一个句子并返回结果。我在上面添加的github 链接有两篇关于此的博文。

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      相关资源
      最近更新 更多