-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
355 lines (271 loc) · 13.9 KB
/
model.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#Importing Builtins
import os
import argparse
def parse_arguments():
#Initilizing ArgumentParser() object
parser = argparse.ArgumentParser(description='Train using either Alexnet Architecture or Nvidia Architecture')
#Give the model architecture name , default is "alexnet"
parser.add_argument('--model_arch',type=str,default='alexnet',help="Either 'nvidia' architecture Or 'alexnet' arch")
#Give the path of the data directory,default is the working directory
parser.add_argument('--datadir',type=str,default='.',help='Specify your data path')
#Specify the size of validation set,default is 0.2
parser.add_argument('--validation_size',type=float,default=0.2,help='Size of validation set , default is 0.2')
parser.add_argument('--batch_size',type=int,default=128,help='Specify your batch size,default 128.')
#Specify the number of epochs you want to train, default is 5
parser.add_argument('--epochs',type=int,default=5,help='Number of epochs you want to train. Default is 1.')
#Specify the activation function default is 'elu'
parser.add_argument('--activation',type=str,default='elu',help='Give your activation function. Default is elu.')
return parser
arg_parser = parse_arguments()
args = arg_parser.parse_args()
#Retreiving the parameters from the command line arguments
activation = args.activation
EPOCHS = args.epochs
BATCH_SIZE = args.batch_size
DATA_DIR = args.datadir
model_arch = args.model_arch
saved_model_path = args.saved_model
test_size = args.validation_size
#Function for checking the arguments
def argument_checker():
if model_arch not in ['nvidia','alexnet']:
raise ValueError('Model architecture must be one of "nvidia" or "alexnet"')
if RETRAIN:
if not os.path.exists(saved_model_path):
raise FileNotFoundError('Specified Path not Found')
if not os.path.exists(os.path.join(DATA_DIR,'IMG')) or not os.path.exists(os.path.join(DATA_DIR,'driving_log.csv')):
raise FileNotFoundError('Data directory path must contain IMG directory and driving_log.csv file')
if activation not in ['elu','relu','sigmoid','tanh','linear']:
raise ValueError('Activation must be one of elu,relu,sigmoid,tanh,linear')
#Cheeck the arguments passed
argument_checker()
#import libraries for number crunching and data exploration
import numpy as np
import pandas as pd
#import libraries for data visualization
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
#importing the deep learning library
import keras
#keras version checker
if keras.__version__!= '2.0.6':
print('This model is developed using keras version 2.0.6')
print('Previous versions may not work properly and versions 1.x.x will not work.')
from keras import backend as K
from keras.optimizers import Adam
from keras.models import Sequential
from keras.models import Model
from keras.models import load_model
from keras.layers import Dense
from keras.layers import Activation
from keras.layers import Conv2D
from keras.layers import Dropout
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Lambda
from keras.layers import Cropping2D
from keras.layers import BatchNormalization
from keras.callbacks import ModelCheckpoint
from keras.callbacks import TensorBoard
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
IMG_DIR = os.path.join(DATA_DIR,'IMG')
CSV_LOG_PATH = os.path.join(DATA_DIR,'driving_log.csv')
#Callback functions to be implemented later at training step
#Tensorboard visualization features added.
model_checkpt = ModelCheckpoint('weights.{epoch:02d}-{val_loss:.2f}.hdf5')
tensor_board = TensorBoard(log_dir='./logdir/',histogram_freq=1,batch_size=32,write_grads=True)
#Correction Factor for left and right images
CF = 0.27
#Learning rate
LR = 1e-3
#Learning rate decay
DECAY = 0.99
#Reading the csv file
samples = pd.read_csv(CSV_LOG_PATH)
samples = pd.read_csv(CSV_LOG_PATH,header=None)
columns = ['center','left','right','steering','throttle','brake','speed']
samples.columns = columns
# as the recorded image names in driving log file contains the whole directory path
#function for removing the directory path
def path_remover(path):
return path.split('/')[-1]
samples.center = list(map(path_remover,samples.center))
samples.left = list(map(path_remover,samples.left))
samples.right = list(map(path_remover,samples.right))
print(samples.head())
steering_center = samples.steering
steering_left = steering_center + CF
steering_right = steering_center - CF
steering_angle = np.concatenate((steering_center,steering_left,steering_right))
center_images = list(samples.center)
left_images = list(samples.left)
right_images = list(samples.right)
sample_images = []
sample_images += center_images
sample_images += left_images
sample_images += right_images
#adding the flipped images with prefix flipped_ to the original image names
#at time of training generator function can understand which image is to flip
flipped_images = list(map(lambda x:'flipped_'+ x,sample_images))
sample_images = sample_images + flipped_images
steering_angle = np.concatenate((steering_angle,steering_angle*(-1)))
X_data = sample_images
y_data = steering_angle
#splitting the training data into training and validation split
X_train,X_validation,y_train,y_validation = train_test_split(X_data,y_data,test_size=test_size)
#shuffling the training data
X_train,y_train = shuffle(X_train,y_train)
#function for flipping the image
def flipped(data):
return np.flip(data,axis=1)
#generator function for feeding the training operation for a fixed batch size
def generator(data,batch_size):
num_samples = len(data['X'])
xdata,ydata = data['X'],data['y']
while True:
for offset in range(0,num_samples,batch_size):
xbatch_samples = xdata[offset:offset+batch_size]
ybatch_samples = ydata[offset:offset+batch_size]
images = []
for batch_sample in xbatch_samples:
if batch_sample.startswith('flipped_'):
img = mpimg.imread(os.path.join(IMG_DIR,batch_sample.replace('flipped_','')))
img = flipped(img)
images.append(img)
else:
img = mpimg.imread(os.path.join(IMG_DIR,batch_sample))
images.append(img)
X_batch = np.array(images)
y_batch = ybatch_samples*(-1)
yield shuffle(X_batch,y_batch)
train_data = {'X':X_train,'y':y_train}
validation_data = {'X':X_validation,'y':y_validation}
train_generator = generator(train_data,batch_size=BATCH_SIZE)
validation_generator = generator(validation_data,batch_size=BATCH_SIZE)
#The ModelArch class which contains the both model methods nvidia_arch() and alexnet_arch()
class ModelArch(object):
"""This model class contains two architecture:
1. Nvidia Architecture:
2. Alexnet Architecture:
"""
def __init__(self,model_arch='nvidia'):
self.model_arch = model_arch
def nvidia_arch(self,activation):
"""This model contains these layers as folows :
Preprocessing layers :
---------------------
(1) First a cropping layer which cropped out the unnecessary background from the input image.
(2) This layer is lambda layer which resized the images to 66x200. Which is the required input
shape for this architecture.For resizing the nearest neighbor approach is used.
(3) Third layer is the normalizing layer which is also a lambda layer.
Convolution layers:
-------------------
There are total 5 convolution layers and all of which have valid padding.This model is trained
using activation function 'elu'.
first_layer : 24 filters with size of 5x5 with strides 2x2
second_layer: 36 filters with size of 5x5 with strides 2x2
third_layer: 48 filters with size of 5x5 with strides 2x2
fourth_layer: 64 filters with size of 5x5 with strides 1x1
fifth_layer: 64 filters with size of 5x5 with strides 1x1
Fully Connected layers:
-----------------------
A total of 4 fully connected layers with output sizes 100,50,10,1 respectively applied.
The last layer is the output layer.
So no activation is applied.
"""
model = Sequential()
model.add(Cropping2D(cropping=((65,40),(0,0)),
input_shape=(160,320,3)))
model.add(Lambda(lambda x:K.tf.image.resize_images(x,(66,200),
method=K.tf.image.ResizeMethod.NEAREST_NEIGHBOR)))
model.add(Lambda(lambda x:((x-K.mean(x))/K.std(x))))
model.add(Conv2D(24,(5,5),strides=(2,2),activation=activation,padding='valid'))
model.add(Conv2D(36,(5,5),strides=(2,2),activation=activation,padding='valid'))
model.add(Conv2D(48,(5,5),strides=(2,2),activation=activation,padding='valid'))
model.add(Conv2D(64,(3,3),strides=(1,1),activation=activation,padding='valid'))
model.add(Conv2D(64,(3,3),strides=(1,1),activation=activation,padding='valid'))
model.add(Flatten())
model.add(Dense(100,activation=activation))
model.add(Dense(50,activation=activation))
model.add(Dense(10,activation=activation))
model.add(Dense(1))
return model
def alexnet_arch(self,activation):
"""
Orignial Alexnet Architecture popularize the use of 'relu' but here 'elu' is used.
I have also ommitted the last three fully connected layers to fit the output .
This model contains these layers as folows :
Preprocessing layers :
---------------------
(1) First a cropping layer which cropped out the unnecessary background from the input image.
(2) This layer is lambda layer which resized the images to 224x224. Which is the required input
shape for this architecture.For resizing the nearest neighbor approach is used.
(3) Third layer is the normalizing layer which is also a lambda layer.
Convolution layers:
-------------------
Here all paddings are same.
In first conv layer 96 11x11 filters with strides 4x4 is applied followed by a Batch normalization layer
and a MaxPooling layer with pool size 3x3 and strides 2x2 .
In second conv layer 256 5x5 filters with strides 1x1 is applied followed by a Batch normalization layer
and a MaxPooling layer with pool size 3x3 and strides 2x2 .
In third, fourth, and fifth conv layer 384,384,256 3x3 filters with strides 1x1 is applied respectively,
followed by a MaxPooling layer with pool size 3x3 and strides 2x2 .
Fully Connected layers:
-----------------------
A total of 4 fully connected layers with output sizes 100,50,10,1 respectively applied.
The last layer is the output layer.
So no activation is applied.
"""
alexnet_model = Sequential()
alexnet_model.add(Cropping2D(cropping=((65, 40), (0, 0)),
input_shape=(160, 320, 3)))
alexnet_model.add(Lambda(lambda x: K.tf.image.resize_images(x, (224, 224),
method=K.tf.image.ResizeMethod.NEAREST_NEIGHBOR)))
alexnet_model.add(Lambda(lambda x: ((x - K.mean(x)) / K.std(x))))
alexnet_model.add(Conv2D(96,(11,11),strides=(4,4),padding='same',activation=activation))
alexnet_model.add(BatchNormalization())
alexnet_model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
alexnet_model.add(Conv2D(256, (5, 5), strides=(1, 1), padding='same', activation=activation))
alexnet_model.add(BatchNormalization())
alexnet_model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
alexnet_model.add(Conv2D(384, (3, 3), strides=(1, 1), padding='same', activation=activation))
alexnet_model.add(Conv2D(384, (3, 3), strides=(1, 1), padding='same', activation=activation))
alexnet_model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation=activation))
alexnet_model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
alexnet_model.add(Flatten())
alexnet_model.add(Dense(100, activation=activation))
alexnet_model.add(Dense(50, activation=activation))
alexnet_model.add(Dense(10, activation=activation))
alexnet_model.add(Dense(1))
return alexnet_model
#Initializing the adam optimizer with learning rate and decay parameter
adam = Adam(lr=LR,decay=DECAY)
if model_arch == 'nvidia':
model = ModelArch('nvidia')
model = model.nvidia_arch(activation)
model.compile(loss='mse', optimizer=adam)
elif model_arch == 'alexnet':
model = ModelArch('alexnet')
model = model.alexnet_arch(activation)
model.compile(loss='mse', optimizer=adam)
print(model.summary())
#creating a history object for visualization later
history_object = model.fit_generator(train_generator,
steps_per_epoch=len(X_train)/BATCH_SIZE,
callbacks=[model_checkpt,tensor_board],
validation_data=validation_generator,
validation_steps=len(X_validation)/BATCH_SIZE,
epochs=EPOCHS)
#For tensor board visualization go to terminal and type $ tensorboard --logdir=$[PATH_TO_YOUR_LOGDIR]
#Saving the model
model.save('model'+'_'+model_arch+'_'+str(EPOCHS)+'.h5')
print(history_object.history.keys())
#Loss visualization
plt.plot(history_object.history['loss'])
plt.plot(history_object.history['val_loss'])
plt.title('model mean squared error loss')
plt.ylabel('mean squared error loss')
plt.xlabel('epoch')
plt.legend(['training set','validation set'],loc='upper right')
plt.show()