-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
225 lines (173 loc) · 6.65 KB
/
utils.py
File metadata and controls
225 lines (173 loc) · 6.65 KB
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
import errno
import json
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.misc
from scipy.ndimage import rotate
from scipy.stats import bernoulli
# Some useful constants
DRIVING_LOG_FILE = './data/driving_log.csv'
IMG_PATH = './data/'
STEERING_COEFFICIENT = 0.229
def crop(image, top_percent, bottom_percent):
"""
Crops an image according to the given parameters
:param image: source image
:param top_percent:
The percentage of the original image will be cropped from the top of the image
:param bottom_percent:
The percentage of the original image will be cropped from the bottom of the image
:return:
The cropped image
"""
assert 0 <= top_percent < 0.5, 'top_percent should be between 0.0 and 0.5'
assert 0 <= bottom_percent < 0.5, 'top_percent should be between 0.0 and 0.5'
top = int(np.ceil(image.shape[0] * top_percent))
bottom = image.shape[0] - int(np.ceil(image.shape[0] * bottom_percent))
return image[top:bottom, :]
def resize(image, new_dim):
"""
Resize a given image according the the new dimension
:return:
Resize image
"""
return scipy.misc.imresize(image, new_dim)
def random_flip(image, steering_angle, flipping_prob=0.5):
"""
Based on the outcome of an coin flip, the image will be flipped.
If flipping is applied, the steering angle will be negated.
https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.bernoulli.html
:return: Both flipped image and new steering angle
"""
head = bernoulli.rvs(flipping_prob)
if head:
return np.fliplr(image), -1 * steering_angle
else:
return image, steering_angle
def random_gamma(image):
"""
used as an alternative method changing the brightness of training images.
http://www.pyimagesearch.com/2015/10/05/opencv-gamma-correction/
:return:
New image generated by applying gamma correction to the source image
"""
gamma = np.random.uniform(0.4, 1.5)
inv_gamma = 1.0 / gamma
table = np.array([((i / 255.0) ** inv_gamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
# apply gamma correction using the lookup table
return cv2.LUT(image, table)
def random_shear(image, steering_angle, shear_range=200):
"""
Source: https://medium.com/@ksakmann/behavioral-cloning-make-a-car-drive-like-yourself-dc6021152713#.7k8vfppvk
:return:
The image generated by applying random shear on the source image
"""
rows, cols, ch = image.shape
dx = np.random.randint(-shear_range, shear_range + 1)
random_point = [cols / 2 + dx, rows / 2]
pts1 = np.float32([[0, rows], [cols, rows], [cols / 2, rows / 2]])
pts2 = np.float32([[0, rows], [cols, rows], random_point])
dsteering = dx / (rows / 2) * 360 / (2 * np.pi * 25.0) / 6.0
M = cv2.getAffineTransform(pts1, pts2)
image = cv2.warpAffine(image, M, (cols, rows), borderMode=1)
steering_angle += dsteering
return image, steering_angle
def random_rotation(image, steering_angle, rotation_amount=15):
"""
:return:
"""
angle = np.random.uniform(-rotation_amount, rotation_amount + 1)
rad = (np.pi / 180.0) * angle
return rotate(image, angle, reshape=False), steering_angle + (-1) * rad
def min_max(data, a=-0.5, b=0.5):
"""
:return:
"""
data_max = np.max(data)
data_min = np.min(data)
return a + (b - a) * ((data - data_min) / (data_max - data_min))
def generate_new_image(image, steering_angle, top_crop_percent=0.35, bottom_crop_percent=0.1,
resize_dim=(64, 64), do_shear_prob=0.9):
"""
:return:
"""
head = bernoulli.rvs(do_shear_prob)
if head == 1:
image, steering_angle = random_shear(image, steering_angle)
image = crop(image, top_crop_percent, bottom_crop_percent)
image, steering_angle = random_flip(image, steering_angle)
image = random_gamma(image)
image = resize(image, resize_dim)
return image, steering_angle
def get_next_image_files(batch_size=64):
"""
The simulator records three images (namely: left, center, and right) at a given time
However, when we are picking images for training we randomly (with equal probability)
one of these three images and its steering angle.
:return:
An list of selected (image files names, respective steering angles)
"""
data = pd.read_csv(DRIVING_LOG_FILE)
num_of_img = len(data)
rnd_indices = np.random.randint(0, num_of_img, batch_size)
image_files_and_angles = []
for index in rnd_indices:
rnd_image = np.random.randint(0, 3)
if rnd_image == 0:
img = data.iloc[index]['left'].strip()
angle = data.iloc[index]['steering'] + STEERING_COEFFICIENT
image_files_and_angles.append((img, angle))
elif rnd_image == 1:
img = data.iloc[index]['center'].strip()
angle = data.iloc[index]['steering']
image_files_and_angles.append((img, angle))
else:
img = data.iloc[index]['right'].strip()
angle = data.iloc[index]['steering'] - STEERING_COEFFICIENT
image_files_and_angles.append((img, angle))
return image_files_and_angles
def generate_next_batch(batch_size=64):
"""
This generator yields the next training batch
:return:
A tuple of features and steering angles as two numpy arrays
"""
while True:
X_batch = []
y_batch = []
images = get_next_image_files(batch_size)
for img_file, angle in images:
raw_image = plt.imread(IMG_PATH + img_file)
raw_angle = angle
new_image, new_angle = generate_new_image(raw_image, raw_angle)
X_batch.append(new_image)
y_batch.append(new_angle)
assert len(X_batch) == batch_size, 'len(X_batch) == batch_size should be True'
yield np.array(X_batch), np.array(y_batch)
def save_model(model, model_name='model.json', weights_name='model.h5'):
"""
Save the model into the hard disk
:return:
"""
silent_delete(model_name)
silent_delete(weights_name)
json_string = model.to_json()
with open(model_name, 'w') as outfile:
json.dump(json_string, outfile)
model.save_weights(weights_name)
def silent_delete(file):
"""
method delete the given file from the file system if it is available
Source: http://stackoverflow.com/questions/10840533/most-pythonic-way-to-delete-a-file-which-may-not-exist
:return:
None
"""
try:
os.remove(file)
except OSError as error:
if error.errno != errno.ENOENT:
raise