-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils1.py
135 lines (122 loc) · 4.56 KB
/
utils1.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
import numpy as np
from numpy.core.defchararray import translate
import pandas as pd
import os
import cv2
import random
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
import matplotlib.image as mpimg
from imgaug import augmenters as iaa
from keras.models import Sequential
from keras.layers import Convolution2D,Flatten,Dense
from keras.optimizers import Adam
import tensorflow
#used to get only last name of the path
def getName(filepath):
return filepath.split('\\')[-1]
def importDataInfo(path):
columns = ['Center', 'Left', 'Right', 'Steering', 'Throttle', 'Brake', 'Speed']
data = pd.read_csv(os.path.join(path, 'driving_log.csv'), names = columns)
#### REMOVE FILE PATH AND GET ONLY FILE NAME
#print(getName(data['center'][0]))
data['Center']=data['Center'].apply(getName)
#print(data.head())
print('Total Images Imported',data.shape[0])
return data
#org
def balanceData(data,display=True):
nBin = 31
samplesPerBin = 1000
hist, bins = np.histogram(data['Steering'], nBin)
if display:
center = (bins[:-1] + bins[1:]) * 0.5
plt.bar(center, hist, width=0.06)
plt.plot((np.min(data['Steering']), np.max(data['Steering'])), (samplesPerBin, samplesPerBin))
plt.show()
removeindexList = []
for j in range(nBin):
binDataList = []
for i in range(len(data['Steering'])):
if data['Steering'][i] >= bins[j] and data['Steering'][i] <= bins[j + 1]:
binDataList.append(i)
binDataList = shuffle(binDataList)
binDataList = binDataList[samplesPerBin:]
removeindexList.extend(binDataList)
print('Removed Images:', len(removeindexList))
data.drop(data.index[removeindexList], inplace=True)
print('Remaining Images:', len(data))
if display:
hist, _ = np.histogram(data['Steering'], (nBin))
plt.bar(center, hist, width=0.06)
plt.plot((np.min(data['Steering']), np.max(data['Steering'])), (samplesPerBin, samplesPerBin))
plt.show()
#data = balanceData(data,display=False)
return data
def loadData(path, data):
imagesPath = []
steering = []
for i in range(len(data)):
indexed_data = data.iloc[i]
imagesPath.append(os.path.join(path,'IMG',indexed_data[0]))
#imagesPath.append(f'{path}/IMG/{indexed_data[0]}')
steering.append(float(indexed_data[3]))
imagesPath = np.asarray(imagesPath)
steering = np.asarray(steering)
return imagesPath, steering
def augmentImage(imgPath,steering):
img = mpimg.imread(imgPath)
if np.random.rand() < 0.5:
pan = iaa.Affine(translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)})
img = pan.augment_image(img)
if np.random.rand() < 0.5:
zoom = iaa.Affine(scale=(1, 1.2))
img = zoom.augment_image(img)
if np.random.rand() < 0.5:
brightness = iaa.Multiply((0.2, 1.2))
img = brightness.augment_image(img)
if np.random.rand() < 0.5:
img = cv2.flip(img, 1)
steering = -steering
return img, steering
def preProcess(img):
#cropping only road on images
img = img[60:135,:,:]
#converting image to yuv(changing color space)
img = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
#adding blur
img = cv2.GaussianBlur(img, (3, 3), 0)
#resize the image
img = cv2.resize(img, (200, 66))
#normalization
img = img/255
return img
def batchGen(imagesPath, steeringList, batchSize, trainFlag):
while True:
imgBatch = []
steeringBatch = []
for i in range(batchSize):
index = random.randint(0, len(imagesPath) - 1)
if trainFlag:
img, steering = augmentImage(imagesPath[index], steeringList[index])
else:
img = mpimg.imread(imagesPath[index])
steering = steeringList[index]
img = preProcess(img)
imgBatch.append(img)
steeringBatch.append(steering)
yield (np.asarray(imgBatch),np.asarray(steeringBatch))
def createModel():
model = Sequential()
model.add(Convolution2D(24,(5,5),input_shape=(66,200,3),activation='elu'))
model.add(Convolution2D(36,(5,5),activation='elu'))
model.add(Convolution2D(48,(5,5),activation='elu'))
model.add(Convolution2D(64,(3,3),activation='elu'))
model.add(Convolution2D(64,(3,3),activation='elu'))
model.add(Flatten())
model.add(Dense(100,activation='elu'))
model.add(Dense(50,activation='elu'))
model.add(Dense(10,activation='elu'))
model.add(Dense(1))
model.compile(Adam(lr=0.0001),loss='mse')
return model