-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
143 lines (119 loc) · 4.21 KB
/
utils.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
from sklearn.utils import shuffle
import matplotlib.image as mpimg
from imgaug import augmenters as iaa
import cv2
import random
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Convolution2D,Flatten,Dense
from tensorflow.keras.optimizers import Adam
def getName(filePath):
return filePath.split('\\')[-1]
def importDataInfo(path):
coloums=['Center','Left','Right','Steering','Throttle','Brake','Speed']
data=pd.read_csv(os.path.join(path,'driving_log.csv'),names=coloums)
# print(getName(data['Center'][0]))
data['Center']=data['Center'].apply(getName)
# print(data.head())
print("Total images imported",data.shape[0])
return data
def balanceData(data,display=True):
nBins=31
samplesPerBin=1000
hist,bins=np.histogram(data['Steering'],nBins)
# print(bins)
if display:
center=(bins[:-1]+bins[1:])*0.5
# print(center)
plt.bar(center,hist,width=0.06)
plt.plot((-1,1),(samplesPerBin,samplesPerBin))
plt.show()
removeIndexList=[]
for j in range(nBins):
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',len(removeIndexList))
data.drop(data.index[removeIndexList],inplace=True)
print("Remaining Images",len(data))
if display:
hist,_= np.histogram(data['Steering'], nBins)
plt.bar(center,hist,width=0.06)
plt.plot((-1,1),(samplesPerBin,samplesPerBin))
plt.show()
return data
def loadData(path,data):
imagesPath=[]
steering=[]
for i in range(len(data)):
indexedData=data.iloc[i]
# print(indexedData)
imagesPath.append(os.path.join(path,'IMG',indexedData[0]))
steering.append(float(indexedData[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.4,1.2))
img=brightness.augment_image(img)
if np.random.rand() < 0.5:
img=cv2.flip(img,1)
steering=-steering
return img,steering
imgRe,st=augmentImage('test.jpg',0)
plt.imshow(imgRe)
plt.show()
def preProcessing(img):
img=img[60:135,:,:]
img=cv2.cvtColor(img,cv2.COLOR_RGB2YUV)
img=cv2.GaussianBlur(img,(3,3),0)
img=cv2.resize(img,(200,66))
img=img/255
return img
imgRe=preProcessing(mpimg.imread('test.jpg'))
plt.imshow(imgRe)
plt.show()
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=preProcessing(img)
imgBatch.append(img)
steeringBatch.append(steering)
yield (np.asarray(imgBatch),np.asarray(steeringBatch))
def createModel():
model=Sequential()
model.add(Convolution2D(24,(5,5),(2,2),input_shape=(66,200,3),activation="elu"))
model.add(Convolution2D(36, (5, 5), (2, 2), activation="elu"))
model.add(Convolution2D(48, (5, 5), (2, 2), 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