forked from fubarlabs/autonomous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepdrive.py
240 lines (198 loc) · 7.21 KB
/
deepdrive.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
import math
import numpy as np
import h5py
import glob
from tqdm import tqdm
import scipy
import keras
from keras.models import Sequential, Model
from keras.layers.core import Dense, Dropout, Activation, Flatten, Reshape
from keras.layers import Embedding, Input, merge, ELU
from keras.layers.recurrent import SimpleRNN, LSTM
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD, Adam, RMSprop
import sklearn.metrics as metrics
import matplotlib.pyplot as plt
plt.ion()
# Read in deep drive data
dfiles = glob.glob('data/*.h5')
# 999 data points, images are 3x227x227
# vehicle_states are just target at previous time
# no distance finder, but this is enough to work with
dfile = dfiles[9]
h5f = h5py.File(dfile,'r')
A = dict(h5f.items())
# view image
plt.imshow(A['images'].value[0].swapaxes(0,2).swapaxes(0,1))
AA = A['images'].value
# look at targets
A['targets'].value[:2]
A['vehicle_states'].value[:2]
at = A['targets'].value[:-1]
av = A['vehicle_states'].value[1:]
# slim inputs
B = np.array(A['images'].value,dtype=np.uint8)
plt.imshow(B[0][::-1,:,:].transpose((1,2,0)))
B = B[:,::-1,:,:]
bb = scipy.misc.imresize(B[0],(128,128),'cubic','RGB')
plt.imshow(bb)
bb = scipy.misc.imresize(B[0],(64,64),'cubic','RGB')
plt.imshow(bb)
# determine scaling parameters
# speed and accel
speedmax = None
speedmin = None
accelmax = None
accelmin = None
for dfile in dfiles:
with h5py.File(dfile,'r') as h5f:
# raw data
A = dict(h5f.items())
smx = np.max(A['vehicle_states'].value[:,2])
smn = np.min(A['vehicle_states'].value[:,2])
amx = np.max(A['vehicle_states'].value[:,3])
amn = np.min(A['vehicle_states'].value[:,3])
if speedmax is None or smx > speedmax:
speedmax = smx
if speedmin is None or smn < speedmin:
speedmin = smn
if accelmax is None or amx > accelmax:
accelmax = amx
if accelmin is None or amn < accelmin:
accelmin = amn
#plt.plot(A['targets'].value[:,3],A['targets'].value[:,5],'.')
# throttle was supposed to be zero to 1, but has negative values, probably -1 to 1
# steering is nominally -1 to 1, but doesn't go below zero?
throttlemax = None
throttlemin = None
steermax = None
steermin = None
for dfile in dfiles:
with h5py.File(dfile,'r') as h5f:
# raw data
A = dict(h5f.items())
smx = np.max(A['targets'].value[:,4])
smn = np.min(A['targets'].value[:,4])
tmx = np.max(A['targets'].value[:,5])
tmn = np.min(A['targets'].value[:,5])
if steermax is None or smx > steermax:
steermax = smx
if steermin is None or smn < steermin:
steermin = smn
if throttlemax is None or tmx > throttlemax:
throttlemax = tmx
if throttlemin is None or tmn < throttlemin:
throttlemin = tmn
ndata = 0
imgsize = 64
# frame size
nrows = 64
ncols = 64
# speed, accel, distance, angle
real_in = Input(shape=(2,), name='real_input')
# video frame in, grayscale
frame_in = Input(shape=(3,nrows,ncols))
# convolution for image input
conv1 = Convolution2D(8,5,5,border_mode='same')
conv_l1 = conv1(frame_in)
Econv_l1 = ELU()(conv_l1)
pool_l1 = MaxPooling2D(pool_size=(2,2))(Econv_l1)
conv2 = Convolution2D(8,5,5,border_mode='same')
conv_l2 = conv2(pool_l1)
Econv_l2 = ELU()(conv_l2)
pool_l2 = MaxPooling2D(pool_size=(2,2))(Econv_l2)
flat = Flatten()(pool_l2)
M = merge([flat,real_in], mode='concat', concat_axis=1)
D1 = Dense(64)(M)
ED1 = ELU()(D1)
D2 = Dense(32)(ED1)
ED2 = ELU()(D2)
D3 = Dense(32)(ED2)
ED3 = ELU()(D3)
A1 = Dense(32)(ED3)
EA1 = ELU()(A1)
S1 = Dense(32)(ED3)
ES1 = ELU()(S1)
Accel = Dense(1, activation='sigmoid')(EA1)
Steer = Dense(1, activation='sigmoid')(ES1)
model = Model(input=[real_in, frame_in], output=[Accel,Steer])
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['accuracy'])
# Fake data
#nsamples = 1000
#fake_real = np.random.random((nsamples,2))
#fake_frame = np.random.random((nsamples,3,nrows,ncols))
#fake_A = np.random.random(nsamples)
#fake_P = np.random.random(nsamples)
#h = model.fit([fake_real, fake_frame], [fake_A, fake_P],
# batch_size = 32, nb_epoch=10, verbose=1,
# validation_split=0.1)
#h = model.predict([fake_real, fake_frame],
# batch_size = 32, verbose=1)
# this produces distinct values
# batch process (fitting, really) one file at a time
for dfile in dfiles:
with h5py.File(dfile,'r') as h5f:
# raw data
A = dict(h5f.items())
# extract images in 1-byte format
B = np.array(A['images'].value,dtype=np.float16)/255.
# change BGR to RGB
B = B[:,::-1,:,:]
# Scale down image size
imgs = np.zeros((len(B),3,64,64),dtype=np.float16)
for i,b in enumerate(B):
imgs[i] = scipy.misc.imresize(b,(64,64),'cubic','RGB').transpose((2,0,1))
# speed and accel scale
speedx = A['vehicle_states'].value[:,2:4]
speedx[:,0] = (speedx[:,0] - speedmin) / (speedmax-speedmin)
speedx[:,1] = (speedx[:,1] - accelmin) / (accelmax-accelmin)
# throttle and steering scale
targets = (A['targets'].value[:,4:] + 1) / 2.
# Train while we have this file open
h = model.fit([speedx, imgs], [targets[:,1], targets[:,0]],
batch_size = 32, nb_epoch=10, verbose=1,
validation_split=0.1)
W = model.get_weights()
# look at conv filters separately in color channel
f, con = plt.subplots(4,3, sharex='col', sharey='row')
for row in range(4):
for col in range(3):
con[row,col].pcolormesh(W[0][row,col],cmap=plt.cm.hot)
# combine color channels into on filter image
f, con = plt.subplots(4,1, sharex='col', sharey='row')
for row in range(4):
con[row].imshow(W[0][row].transpose((1,2,0)),
interpolation="none")
# View the correct and predicted steering angle and accel
# Draw a line from center to outside, length is accel, angle is steering
all_pred = list()
for dfile in dfiles:
with h5py.File(dfile,'r') as h5f:
# raw data
A = dict(h5f.items())
# extract images in 1-byte format
B = np.array(A['images'].value,dtype=np.uint8)/255.
# change BGR to RGB
B = B[:,::-1,:,:]
# Scale down image size
imgs = np.zeros((len(B),3,64,64),dtype=np.uint8)
for i,b in enumerate(B):
imgs[i] = scipy.misc.imresize(b,(64,64),'cubic','RGB').transpose((2,0,1))
# speed and accel
speedx = A['vehicle_states'].value[:,2:4]
speedx[:,0] = (speedx[:,0] - speedmin) / (speedmax-speedmin)
speedx[:,1] = (speedx[:,1] - accelmin) / (accelmax-accelmin)
# throttle and steering scale
targets = (A['targets'].value[:,4:] + 1) / 2.
# Train while we have this file open
h = model.predict([speedx, imgs],
batch_size = 32, verbose=1)
all_pred.append(h)
# Make sure we get a variety of values
plt.plot([np.array([p[0][i] for p in all_pred]).reshape(11) for i in range(999)],'.')
plt.plot([np.array([p[1][i] for p in all_pred]).reshape(11) for i in range(999)],'.')
# Should look at intermediate values to see where this goes wrong
# throttle and steering may have been mixed up