A Unified Slim Client on PredictionService
The objective is unifying the clients:
connect to a tensorflow model servable with specific name given from parameter
capable of processing a batch of image_urls
slim_unified_client.py
- add slim_unified_client deps in example's BUILD
- ./tensorflow_serving/example/BUILD
...
py_binary(
name = "slim_unified_client",
srcs = [
"slim_unified_client.py",
],
srcs_version = "PY2AND3",
deps = [
"//tensorflow_serving/apis:predict_proto_py_pb2",
"//tensorflow_serving/apis:prediction_service_proto_py_pb2",
"@org_tensorflow//tensorflow:tensorflow_py",
],
)
...
- create slim unified client target
- ./tensorflow_serving/example/slim_unified_client.py
...
#!/usr/bin/env python2.7
"""Send image_urls to tensorflow_model_server loaded with slim model.
"""
from __future__ import print_function
import urllib2
from multiprocessing.pool import ThreadPool
# This is a placeholder for a Google-internal import.
from grpc.beta import implementations
import tensorflow as tf
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
tf.app.flags.DEFINE_string(
'server', 'localhost:9000', 'PredictionService host:port'
)
tf.app.flags.DEFINE_string(
'model_name', '', 'Tensorflow Serving Model Name'
)
tf.app.flags.DEFINE_string(
'image_urls', '', 'URL to image in JPEG format, comma separated URLs.'
)
FLAGS = tf.app.flags.FLAGS
def fetch_url(url):
try:
response = urllib2.urlopen(url)
return url, response.read(), None
except Exception as e:
return url, None, e
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
# See prediction_service.proto for gRPC request/response details.
image_urls = FLAGS.image_urls.split(',')
print('image_urls:', image_urls)
image_fetch_results = ThreadPool(128).map(fetch_url, image_urls)
image_bytes = [
result[1] for result in image_fetch_results if result[2] is None
]
for result in image_fetch_results:
if result[2] is not None:
print('Error in Fetch URL: {}'.format(result[2]))
request = predict_pb2.PredictRequest()
request.model_spec.name = FLAGS.model_name
request.model_spec.signature_name = 'predict_images'
request.inputs['images'].CopyFrom(
tf.contrib.util.make_tensor_proto(
image_bytes, shape=[len(image_bytes)]
)
)
result = stub.Predict(request, 60.0) # 60 secs timeout
print(result)
if __name__ == '__main__':
tf.app.run()
...
- server
# open a new terminal //src/serving
$ bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server \
--model_name=slim_inception_resnet_v2 \
--model_base_path=$PWD/tf_servables/slim/inception-resnet-v2 \
--port=9000 &
# open a new terminal //src/serving
$ bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server \
--model_name=slim_inception_v4 \
--model_base_path=$PWD/tf_servables/slim/inception-v4 \
--port=9090 &
- client
# open a new terminal //src/serving
$ bazel --output_user_root=./tf_bazel_cache build -c opt //tensorflow_serving/example:slim_unified_client
$ bazel-bin/tensorflow_serving/example/slim_unified_client \
--server=localhost:9000 \
--model_name=slim_inception_resnet_v2 \
--image_urls=/comma/separated/urls/to/my_favoriate_image.jpg
$ bazel-bin/tensorflow_serving/example/slim_unified_client \
--server=localhost:9090 \
--model_name=slim_inception_v4 \
--image_urls=/comma/separated/urls/to/my_favoriate_image.jpg
$ bazel-bin/tensorflow_serving/example/slim_unified_client \
--server=localhost:9000 \
--model_name=slim_inception_resnet_v2 \
--image_url=https://upload.wikimedia.org/wikipedia/commons/d/d9/First_Student_IC_school_bus_202076.jpg,\
https://upload.wikimedia.org/wikipedia/commons/thumb/9/90/Labrador_Retriever_portrait.jpg/1200px-Labrador_Retriever_portrait.jpg,\
https://upload.wikimedia.org/wikipedia/commons/f/fd/Qantas_a380_vh-oqa_takeoff_heathrow_arp.jpg
$ bazel-bin/tensorflow_serving/example/slim_unified_client \
--server=localhost:9090 \
--model_name=slim_inception_v4 \
--image_url=https://upload.wikimedia.org/wikipedia/commons/d/d9/First_Student_IC_school_bus_202076.jpg,\
https://upload.wikimedia.org/wikipedia/commons/thumb/9/90/Labrador_Retriever_portrait.jpg/1200px-Labrador_Retriever_portrait.jpg,\
https://upload.wikimedia.org/wikipedia/commons/f/fd/Qantas_a380_vh-oqa_takeoff_heathrow_arp.jpg
# note: classes, prelogits, and scores are tensors with shape [n-images] x [k-outputs]