-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathai_driver.py
478 lines (369 loc) · 14.2 KB
/
ai_driver.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# -*- coding: utf-8 -*-
"""AI_Driver.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1duHGNGqfOBkiaLVnnrFDt-FHLskcOgm5
"""
!git clone https://github.com/LiyuanLucasLiu/RAdam.git
!python RAdam/setup.py install
# imports
import os
import sys
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils import data
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
from torch.utils.data.sampler import SubsetRandomSampler
from RAdam import radam
import cv2
import matplotlib.image as mpimg
import numpy as np
import csv
import requests
import zipfile
import time
import pandas as pd
# class for download
class DataDownloader:
def __init__(self, file_id, destination, download = True):
self.file_id = file_id
self.destination = destination
if download:
self.download_dataset()
self.extract_zip()
def download_dataset(self):
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def save_response_content(response):
CHUNK_SIZE = 32768
with open(self.destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params = { 'id' : self.file_id }, stream = True)
token = get_confirm_token(response)
if token:
params = { 'id' : self.file_id, 'confirm' : token }
response = session.get(URL, params = params, stream = True)
save_response_content(response)
def extract_zip(self):
if not os.path.exists('input'):
os.makedirs('input')
if not os.path.exists('output'):
os.makedirs('output')
with zipfile.ZipFile(self.destination, 'r') as zip_ref:
zip_ref.extractall('./input/')
FILE_ID = '1VaYonsJUovGO1AamMQuC2LN47AZ4pkTm'
DST_LOC = './self_driving_dataset.zip'
DATA_CSV_FILE_PATH = './input/driving_log.csv'
DATA_IMAGES_DIR = './input/IMG'
MODEL_SAVE_PATH = './output/ai_driver_cnn.pth'
IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS = 66, 200, 3
SAVE_DIR = './output/'
data_download = DataDownloader(FILE_ID, DST_LOC, True)
# Helper defs
def load_image(data_dir, image_file):
"""
Load RGB images from a file
"""
name = image_file.split('/')[-1]
return mpimg.imread(os.path.join(data_dir, name))
def crop(image):
"""
Crop the image (removing the sky at the top and the car front at the bottom)
"""
return image[60:-25, :, :] # remove the sky and the car front
def resize(image):
"""
Resize the image to the input shape used by the network model
"""
return cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT), cv2.INTER_AREA)
def rgb2yuv(image):
"""
Convert the image from RGB to YUV (This is what the NVIDIA model does)
"""
return cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
def preprocess(image):
"""
Combine all preprocess functions into one
"""
image = crop(image)
image = resize(image)
image = rgb2yuv(image)
return image
def choose_image(data_dir, center, left, right, steering_angle):
"""
Randomly choose an image from the center, left or right, and adjust
the steering angle.
"""
choice = np.random.choice(3)
if choice == 0:
return load_image(data_dir, left), steering_angle + 0.2
elif choice == 1:
return load_image(data_dir, right), steering_angle - 0.2
return load_image(data_dir, center), steering_angle
def random_flip(image, steering_angle):
"""
Randomly flipt the image left <-> right, and adjust the steering angle.
"""
if np.random.rand() < 0.5:
image = cv2.flip(image, 1)
steering_angle = -steering_angle
return image, steering_angle
def random_translate(image, steering_angle, range_x, range_y):
"""
Randomly shift the image virtially and horizontally (translation).
"""
trans_x = range_x * (np.random.rand() - 0.5)
trans_y = range_y * (np.random.rand() - 0.5)
steering_angle += trans_x * 0.002
trans_m = np.float32([[1, 0, trans_x], [0, 1, trans_y]])
height, width = image.shape[:2]
image = cv2.warpAffine(image, trans_m, (width, height))
return image, steering_angle
def random_shadow(image):
"""
Generates and adds random shadow
"""
print(image.shape)
# (x1, y1) and (x2, y2) forms a line
# xm, ym gives all the locations of the image
x1, y1 = IMAGE_WIDTH * np.random.rand(), 0
x2, y2 = IMAGE_WIDTH * np.random.rand(), IMAGE_HEIGHT
xm, ym = np.mgrid[0:IMAGE_HEIGHT, 0:IMAGE_WIDTH]
# mathematically speaking, we want to set 1 below the line and zero otherwise
# Our coordinate is up side down. So, the above the line:
# (ym-y1)/(xm-x1) > (y2-y1)/(x2-x1)
# as x2 == x1 causes zero-division problem, we'll write it in the below form:
# (ym-y1)*(x2-x1) - (y2-y1)*(xm-x1) > 0
mask = np.zeros_like(image[:, :, 1])
mask[(ym - y1) * (x2 - x1) - (y2 - y1) * (xm - x1) > 0] = 1
# choose which side should have shadow and adjust saturation
cond = mask == np.random.randint(2)
s_ratio = np.random.uniform(low=0.2, high=0.5)
# adjust Saturation in HLS(Hue, Light, Saturation)
hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
hls[:, :, 1][cond] = hls[:, :, 1][cond] * s_ratio
return cv2.cvtColor(hls, cv2.COLOR_HLS2RGB)
def random_brightness(image):
"""
Randomly adjust brightness of the image.
"""
# HSV (Hue, Saturation, Value) is also called HSB ('B' for Brightness).
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
ratio = 1.0 + 0.4 * (np.random.rand() - 0.5)
hsv[:,:,2] = hsv[:,:,2] * ratio
return cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
def augument(data_dir, center, left, right, steering_angle, range_x=100, range_y=10):
"""
Generate an augumented image and adjust steering angle.
(The steering angle is associated with the center image)
"""
image, steering_angle = choose_image(data_dir, center, left, right, steering_angle)
image, steering_angle = random_flip(image, steering_angle)
image, steering_angle = random_translate(image, steering_angle, range_x, range_y)
# image = random_shadow(image)
image = random_brightness(image)
return image, steering_angle
class CustomDataset(data.Dataset):
def __init__(self, csv_file_path, image_dir, transform = None):
self.csv_file_path = csv_file_path
self.image_dir = image_dir
self.transform = transform
self.examples = []
with open(self.csv_file_path) as csvfile:
reader = csv.reader(csvfile)
next(reader, None)
for line in reader:
self.examples.append(line)
def __getitem__(self, index):
example = self.examples[index]
center, left, right = example[0], example[1], example[2]
steering_angle = float(example[3])
if np.random.rand() < 0.6:
image, steering_angle = augument(self.image_dir, center, left, right, steering_angle)
else:
image = load_image(self.image_dir, center)
image = preprocess(image)
if self.transform is not None:
image = self.transform(image)
return image, steering_angle
def __len__(self):
return len(self.examples)
batch_size = 128
num_epochs = 40
validation_split = 0.25
shuffle_dataset = True
random_seed = 42
num_workers = 4
print("Initializing Datasets and Dataloaders...")
# Creating data indices for training and validation splits:
#Create a dataset object
transformations = transforms.Compose([transforms.Lambda(lambda x: (x / 127.5) - 1.0)])
dataset = CustomDataset(DATA_CSV_FILE_PATH, DATA_IMAGES_DIR, transformations)
dataset_size = len(dataset)
# dataset_size = 3000
indices = list(range(dataset_size))
split = int(np.floor(validation_split * dataset_size))
if shuffle_dataset :
np.random.seed(random_seed)
np.random.shuffle(indices)
train_indices, val_indices = indices[split:], indices[:split]
# Creating PT data samplers and loaders:
train_sampler = SubsetRandomSampler(train_indices)
valid_sampler = SubsetRandomSampler(val_indices)
train_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
sampler=train_sampler, num_workers=num_workers)
validation_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
sampler=valid_sampler, num_workers=num_workers)
test_loader = torch.utils.data.DataLoader(dataset, batch_size=1,
sampler=valid_sampler, num_workers=num_workers)
data_loader_dict = {
'train': train_loader,
'val': validation_loader
}
# Detect if we have a GPU available
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class DriverNet(nn.Module):
def __init__(self):
super(DriverNet, self).__init__()
self.conv_layers = nn.Sequential(
nn.Conv2d(3, 24, kernel_size=5, stride=2),
nn.ELU(),
nn.Conv2d(24, 36, kernel_size=5, stride=2),
nn.ELU(),
nn.Conv2d(36, 48, kernel_size=5, stride=2),
nn.ELU(),
nn.Conv2d(48, 64, kernel_size=3, stride=1),
nn.ELU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.ELU(),
nn.Dropout(p=0.5)
)
self.linear_layers = nn.Sequential(
nn.Linear(in_features=64*1*18, out_features=100),
nn.ELU(),
nn.Dropout(p=0.5),
nn.Linear(in_features=100, out_features=64),
nn.ELU(),
nn.Linear(in_features=64, out_features=10),
nn.ELU(),
nn.Linear(in_features=10, out_features=1)
)
def forward(self, input):
input = input.view(input.size(0), 3, 66, 200)
output = self.conv_layers(input)
output = output.view(output.size(0), -1)
output = self.linear_layers(output)
return output
model_ft = DriverNet()
# Send the model to GPU
model_ft = model_ft.to(device)
# Gather the parameters to be optimized/updated in this run. If we are
# finetuning we will be updating all parameters. However, if we are
# doing feature extract method, we will only update the parameters
# that we have just initialized, i.e. the parameters with requires_grad
# is True.
params_to_update = model_ft.parameters()
print("Params to learn:")
for name,param in model_ft.named_parameters():
if param.requires_grad == True:
print("\t",name)
# Observe that all parameters are being optimized
optimizer_ft = radam.RAdam(params_to_update)
# optimizer_ft = optim.SGD(params_to_update, lr = 0.00008)
# optimizer_ft = optim.Adam(params_to_update, lr = 0.0001)
def toDevice(data, device):
return data.float().to(device)
def train_model(model, dataloaders, criterion, optimizer, num_epochs=25):
since = time.time()
epoch_number, train_losses, val_losses, = [], [], []
best_loss = 10000.0
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
epoch_number.append(epoch)
# Each epoch has a training and validation phase
# Training loop
train_loss = 0.0
val_loss = 0.0
# Training
model.train()
for inputs, labels in dataloaders['train']:
inputs = toDevice(inputs, device)
labels = toDevice(labels, device)
optimizer.zero_grad()
# Generate predictions
out = model(inputs)
# Calculate loss
loss = criterion(out, labels.unsqueeze(1))
# Backpropagation
loss.backward()
# Update model parameters
optimizer.step()
train_loss += loss.item()
# Validation
model.eval()
with torch.no_grad():
for inputs, labels in dataloaders['val']:
inputs = toDevice(inputs, device)
labels = toDevice(labels, device)
# Generate predictions
out = model(inputs)
# Calculate loss
loss = criterion(out, labels.unsqueeze(1))
val_loss += loss.item()
# Average validation loss
train_loss = train_loss / len(dataloaders['train'])
val_loss = val_loss / len(dataloaders['val'])
train_losses.append(train_loss)
val_losses.append(val_loss)
print('Train Loss: {:.4f}'.format(train_loss))
print('Val Loss: {:.4f}'.format(val_loss))
# If the validation loss is at a minimum
if val_loss < best_loss:
# Save the model
torch.save(model, MODEL_SAVE_PATH)
best_loss = val_loss
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
print('Lead val Loss: {:4f}'.format(best_loss))
#creating dataframe and record all the losses and accuracies at each epoch
log_frame = pd.DataFrame(columns = ["Epoch", "Train Loss", "Test Loss"])
log_frame["Epoch"] = epoch_number
log_frame["Train Loss"] = train_losses
log_frame["Test Loss"] = val_losses
log_frame.to_csv(os.path.join(SAVE_DIR, "log2.csv"), index = False)
# load best model weights
# model.load_state_dict(best_model_wts)
return model
criterion = nn.MSELoss()
# Train and evaluate
model_ft = train_model(model_ft, data_loader_dict, criterion, optimizer_ft, num_epochs=num_epochs)
frame = pd.read_csv(os.path.join(SAVE_DIR, "log.csv"))
frame
from matplotlib import pyplot as plt
from matplotlib import style
from numpy import genfromtxt
data = genfromtxt(os.path.join(SAVE_DIR, "log2.csv"),delimiter=',', names=['Epoch', 'Train Loss', 'Test Loss'])
epoch_list = []
train_loss_list = []
test_loss_list = []
for row in data:
if not np.isnan(row[0]):
epoch_list.append(row[0])
train_loss_list.append(row[1])
test_loss_list.append(row[2])
plt.plot(epoch_list, train_loss_list, label = "Training Loss")
plt.plot(epoch_list, test_loss_list, label = "Testing Loss")
plt.title('MSE Loss Vs Epoch')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.show()