forked from udacity/CarND-Behavioral-Cloning-P3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
behav_clone_updated_model.py
193 lines (156 loc) · 6.21 KB
/
behav_clone_updated_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
import csv
import cv2
import numpy as np
from scipy import ndimage
from sklearn.model_selection import train_test_split
def process_image(img):
print("In process image")
"""
# Code to combine Udacity and PK data
lines = []
my_data = ['/home/workspace', '/home/workspace/datafiles/erratic_correct', '/home/workspace/datafiles/erratic_correct2', '/home/workspace/datafiles/erratic_correct3']
udacity_data = ['/home/workspace/datafiles/datas']
for list in [my_data, udacity_data]:
print("List Values", list)
for filename in list:
with open(filename + '/driving_log.csv') as file:
for line in csv.reader(file):
if 'IMG' in line[0]:
lines.append(line)
"""
lines = []
#with open('../0806/driving_log.csv') as csvfile:
#with open('../good/driving_log.csv') as csvfile:
#with open('../sim_full_good/driving_log.csv') as csvfile:
with open('../orig_data/data/driving_log.csv') as csvfile:
#with open('../training_data/sim_ml/driving_log.csv') as csvfile:
reader = csv.reader(csvfile)
next(reader)
print("B4 for")
for line in reader:
lines.append(line)
#print("Lines size : ", len(lines))
images = []
car_images = []
images_center = []
images_left = []
images_right = []
measurements = []
steering_angles = []
steerings_centered = []
steerings_left = []
steerings_right = []
for line in lines:
source_path = line[0]
filename = source_path.split('/')[-1]
#print("File : ", filename)
#current_path = '../0806/IMG/' + filename
#image_path = '../0806/IMG/'
#current_path = '../good/IMG/' + filename
#image_path = '../good/IMG/'
#current_path = '../sim_full_good/IMG/' + filename
#image_path = '../sim_full_good/IMG/'
current_path = '../orig_data/data/IMG/' + filename
image_path = '../orig_data/data/IMG/'
#current_path = '../training_data/sim_ml/IMG/' + filename
#print("Current Path : ", current_path)
image = cv2.imread(current_path)
#image = ndimage.imread(current_path)
images.append(image)
measurement = float(line[3])
measurements.append(measurement)
#print("File : Measurement : ", filename, measurement)
steering_center = float(line[3])
# create adjusted steering measurements for the side camera images
#correction = 0.4 # this is a parameter to tune
correction = 0.2 # this is a parameter to tune
steering_left = steering_center + correction + 0.2
steering_right = steering_center - correction
# print(steering_left, steering_right)
steerings_centered.append(steering_center)
steerings_left.append(steering_left)
steerings_right.append(steering_right)
# read in images from center, left and right cameras
#path = '../IMG/' # fill in the path to your training IMG directory
#center_cam_fname = line[0].split('/')[-1]
#print("Center - ", center_cam_fname)
#print("File : ", image_path + line[0].split('/')[-1])
#print("Center File : ", line[0].split('/')[-1])
img_center = cv2.imread(image_path + line[0].split('/')[-1])
img_left = cv2.imread(image_path + line[1].split('/')[-1])
img_right = cv2.imread(image_path + line[2].split('/')[-1])
#print("img_center : ", img_center.shape)
#print("img_left : ", img_left.shape)
#print("img_right : ", img_right.shape)
images_center.append(img_center)
images_left.append(img_left)
images_right.append(img_right)
car_images.extend([img_center, img_left, img_right])
steering_angles.extend([steering_center, steering_left, steering_right])
"""
img_center = process_image(np.asarray(image.open(path + line[0])))
img_left = process_image(np.asarray(image.open(path + line[1])))
img_right = process_image(np.asarray(image.open(path + line[2])))
"""
augmented_images = []
augmented_measurements = []
for image, measurement in zip(car_images, steering_angles):
#for image, measurement in zip(images, measurements):
augmented_images.append(image)
augmented_measurements.append(measurement)
augmented_images.append(cv2.flip(image,1))
augmented_measurements.append(measurement*-1.0)
#print("augmented_images : ", len(augmented_images))
X = np.array(augmented_images)
y = np.array(augmented_measurements)
#print("X.shape", len(augmented_images))
# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
X_train = X_train.reshape(X_train.shape[0], 160,320,3)
from sklearn.preprocessing import StandardScaler
#print("X_train.shape", X_train.shape)
#nsamples, nx, ny, num = X_train.shape # Comment for now pk
#X_train = X_train.reshape((nsamples,nx*ny*num))
###### Review this again ############
#from sklearn.preprocessing import StandardScaler
#sc = StandardScaler()
#X_train = sc.fit_transform(X_train)
#X_test = sc.transform(X_test)
from keras.models import Sequential
from keras.layers import Flatten, ELU, Dense, Dropout, Lambda
from keras.layers.convolutional import Convolution2D, Cropping2D
from keras.layers.pooling import MaxPooling2D
from matplotlib import pyplot
# Initialising the ANN
model = Sequential()
#model.add(Lambda(preprocess_batch, input_shape=(160, 320, 3), output_shape=(64, 64, 3)))
model.add(Lambda(lambda x: x/255.0 - 0.5, input_shape=(160,320,3) ))
# layer 1 output shape is 32x32x32
model.add(Convolution2D(32, (5, 5), input_shape=(160, 320, 3), subsample=(2, 2), border_mode="same"))
model.add(ELU())
# layer 2 output shape is 15x15x16
model.add(Convolution2D(16, (3, 3), subsample=(1, 1), border_mode="valid"))
model.add(ELU())
model.add(Dropout(.4))
model.add(MaxPooling2D((2, 2), border_mode='same'))
# layer 3 output shape is 12x12x16
model.add(Convolution2D(16, (3, 3), subsample=(1, 1), border_mode="valid"))
model.add(ELU())
model.add(Dropout(.4))
# Flatten the output
model.add(Flatten())
# layer 4
model.add(Dense(1024))
model.add(Dropout(.3))
model.add(ELU())
# layer 5
model.add(Dense(512))
model.add(ELU())
# Finally a single output, since this is a regression problem
model.add(Dense(1))
model.compile(optimizer="adam", loss="mse")
model.compile(loss='mse', optimizer='adam')
model.fit(X_train, y_train, validation_split=0.2, shuffle=True, epochs=6)
model.save('sim_3cam_0808_ucity_data_upd_model.h5')
# Reference "https://machinelearningmastery.com/how-to-reduce-overfitting-with-dropout-regularization-in-keras/"
# Reference "https://machinelearningmastery.com/dropout-for-regularizing-deep-neural-networks/"