如何在TensorFlow服务中加载TensorFlow的Wide和Deep模型进行预测model_server

时间:2016-11-17 21:53:32

标签: tensorflow tensorflow-serving

有人可以协助我对TensorFlow的广泛和深度学习模型进行预测,这些模型加载到TensorFlow服务的model_server中吗?

如果有人能指出我的资源或文档,那将非常有用。

2 个答案:

答案 0 :(得分:1)

您可以尝试调用估算器的预测方法,并将n_array的as_iterable设置为false

y = m.predict(input_fn=lambda: input_fn(df_test), as_iterable=False)

但是,请注意弃用说明here,以便将来兼容。

答案 1 :(得分:0)

如果使用Estimator.export_savedmodel()导出模型并且您成功构建了TensorFlow服务,则可以执行以下操作:

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)))

在这里,我编写了一个简单的教程Exporting and Serving a TensorFlow Wide & Deep Model,其中包含更多详细信息。

希望它有所帮助。