-
Notifications
You must be signed in to change notification settings - Fork 94
/
svnh_semi_supervised_client_no_tf.py
90 lines (65 loc) · 2.71 KB
/
svnh_semi_supervised_client_no_tf.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''
Send JPEG image to tensorflow_model_server loaded with GAN model.
Hint: the client does not require the complete Tensorflow framework. However
you must create Python files from the protobuf files for:
- tensorflow.core.framework
- tensorflow.core.example
- tensorflow.core.protobuf
- tensorflow_serving.apis
'''
from __future__ import print_function
import time
from argparse import ArgumentParser
# Communication to TensorFlow server via gRPC
from grpc.beta import implementations
# TensorFlow serving stuff to send messages
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
from tensorflow.core.framework import tensor_pb2
from tensorflow.core.framework import tensor_shape_pb2
from tensorflow.core.framework import types_pb2
import six as _six
def parse_args():
parser = ArgumentParser(description="Request a TensorFlow server for a prediction on the image")
parser.add_argument("-s", "--server",
dest="server",
default='172.17.0.2:9000',
help="prediction service host:port")
parser.add_argument("-i", "--image",
dest="image",
default="",
help="path to image in JPEG format",)
args = parser.parse_args()
host, port = args.server.split(':')
return host, port, args.image
def main():
# parse command line arguments
host, port, image = parse_args()
channel = implementations.insecure_channel(host, int(port))
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
# Send request
with open(image, 'rb') as f:
# See prediction_service.proto for gRPC request/response details.
data = f.read()
start = time.time()
request = predict_pb2.PredictRequest()
# Call GAN model to make prediction on the image
request.model_spec.name = 'gan'
request.model_spec.signature_name = 'predict_images'
# create TensorProto object for a request
dims = [tensor_shape_pb2.TensorShapeProto.Dim(size=1)]
tensor_shape_proto = tensor_shape_pb2.TensorShapeProto(dim=dims)
tensor_proto = tensor_pb2.TensorProto(
dtype=types_pb2.DT_STRING,
tensor_shape=tensor_shape_proto,
string_val=[data])
# put data into TensorProto and copy them into the request object
request.inputs['images'].CopyFrom(tensor_proto)
# call prediction
result = stub.Predict(request, 60.0) # 60 secs timeout
end = time.time()
time_diff = end - start
print(result)
print('time elapased: {}'.format(time_diff))
if __name__ == '__main__':
main()