-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
302 lines (258 loc) · 9.28 KB
/
main.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import Adam
from keras.models import load_model
from tensorflow.python.client import device_lib
from keras import backend as K
import tensorflow as tf
import cv2
from imageElaboration import *
# Setting up a Keras model of: 4 Conv and Pool + Flat + 5 Dense
def setupNvidiaModel(inputShape):
model = Sequential()
# Convolution Layer 1
convLayer = Conv2D(filters=12,
kernel_size=(5, 5),
strides=(1, 1),
activation='elu',
input_shape=inputShape)
model.add(convLayer)
# Pooling Layer 1
poolingLayer = MaxPooling2D(pool_size=(2, 2),
strides=(2, 2))
model.add(poolingLayer)
# Convolution Layer 2
convLayer = Conv2D(filters=24,
kernel_size=(5, 5),
activation='elu')
model.add(convLayer)
# Pooling Layer 2
poolingLayer = MaxPooling2D(pool_size=(2, 2),
strides=(2, 2))
model.add(poolingLayer)
# Convolution Layer 3
convLayer = Conv2D(filters=36,
kernel_size=(3, 3),
activation='elu')
model.add(convLayer)
# Pooling Layer 3
poolingLayer = MaxPooling2D(pool_size=(2, 2),
strides=(2, 2))
model.add(poolingLayer)
# Convolution Layer 4
convLayer = Conv2D(filters=48,
kernel_size=(3, 3),
activation='elu')
model.add(convLayer)
# Pooling Layer 4
poolingLayer = MaxPooling2D(pool_size=(2, 2),
strides=(2, 2))
model.add(poolingLayer)
# Flatten
model.add(Flatten())
# Dense layer 1
denseLayer = Dense(1164,
activation='elu')
model.add(denseLayer)
# Dense layer 2
denseLayer = Dense(100,
activation='elu')
model.add(denseLayer)
# Dense layer 3
denseLayer = Dense(50,
activation='elu')
model.add(denseLayer)
# Dense layer 4
denseLayer = Dense(10,
activation='elu')
model.add(denseLayer)
# Dense layer 5
denseLayer = Dense(1)
model.add(denseLayer)
# Compilation
model.compile(Adam(lr=0.001),
loss="mse",
metrics=["mse"])
return model
def setupTestModel(inputShape):
model = Sequential()
# Adding the first convolutional layer
convLayer = Conv2D(filters=16,
kernel_size=(5, 5),
strides=(1, 1),
activation='relu',
input_shape=inputShape)
model.add(convLayer)
# First pooling
poolingLayer = MaxPooling2D(pool_size=(2, 2),
strides=(2, 2))
model.add(poolingLayer)
# Adding the second convolutional layer
convLayer = Conv2D(filters=32,
kernel_size=(5, 5),
activation='relu')
model.add(convLayer)
# Adding the second pooling layer
poolingLayer = MaxPooling2D(pool_size=(2, 2))
model.add(poolingLayer)
# Flatten
model.add(Flatten())
# Dense layer 1
denseLayer = Dense(100,
activation='relu')
model.add(denseLayer)
# Dense layer 2
denseLayer = Dense(1)
model.add(denseLayer)
# Compilation
model.compile(Adam(lr=0.001),
loss="mse",
metrics = ["mse"])
return model
############ IMPORTANT VARIABLES ###########
batchSize = 500
############################################
# Reading all the speed ground truths
print("Reading speed ground truths")
file = open("./sourceData/train.txt")
speedTruthArrayString = file.readlines()
speedTruthArray = []
for numeric_string in speedTruthArrayString:
numeric_string = numeric_string.strip('\n')
speedTruthArray.append(float(numeric_string))
file.close()
print("Read " + str(len(speedTruthArray)) + " values")
# Uncomment to check if GPU is correcetly detected
# print(device_lib.list_local_devices())
# print(K.tensorflow_backend._get_available_gpus())
# GPU settings to allow memory growth
# config = tf.ConfigProto()
# config.gpu_options.allow_growth = True
# config.log_device_placement=True
# sess = tf.Session(config=config) #With the two options defined above
# Opening training video
videoFeed = cv2.VideoCapture('./sourceData/train.mp4')
videoLengthInFrames = int(videoFeed.get(cv2.CAP_PROP_FRAME_COUNT))
# Preparing for validation data retrieval
validationSize = int(videoLengthInFrames * 0.15)
validationGap = int(videoLengthInFrames/validationSize)
# Iterating through all couples of frames of the video
coupleCounter = 0
frameCoupleArray = []
frameCounter = 0
batchFrames = []
batchSpeeds = []
evalFrames = []
evalSpeeds = []
flow_mat = None
image_scale = 0.5
nb_images = 1
win_size = 15
nb_iterations = 2
deg_expansion = 5
STD = 1.3
while(coupleCounter < videoLengthInFrames-1):
# Read a couple of new frames from the video feed
ret2, newFrame = videoFeed.read()
# Elaborating image
newFrameROI = elaborateImage(newFrame)
# Calculating the optical flow
if coupleCounter == 0:
# If this is the first frame...
oldFrameROI = newFrameROI
oldFrame = newFrame
flow = cv2.calcOpticalFlowFarneback(oldFrameROI, newFrameROI,
flow_mat,
image_scale,
nb_images,
win_size,
nb_iterations,
deg_expansion,
STD,
0)
#flow = opticalFlowDense(oldFrame, newFrame)
# Also, set up the CNN model
flowShape = flow.shape
model = setupNvidiaModel(flowShape)
else:
flow = cv2.calcOpticalFlowFarneback(oldFrameROI, newFrameROI,
flow_mat,
image_scale,
nb_images,
win_size,
nb_iterations,
deg_expansion,
STD,
0)
#flow = opticalFlowDense(oldFrame, newFrame)
# Check if this frame is for training or validation
if frameCounter == validationGap:
frameCounter = 0
evalFrames.append(flow)
evalSpeeds.append(speedTruthArray[coupleCounter])
else:
# Saving the couple of data and label for training
batchFrames.append(flow)
batchSpeeds.append(speedTruthArray[coupleCounter])
# Incrementing couples counter and swapping frames
oldFrameROI = newFrameROI
oldFrame = newFrame
coupleCounter = coupleCounter + 1
frameCounter = frameCounter + 1
cv2.imshow('frame',draw_flow(newFrameROI,flow))
cv2.waitKey(1)
print(str(coupleCounter))
# Shuffling data before training
# For training
print("\n\n\n###############################\nSHUFFLING MODEL\n")
unified = list(zip(batchFrames, batchSpeeds))
np.random.shuffle(unified)
batchFrames, batchSpeeds = zip(*unified)
# For validation
unified = list(zip(evalFrames, evalSpeeds))
np.random.shuffle(unified)
evalFrames, evalSpeeds = zip(*unified)
# Training model
print("\n\n\n###############################\nTRAINING MODEL\n")
index = 0
trainBatchFrame = []
trainBatchSpeed = []
frameCounter = 0
while(index < len(batchSpeeds)):
# Forming batch
trainBatchFrame.append(batchFrames[index])
trainBatchSpeed.append(batchSpeeds[index])
# Training batch
index = index + 1
frameCounter = frameCounter + 1
if frameCounter == batchSize or index==(len(batchSpeeds)-1) :
print("\nWe are at " + str(index) + "\n")
# Preparing data
X = np.array(trainBatchFrame)
Y = np.array(trainBatchSpeed)
#with tf.device('/cpu:0'): #Uncomment to use CPU instead of GPU
model.fit(x=X,
y=Y,
verbose=1,
epochs=15,
batch_size=32,
shuffle=True
)
# Resetting counter and x and y arrays
frameCounter = 0
trainBatchFrame = []
trainBatchSpeed = []
# Saving the trained model
model.save('speed_model.h5') # creates a HDF5 file 'speed_model.h5'
# Evaluation of the model
print("\n\n\n#########################################\nEVALUATION OF THE MODEL\n")
X = np.array(evalFrames)
Y = np.array(evalSpeeds)
scores = model.evaluate(X, Y, verbose=1)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
print(str(scores))
videoFeed.release()
cv2.destroyAllWindows()