0

我想使用类型模型的导出方法tf.contrib.learn.DNNLinearCombinedClassifier来保存模型,然后编写 tensorflow 服务客户端来请求对模型的预测。

有人可以解释一下:

  1. 如何BaseEstimator.export从教程中的 input_fn 的结果或预训练估计器的任何其他部分创建参数?

  2. 如何创建request=predict_pb2.PredictRequest()发送到张量流服务器实例?

4

2 回答 2

1

在这里,我写了一个简单的教程Exporting and Serving a TensorFlow Wide & Deep Model

要导出估算器,有四个步骤:

  1. 将要导出的特征定义为估计器初始化期间使用的所有特征的列表。

  2. 使用create_feature_spec_for_parsing.

  3. 构建一个serving_input_fn适合使用的服务使用input_fn_utils.build_parsing_serving_input_fn

  4. 使用 导出模型export_savedmodel()

要正确运行客户端脚本,您需要执行以下三个步骤:

  1. 创建脚本并将其放置在 /serving/ 文件夹中的某个位置,例如 /serving/tensorflow_serving/example/

  2. 通过添加py_binary.

  3. 构建并运行模型服务器,例如tensorflow_model_server.

  4. 创建、构建和运行一个客户端,该客户端向我们发送一个 tf.Example 以tensorflow_model_server进行推理。

如果您的模型是使用导出的Estimator.export_savedmodel()并且您成功构建了 TensorFlow Serving 本身,那么您可以执行以下操作:

from grpc.beta import implementations
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2

tf.app.flags.DEFINE_string('server', 'localhost:9000', 'Server host:port.')
tf.app.flags.DEFINE_string('model', 'wide_and_deep', 'Model name.')
FLAGS = tf.app.flags.FLAGS
...
def main(_):

  host, port = FLAGS.server.split(':')
  # Set up a connection to the TF Model Server
  channel = implementations.insecure_channel(host, int(port))
  stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

  # Create a request that will be sent for an inference
  request = predict_pb2.PredictRequest()
  request.model_spec.name = FLAGS.model
  request.model_spec.signature_name = 'serving_default'

  # A single tf.Example that will get serialized and turned into a TensorProto
  feature_dict = {'age': _float_feature(value=25),
                  'capital_gain': _float_feature(value=0),
                  'capital_loss': _float_feature(value=0),
                  'education': _bytes_feature(value='11th'.encode()),
                  'education_num': _float_feature(value=7),
                  'gender': _bytes_feature(value='Male'.encode()),
                  'hours_per_week': _float_feature(value=40),
                  'native_country': _bytes_feature(value='United-States'.encode()),
                  'occupation': _bytes_feature(value='Machine-op-inspct'.encode()),
                  'relationship': _bytes_feature(value='Own-child'.encode()),
                  'workclass': _bytes_feature(value='Private'.encode())}
  label = 0

  example = tf.train.Example(features=tf.train.Features(feature=feature_dict))
  serialized = example.SerializeToString()

  request.inputs['inputs'].CopyFrom(
    tf.contrib.util.make_tensor_proto(serialized, shape=[1]))

  # Create a future result, and set 5 seconds timeout
  result_future = stub.Predict.future(request, 5.0)
  prediction = result_future.result().outputs['scores']

  print('True label: ' + str(label))
  print('Prediction: ' + str(np.argmax(prediction)))

有关更多详细信息,请查看教程本身。

希望能帮助到你。

PS 这类问题至少有 4 个重复。如果有高代表的人可以关闭或分组他们,那就太好了)

于 2017-05-09T19:24:19.467 回答
0

当您提供参考链接时,我会遵循: https ://github.com/MtDersvan/tf_playground/blob/master/wide_and_deep_tutorial/wide_and_deep_basic_serving.md

你在哪里定义签名? serving_default 我认为当您导出模型时需要它。但在您的导出代码中没有提及。请定义它。

于 2017-05-29T12:04:18.147 回答