-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient-big.py
72 lines (61 loc) · 2.39 KB
/
client-big.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
import logging, grpc, time
import numpy as np
import server_tools_pb2
import server_tools_pb2_grpc
PORT = '50051'
f = open("IP.txt")
IP = f.read()
if IP[-1] == '\n':
IP = IP[:-1]
f.close()
# Set this flag to indicate whether the client should wait until the prediction
# is finished or check in with the server periodically until it is
WAIT = False
# Change this parameter depending on how many images you want to send at once.
# There is an upper limit (668 on my machine) where the size of the package
# becomes too great and the client will throw an error.
NUM_IMAGES = 668
def run():
# Get a handle to the server
channel = grpc.insecure_channel(IP + ':' + PORT)
stub = server_tools_pb2_grpc.MnistServerStub(channel)
# Get a client ID which you need to talk to the server
try:
response = stub.RequestClientID(server_tools_pb2.NullParam())
except:
print("Connection to the server could not be established. Press enter to try again.")
return
client_id = response.new_id
# Generate lots of data
data = np.random.rand(NUM_IMAGES, 28, 28, 1)
data = data.tostring()
# Send the data to the server and receive an answer
start_time = time.time()
if WAIT:
print("Submitting images and waiting")
response = stub.StartJobWait(server_tools_pb2.DataMessage(images=data, client_id=client_id, batch_size=32))
else:
print("Submitting images")
try:
idPackage = stub.StartJobNoWait(server_tools_pb2.DataMessage(images=data, client_id=client_id, batch_size=32))
except:
print("NUM_IMAGES is too high")
return
response = stub.ProbeJob(idPackage)
print("Checking in with server")
while not response.complete:
response = stub.ProbeJob(idPackage)
if response.error != '':
print(response.error)
break
# Print output
original_array = np.frombuffer(response.prediction).reshape(NUM_IMAGES, 10)
whole_time = time.time() - start_time
print("Total time:", whole_time)
print("Predict time:", response.infer_time)
print("Fraction of time spent not predicting:", (1 - response.infer_time / whole_time) * 100, '%')
channel.close()
if __name__ == '__main__':
logging.basicConfig()
while input() == '':
run()