-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
92 lines (71 loc) · 2.42 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
import numpy as np
from scipy.misc import imread, imresize
def read_imgs(img_paths):
imgs = np.empty([len(img_paths), 160, 320, 3])
for i, path in enumerate(img_paths):
imgs[i] = imread(path)
return imgs
def resize(imgs, shape=(64, 128, 3)):
"""
Resize images to shape.
"""
height, width, channels = shape
imgs_resized = np.empty([len(imgs), height, width, channels])
for i, img in enumerate(imgs):
imgs_resized[i] = imresize(img, shape)
return imgs_resized
def rgb2gray(imgs):
"""
Convert images to grayscale.
"""
return np.mean(imgs, axis=3, keepdims=True)
def raindrop_droptop(imgs):
"""
Crop out the top half of imgs.
"""
height = np.shape(imgs)[1]
return imgs[:, height//2:, :, :]
def normalize(imgs):
"""
Normalize images between [-1, 1].
"""
return imgs / (255.0 / 2) - 1
def preprocess(imgs):
imgs_processed = resize(imgs)
# imgs_processed = rgb2gray(imgs_processed)
imgs_processed = normalize(imgs_processed)
imgs_processed = raindrop_droptop(imgs_processed)
return imgs_processed
def random_flip(imgs, angles):
"""
Augment the data by randomly flipping some angles / images horizontally.
"""
new_imgs = np.empty_like(imgs)
new_angles = np.empty_like(angles)
for i, (img, angle) in enumerate(zip(imgs, angles)):
if np.random.choice(2):
new_imgs[i] = np.fliplr(img)
new_angles[i] = angle * -1
else:
new_imgs[i] = img
new_angles[i] = angle
return new_imgs, new_angles
def augment(imgs, angles):
imgs_augmented, angles_augmented = random_flip(imgs, angles)
return imgs_augmented, angles_augmented
def gen_batches(imgs, angles, batch_size):
"""
Generates random batches of the input data.
:param imgs: The input images.
:param angles: The steering angles associated with each image.
:param batch_size: The size of each minibatch.
:yield: A tuple (images, angles), where both images and angles have batch_size elements.
"""
num_elts = len(imgs)
while True:
indeces = np.random.choice(num_elts, batch_size)
batch_imgs_raw, angles_raw = read_imgs(imgs[indeces]), angles[indeces].astype(float)
batch_imgs, batch_angles = augment(preprocess(batch_imgs_raw), angles_raw)
# print(batch_imgs)
# print(batch_angles)
yield batch_imgs, batch_angles