/
lstm.py
241 lines (225 loc) · 10.2 KB
/
lstm.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
241
from __future__ import print_function
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.cross_validation import train_test_split
from sklearn.model_selection import cross_val_score, KFold, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from keras.models import Sequential, load_model
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from keras.wrappers.scikit_learn import KerasRegressor
from keras.layers.wrappers import TimeDistributed
from keras.layers import ELU
from keras.constraints import maxnorm
from keras.layers.core import RepeatVector, Activation, Dropout, Dense, Lambda, Flatten, TimeDistributedDense
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.recurrent import LSTM, GRU, SimpleRNN
from keras.wrappers.scikit_learn import KerasRegressor
from keras.constraints import maxnorm
from keras.layers.core import Activation, Dropout, Dense, LSTM
from keras.callbacks import LearningRateScheduler
from keras.layers.advanced_activations import PReLU
from keras.layers.normalization import BatchNormalization
from sklearn.preprocessing import MinMaxScaler
from keras.optimizers import SGD, RMSprop, Adadelta
from keras.utils.visualize_util import plot
from keras.utils import np_utils
from keras import backend as K
from skimage import transform, color, exposure
from skimage.transform import rotate
from skimage.viewer import ImageViewer
from imutils import paths
from common import anorm2, draw_str
from time import clock
import skimage as skimage
import numpy as np
import argparse
import sklearn
import pandas
import cv2
import video
import math
seed = 7
np.random.seed(seed) # for reproducibility
def step_decay(epoch):
<<<<<<< HEAD
initial_lrate = 0.1
drop = 0.5
epochs_drop = 10.0
lrate = initial_lrate * math.pow(drop, math.floor((1+epoch)/epochs_drop))
return float(lrate)
def baseline_model(optimizer='adam', init_mode='uniform'):
model = Sequential()
# model.add(Convolution2D(32, 2, 2, border_mode='valid', input_shape=(2, 32, 32), activation='relu'))
# model.add(Convolution2D(32, 1, 1, activation='relu'))
# model.add(MaxPooling2D(pool_size=(1, 1), dim_ordering="tf"))
# model.add(Dropout(0.15))
# # model.add(TimeDistributed(1536, input_dim=6144, init=init_mode, activation="relu"))
# # model.add(LSTM(512, return_sequences=False))
# # model.add(LSTM(256, return_sequences=False))
# model.add(Flatten())
# model.add(Dense(1))
model.add(Lambda(
lambda x: x/127.5 - 1.,
input_shape=(2, 480, 640),
output_shape=(2, 480, 640))
)
# Several convolutional layers, each followed by ELU activation
# 8x8 convolution (kernel) with 4x4 stride over 16 output filters
# model.add(Convolution2D(16, 8, 8, subsample=(4, 4), border_mode="same"))
# model.add(ELU())
# # 5x5 convolution (kernel) with 2x2 stride over 32 output filters
# model.add(Convolution2D(32, 5, 5, subsample=(2, 2), border_mode="same"))
# model.add(ELU())
# # 5x5 convolution (kernel) with 2x2 stride over 64 output filters
# model.add(Convolution2D(64, 5, 5, subsample=(2, 2), border_mode="same"))
# # Flatten the input to the next layer
# model.add(Flatten())
# # Apply dropout to reduce overfitting
# model.add(Dropout(.2))
# model.add(ELU())
# # Fully connected layer
# model.add(Dense(512))
# # More dropout
# model.add(Dropout(.2))
# model.add(ELU())
# # Fully connected layer with one output dimension (representing the speed).
# model.add(Dense(1))
model.add(Convolution2D(64, 3, 3, border_mode='same', activation='relu', input_shape=(2, 480, 640)))
model.add(BatchNormalization(mode=0, axis=1))
model.add(Convolution2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(BatchNormalization(mode=0, axis=1))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
model.add(Convolution2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(BatchNormalization(mode=0, axis=1))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.3))
model.add(LSTM(512, return_sequences=True))
model.add(TimeDistributed(Dropout(0.5)))
model.add(TimeDistributed(Dense(128, activation='relu')))
model.add(Dense(1))
model.compile(optimizer="adam", loss="mse")
plot(model, to_file='model_lstm_cnn.png')
print("[INFO] compiling uniform batch normalization model...")
initial_lrate = 0.1
drop = 0.5
epochs_drop = 10.0
lrate = initial_lrate * math.pow(drop, math.floor((1+epoch)/epochs_drop))
return float(lrate)
def baseline_model(optimizer='adam', init_mode='uniform'):
model = Sequential()
model.add(Dense(1536, input_dim=6144, init=init_mode, activation="relu"))
model.add(BatchNormalization())
#model.add(Dropout(0.20))
model.add(Dense(768, init=init_mode, activation="relu"))
model.add(BatchNormalization())
#model.add(Dropout(0.20))
model.add(Dense(1))
print("[INFO] compiling uniform batch normalization model...")
model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['accuracy'])
#sgd = SGD(lr=0.0, momentum=0.9, decay=0.0, nesterov=False)
#model.compile(loss='mean_squared_logarithmic_error', optimizer=sgd, metrics=['accuracy'])
return model
class App:
def __init__(self, video_src):
self.cap = cv2.VideoCapture(video_src)
self.frames = []
self.data = []
self.speeds = []
self.X_train = []
self.Y_train = []
self.X_test = []
self.Y_test = []
self.model = None
self.dataset = dict(pandas.read_json("drive.json"))
self.frame_idx = 0
def draw_flow(self, img, flow, step=20):
h, w = img.shape[:2]
y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1).astype(int)
fx, fy = flow[y,x].T
lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2)
lines = np.int32(lines + 0.5)
vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.polylines(vis, lines, 0, (0, 255, 0))
for (x1, y1), (x2, y2) in lines:
cv2.circle(vis, (x1, y1), 1, (0, 255, 0), -1)
return vis
def get_flow(self):
print("[INFO] constructing flow matrices and speed truth...")
while (self.cap.isOpened()):
ret, frame = self.cap.read()
# Rexposes, blurs, and makes frame grayscale in order to smoothen it out.
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rescale_gray = skimage.exposure.rescale_intensity(gray,out_range=(0,255))
blur_gray = cv2.GaussianBlur(rescale_gray, (21, 21), 0)
self.frames.append(blur_gray)
if (self.frame_idx > 0):
prev_frame, curr_frame = self.frames[self.frame_idx-1], self.frames[self.frame_idx]
flow_matrix = cv2.calcOpticalFlowFarneback(prev_frame, curr_frame, 0.5, 3, 15, 2, 5, 1.2, 0)
self.data.append(flow_matrix)
# features = cv2.resize(flow_matrix, (64, 48)).flatten()
# self.data.append(features)
speed = self.dataset[1][self.frame_idx]
self.speeds.append(speed)
cv2.imshow('flow', self.draw_flow(gray, flow_matrix))
self.frame_idx += 1
else:
self.cap.release()
cv2.destroyAllWindows()
ch = 0xFF & cv2.waitKey(1)
if ch == 27:
break
def construct_data(self):
self.data = np.array(self.data)
self.speeds = np.array(self.speeds)
scale = MinMaxScaler(feature_range=(0, 1))
ascolumns = self.data.reshape(-1, 2)
dataset = scale.fit_transform(ascolumns)
dataset = dataset.reshape(self.data.shape)
(self.X_train, self.X_test, self.Y_train, self.Y_test) = train_test_split(dataset, self.speeds, test_size=0.15, random_state=42)
# self.X_train = scale.fit_transform(self.X_train)
# # self.X_test = scale.transform(self.X_test)
self.X_train = self.X_train.reshape(self.X_train.shape[0], 2, 480, 640).astype('float32')
self.X_test = self.X_test.reshape(self.X_test.shape[0], 2, 480, 640).astype('float32')
# dataset = scaler.fit_transform(self.data)
# (self.X_train, self.X_test, self.Y_train, self.Y_test) = train_test_split(self.data, self.speeds, test_size=0.15, random_state=42)
# self.X_train = scale.fit_transform(self.X_train)
# self.X_test = scale.transform(self.X_test)
def train_model(self):
lrate = LearningRateScheduler(step_decay)
callbacks_list = [lrate]
print(self.X_train.shape)
self.model = baseline_model()
#self.model = KerasRegressor(build_fn=baseline_model, nb_epoch=150, batch_size=60, verbose=1)
self.model.fit(self.X_train, self.Y_train, nb_epoch=50, batch_size=64, validation_split=0.15, shuffle=True, callbacks=callbacks_list, verbose=1)
# self.model.save('lstm_model.h5')
self.model = baseline_model()
#self.model = KerasRegressor(build_fn=baseline_model, nb_epoch=150, batch_size=60, verbose=1)
self.model.fit(self.X_train, self.Y_train, nb_epoch=150, batch_size=64, validation_split=0.15, shuffle=True, callbacks=callbacks_list, verbose=1)
self.model.save('model.h5')
def evaluate_model(self):
print("[INFO] evaluating on testing set...")
(loss, accuracy) = self.model.evaluate(self.X_test, self.Y_test, batch_size=64, verbose=1)
print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100))
output = self.model.predict(self.X_test)
print(output)
for i in range(0, len(output)):
print("actual: %f, model: %f" % (self.Y_test[i], output[i]))
score = mean_squared_error(self.Y_test, output)
print("SCORE: %f" % score)
def main():
import sys
video_src = "drive.mp4"
app = App(video_src)
app.get_flow()
app.construct_data()
app.train_model()
app.evaluate_model()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()