forked from commaai/research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dask_generator.py
135 lines (106 loc) · 3.9 KB
/
dask_generator.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
This file is named after `dask` for historical reasons. We first tried to
use dask to coordinate the hdf5 buckets but it was slow and we wrote our own
stuff.
"""
import numpy as np
import h5py
import time
import logging
import traceback
# logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def concatenate(camera_names, time_len):
logs_names = [x.replace('camera', 'log') for x in camera_names]
angle = [] # steering angle of the car
speed = [] # steering angle of the car
hdf5_camera = [] # the camera hdf5 files need to continue open
c5x = []
filters = []
lastidx = 0
for cword, tword in zip(camera_names, logs_names):
try:
with h5py.File(tword, "r") as t5:
c5 = h5py.File(cword, "r")
hdf5_camera.append(c5)
x = c5["X"]
c5x.append((lastidx, lastidx+x.shape[0], x))
speed_value = t5["speed"][:]
steering_angle = t5["steering_angle"][:]
idxs = np.linspace(0, steering_angle.shape[0]-1, x.shape[0]).astype("int") # approximate alignment
angle.append(steering_angle[idxs])
speed.append(speed_value[idxs])
goods = np.abs(angle[-1]) <= 200
filters.append(np.argwhere(goods)[time_len-1:] + (lastidx+time_len-1))
lastidx += goods.shape[0]
# check for mismatched length bug
print("x {} | t {} | f {}".format(x.shape[0], steering_angle.shape[0], goods.shape[0]))
if x.shape[0] != angle[-1].shape[0] or x.shape[0] != goods.shape[0]:
raise Exception("bad shape")
except IOError:
import traceback
traceback.print_exc()
print "failed to open", tword
angle = np.concatenate(angle, axis=0)
speed = np.concatenate(speed, axis=0)
filters = np.concatenate(filters, axis=0).ravel()
print "training on %d/%d examples" % (filters.shape[0], angle.shape[0])
return c5x, angle, speed, filters, hdf5_camera
first = True
def datagen(filter_files, time_len=1, batch_size=256, ignore_goods=False):
"""
Parameters:
-----------
leads : bool, should we use all x, y and speed radar leads? default is false, uses only x
"""
global first
assert time_len > 0
filter_names = sorted(filter_files)
logger.info("Loading {} hdf5 buckets.".format(len(filter_names)))
c5x, angle, speed, filters, hdf5_camera = concatenate(filter_names, time_len=time_len)
filters_set = set(filters)
logger.info("camera files {}".format(len(c5x)))
X_batch = np.zeros((batch_size, time_len, 3, 160, 320), dtype='uint8')
angle_batch = np.zeros((batch_size, time_len, 1), dtype='float32')
speed_batch = np.zeros((batch_size, time_len, 1), dtype='float32')
while True:
try:
t = time.time()
count = 0
start = time.time()
while count < batch_size:
if not ignore_goods:
i = np.random.choice(filters)
# check the time history for goods
good = True
for j in (i-time_len+1, i+1):
if j not in filters_set:
good = False
if not good:
continue
else:
i = np.random.randint(time_len+1, len(angle), 1)
# GET X_BATCH
# low quality loop
for es, ee, x in c5x:
if i >= es and i < ee:
X_batch[count] = x[i-es-time_len+1:i-es+1]
break
angle_batch[count] = np.copy(angle[i-time_len+1:i+1])[:, None]
speed_batch[count] = np.copy(speed[i-time_len+1:i+1])[:, None]
count += 1
# sanity check
assert X_batch.shape == (batch_size, time_len, 3, 160, 320)
logging.debug("load image : {}s".format(time.time()-t))
print("%5.2f ms" % ((time.time()-start)*1000.0))
if first:
print "X", X_batch.shape
print "angle", angle_batch.shape
print "speed", speed_batch.shape
first = False
yield (X_batch, angle_batch, speed_batch)
except KeyboardInterrupt:
raise
except:
traceback.print_exc()
pass