-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
182 lines (147 loc) · 5.14 KB
/
data.py
File metadata and controls
182 lines (147 loc) · 5.14 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
"""
Python module to load the data for training
"""
import cv2
import csv
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.image as mpimg
from sklearn.utils import shuffle
from tqdm import tqdm
# Function to load dataset
def load_dataset(csv_path, relative_path):
"""
Inputs
---
csv_path: path to training data csv
relative_path: relative path to training data
Outputs
---
X: Training data numpy array
y: Training labels numpy array
"""
# Read CSV lines
lines = []
with open(csv_path) as csvfile:
reader = csv.reader(csvfile)
print("Loading CSV File ...")
for line in tqdm(reader):
lines.append(line)
images = []; measurements = []
print("Loading Data ...")
# Read from CSV lines
for line in tqdm(lines):
# Center Image
image, measurement = _load_image(line, 0, relative_path)
images.append(image)
measurements.append(measurement)
image_flipped = np.fliplr(image)
images.append(image_flipped)
measurement_flipped = -1 * measurement
measurements.append(measurement_flipped)
# Left Image
image, measurement = _load_image(line, 1, relative_path)
images.append(image)
measurements.append(measurement)
image_flipped = np.fliplr(image)
images.append(image_flipped)
measurement_flipped = -1 * measurement
measurements.append(measurement_flipped)
# Right Image
image, measurement = _load_image(line, 2, relative_path)
images.append(image)
measurements.append(measurement)
image_flipped = np.fliplr(image)
images.append(image_flipped)
measurement_flipped = -1 * measurement
measurements.append(measurement_flipped)
X = np.array(images)
y = np.array(measurements)
return X, y
# Function to generate a Generator
def load_generator(csv_path, relative_path, batch_size = 5):
"""
Inputs
---
csv_path: csv file to read data from
relative_path: relative path of the data
batch_size: batch size of the generator (factor of 6)
Outputs
---
generator: generator function
"""
# Read CSV lines
lines = []
with open(csv_path) as csvfile:
reader = csv.reader(csvfile)
print("Loading CSV File ...")
for line in tqdm(reader):
lines.append(line)
train_data, validation_data = train_test_split(lines, test_size=0.2)
# Define a generator function
def generator(data, batch_size = batch_size):
num_data = len(data)
while True:
shuffle(data)
for offset in range(0, num_data, batch_size):
batch_data = data[offset : offset + batch_size]
images = []; measurements = []
# Generate batches
for batch in batch_data:
# Center Image
image, measurement = _load_image(batch, 0, relative_path)
images.append(image)
measurements.append(measurement)
image_flipped = np.fliplr(image)
images.append(image_flipped)
measurement_flipped = -1 * measurement
measurements.append(measurement_flipped)
# Left Image
image, measurement = _load_image(batch, 1, relative_path)
images.append(image)
measurements.append(measurement)
image_flipped = np.fliplr(image)
images.append(image_flipped)
measurement_flipped = -1 * measurement
measurements.append(measurement_flipped)
# Right Image
image, measurement = _load_image(batch, 2, relative_path)
images.append(image)
measurements.append(measurement)
image_flipped = np.fliplr(image)
images.append(image_flipped)
measurement_flipped = -1 * measurement
measurements.append(measurement_flipped)
X = np.array(images)
y = np.array(measurements)
X, y = shuffle(X, y)
yield (X, y)
return generator(train_data), generator(validation_data), len(train_data), len(validation_data)
# Private function to load image
def _load_image(line, index, relative_path):
"""
Inputs
---
line: csv line to read data from
index: decides left, right or center
relative_path: relative path of the data
Outputs
---
image: output image
measurement: output measurement
"""
source_path = line[index]
filename = source_path.split('\\')[-1]
current_path = relative_path + filename
image = mpimg.imread(current_path)
if index == 1:
# Left Image
correction = 0.2
elif index == 2:
# Right Image
correction = -0.2
else:
# Center Image
correction = 0
measurement = float(line[3]) + correction
return image, measurement