Created
May 14, 2020 07:39
-
-
Save liviaerxin/e2e8ef2478d4a4ff17c154289013eaf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import argparse | |
import time | |
import numpy as np | |
from scipy.misc import imread | |
import grpc | |
from tensorflow.contrib.util import make_tensor_proto | |
from tensorflow_serving.apis import predict_pb2 | |
from tensorflow_serving.apis import prediction_service_pb2_grpc | |
def run(host, port, image, model, signature_name): | |
channel = grpc.insecure_channel('{host}:{port}'.format(host=host, port=port)) | |
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) | |
# Read an image | |
data = imread(image) | |
data = data.astype(np.float32) | |
print(data) | |
start = time.time() | |
# Call classification model to make prediction on the image | |
request = predict_pb2.PredictRequest() | |
request.model_spec.name = model | |
request.model_spec.signature_name = signature_name | |
request.inputs['image'].CopyFrom(make_tensor_proto(data, shape=[1, 28, 28, 1])) | |
result = stub.Predict(request, 10.0) | |
end = time.time() | |
time_diff = end - start | |
# Reference: | |
# How to access nested values | |
# https://stackoverflow.com/questions/44785847/how-to-retrieve-float-val-from-a-predictresponse-object | |
print(result) | |
print('time elapased: {}'.format(time_diff)) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--host', help='Tensorflow server host name', default='localhost', type=str) | |
parser.add_argument('--port', help='Tensorflow server port number', default=8500, type=int) | |
parser.add_argument('--image', help='input image', type=str) | |
parser.add_argument('--model', help='model name', type=str) | |
parser.add_argument('--signature_name', help='Signature name of saved TF model', | |
default='serving_default', type=str) | |
args = parser.parse_args() | |
run(args.host, args.port, args.image, args.model, args.signature_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment