TenserFlow Serving Client


In [11]:
!pip install tensorflow-serving-api


Collecting tensorflow-serving-api
  Could not find a version that satisfies the requirement tensorflow-serving-api (from versions: )
No matching distribution found for tensorflow-serving-api

In [1]:
import tensorflow as tf

In [2]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('/tmp/tensorflow/alex/mnist/input_data', one_hot=True)


Extracting /tmp/tensorflow/alex/mnist/input_data/train-images-idx3-ubyte.gz
Extracting /tmp/tensorflow/alex/mnist/input_data/train-labels-idx1-ubyte.gz
Extracting /tmp/tensorflow/alex/mnist/input_data/t10k-images-idx3-ubyte.gz
Extracting /tmp/tensorflow/alex/mnist/input_data/t10k-labels-idx1-ubyte.gz

In [4]:
from grpc.beta import implementations
from tensorflow_serving.apis import prediction_service_pb2

channel = implementations.insecure_channel('10.8.58.87', 8500)
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)


---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-b0cbaa10edca> in <module>()
      1 from grpc.beta import implementations
----> 2 from tensorflow_serving.apis import prediction_service_pb2
      3 
      4 channel = implementations.insecure_channel('10.8.58.87', 8500)
      5 stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

ModuleNotFoundError: No module named 'tensorflow_serving'

In [13]:
from grpc.beta import implementations
from tensorflow_serving.apis import prediction_service_pb2

channel = implementations.insecure_channel('10.8.58.87', 8500)
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)


---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-13-fefcaa73f167> in <module>()
      1 from grpc.beta import implementations
----> 2 from tensorflow_serving.apis import predict_pb2
      3 from tensorflow_serving.apis import prediction_service_pb2
      4 
      5 class _ResultCounter(object):

ModuleNotFoundError: No module named 'tensorflow_serving'

In [ ]:
channel = implementations.insecure_channel('10.8.58.87', 8500)
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

for _ in range(num_tests):
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'mnist'
    request.model_spec.signature_name = 'predict_images'
    image, label = mnist.test.next_batch(1)
    request.inputs['images'].CopyFrom(tf.contrib.util.make_tensor_proto(image[0], shape=[1, image[0].size]))
    result_counter.throttle()
    result_future = stub.Predict.future(request, 5.0)  # 5 seconds
    result_future.add_done_callback(_create_rpc_callback(label[0], result_counter))
return result_counter.get_error_rate()