-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdataGenerator.py
48 lines (36 loc) · 1.13 KB
/
dataGenerator.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
import socket
import os
import stat
import numpy as np
import time
import select
import grpc
from augmentation import augGrpc, augPb, SERVER_PORT, CS
MAX_MESSAGE_LENGTH = 90000000
def DataGenerator(data_type='train'):
channel = grpc.insecure_channel(
'localhost:%d' % SERVER_PORT,
options=[('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)]
)
stub = augGrpc.DataProviderStub(channel)
empty = augPb.Empty()
ret = stub.Control(CS(sign=CS.START))
if data_type == 'train':
func = stub.GetTrainData
elif data_type == 'test':
func = stub.GetTestData
else:
raise ValueError('data_type should be either "train" or "test"')
while True:
ret = func(empty)
data = np.frombuffer(ret.data, dtype=np.float32)
label = np.frombuffer(ret.label, dtype=np.float32)
data = data.reshape([ret.batch_size, -1, 1, 1])
label = label.reshape([ret.batch_size, -1])
yield data, label
if __name__ == '__main__':
gen = DataGenerator()
for i in range(0, 100):
label, d = next(gen)
print(i, label)
time.sleep(1)