forked from tobegit3hub/tensorflow_template_application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_client.py
executable file
·51 lines (40 loc) · 1.67 KB
/
predict_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
import numpy
from grpc.beta import implementations
import tensorflow as tf
import predict_pb2
import prediction_service_pb2
tf.app.flags.DEFINE_string("host", "127.0.0.1", "gRPC server host")
tf.app.flags.DEFINE_integer("port", 9000, "gRPC server port")
tf.app.flags.DEFINE_string("model_name", "cancer", "TensorFlow model name")
tf.app.flags.DEFINE_integer("model_version", -1, "TensorFlow model version")
tf.app.flags.DEFINE_float("request_timeout", 10.0, "Timeout of gRPC request")
FLAGS = tf.app.flags.FLAGS
def main():
host = FLAGS.host
port = FLAGS.port
model_name = FLAGS.model_name
model_version = FLAGS.model_version
request_timeout = FLAGS.request_timeout
# Generate inference data
keys = numpy.asarray([1, 2, 3])
keys_tensor_proto = tf.contrib.util.make_tensor_proto(keys, dtype=tf.int32)
features = numpy.asarray(
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 1, 1, 1, 1, 1, 1, 1],
[9, 8, 7, 6, 5, 4, 3, 2, 1], [9, 9, 9, 9, 9, 9, 9, 9, 9]])
features_tensor_proto = tf.contrib.util.make_tensor_proto(features,
dtype=tf.float32)
# Create gRPC client and request
channel = implementations.insecure_channel(host, port)
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = model_name
if model_version > 0:
request.model_spec.version.value = model_version
request.inputs['keys'].CopyFrom(keys_tensor_proto)
request.inputs['features'].CopyFrom(features_tensor_proto)
# Send request
result = stub.Predict(request, request_timeout)
print(result)
if __name__ == '__main__':
main()