-
Notifications
You must be signed in to change notification settings - Fork 0
/
firstTrainWithRighLeft_Nvidia.py
165 lines (139 loc) · 4.7 KB
/
firstTrainWithRighLeft_Nvidia.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
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 4 08:58:40 2017
@author: pcomitz
"""
###############
# 8/7/2017
# first nvidia
# 5:46 pm
# 3 epochs - around track - 9 mph
# trying 4 epochs at 7:26 pm
##############
import csv
import cv2
import numpy as np
#cv2 wont display images?
#import matplotlib.image as img
import matplotlib.pyplot as plt
import random
lines = []
lineNum = 0;
file = 'Z:\\proj3OnDisk\\data\\data\\driving_log_no_header.csv'
#for data I collected use
#file = 'Z:\\proj3OnDisk\\phc\\driving_log.csv'
with open(file) as csvfile:
reader = csv.reader(csvfile)
for line in reader:
lines.append(line)
lineNum = lineNum+1
print("number of lines in ",file, " is " , lineNum)
images = []
measurements = []
#Step 12 adding right and left camera
for line in lines:
for i in range(3):
source_path = line[i]
next = source_path.split('/')
filename = next[1]
current_path = 'Z:\\proj3OnDisk\\data\\data\\IMG\\' + filename
image = cv2.imread(current_path)
# to go BGR to RGB - from tutorial
imgOut = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
images.append(imgOut)
if(i == 0):
measurement = float(line[3])
elif(i == 1):
measurement = float(line[3]) + 0.2
elif (i ==2):
measurement = float(line[3]) - 0.2
# add to measurements
measurements.append(measurement)
#Step 11 Data Augmentation
augmented_images, augmented_measurements = [],[]
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)
#test image display
index = random.randint(0, len(augmented_images))
plt.figure(figsize=(1,1))
fig = plt.figure()
title = 'first:' + str(augmented_measurements[index])
a=fig.add_subplot(1,3,1)
a.set_title(title)
testImage= augmented_images[index].squeeze()
plt.imshow(testImage)
#next image
a=fig.add_subplot(1,3,2)
title = 'second:' + str(augmented_measurements[index+1])
a.set_title(title)
testImage= augmented_images[index+1].squeeze()
plt.imshow(testImage)
#next image
a=fig.add_subplot(1,3,3)
title = 'third:' + str(augmented_measurements[index+2])
a.set_title(title)
testImage= augmented_images[index+2].squeeze()
plt.imshow(testImage)
# prepare for Keras with augmented data from Step 11
# Keras requires numpy arrays
X_train= np.array(augmented_images)
y_train = np.array(augmented_measurements)
# Step 7 build simplest network possible
# flattened image connected to single output node
# single output node will predict steering angle
# makes this a regression network
# for classification network might apply softmax
# activation function to the output layer
#
from keras.models import Sequential
#Step 13 add cropping
from keras.layers import Flatten, Dense, Lambda, Cropping2D
#from Step 10 add 2D Convolution
from keras.layers import Convolution2D
from keras.layers.pooling import MaxPooling2D
model = Sequential()
#from step 9 Data Preprocessing
model.add(Lambda(lambda x: (x/255.0)-0.5, input_shape=(160,320,3)))
# Step 13 Crop 70 pix from top, 25 from bottom
# from keras docuentation
# If tuple of 2 tuples of 2 ints: interpreted as
# ((top_crop, bottom_crop), (left_crop, right_crop))
model.add(Cropping2D(cropping=((70,25),(0,0))))
#Step 10
"""
model.add(Convolution2D(6,5,5, activation = "relu"))
model.add(MaxPooling2D())
model.add(Convolution2D(6,5,5, activation = "relu"))
model.add(MaxPooling2D())
model.add(Dense(120))
model.add(Dense(84))
#model.add(Flatten(input_shape=(160,320,3)))
model.add(Flatten())
model.add(Dense(1))
"""
###
# Step 14 nvidia
# 5 convolutions followed by 4 fully connected
###
model.add(Convolution2D(24,5,5,subsample=(2,2), activation = "relu"))
model.add(Convolution2D(36,5,5,subsample=(2,2), activation = "relu"))
model.add(Convolution2D(48,5,5,subsample=(2,2), activation = "relu"))
model.add(Convolution2D(64,3,3,activation = "relu"))
model.add(Convolution2D(64,3,3,activation = "relu"))
model.add(Flatten())
model.add(Dense(100))
model.add(Dense(50))
model.add(Dense(10))
model.add(Dense(1))
#use mse rather than cross entropy
model.compile(loss='mse', optimizer='adam')
#shuffle and split off 20% for validation
EPOCHS = 4
print("starting to train for ",EPOCHS, " epochs")
model.fit(X_train,y_train, validation_split = 0.2, shuffle=True, nb_epoch = EPOCHS)
#save the model
print("saving the model")
model.save('model.h5.lambda.conv.aug.crop.lr.rgb.nvidia.4epochs')