-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
72 lines (59 loc) · 2.99 KB
/
data.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
import csv
import numpy as np
from sklearn.utils import shuffle
import config
# load the driving-samples generated by the Udacity simulator into numpy arrays
def read_samples_from_file(driving_log_filepath, steering_correction):
image_paths = []
measurements = []
with open(driving_log_filepath) as csvfile:
reader = csv.reader(csvfile)
next(reader, None) # skip headers
for line in reader:
# camera images
center = line[0].strip()
left = line[1].strip()
right = line[2].strip()
# measurements
steering = float(line[3])
throttle = float(line[4])
brake = float(line[5])
speed = float(line[6])
# skip if speed is less than 0.1 because it's not representative for driving behavior
if abs(speed) < 0.1:
continue
# if the car drifts left, use center and the left camera image
if steering > config.STEERING_THRESHOLD:
image_paths.extend([center, left])
# correct steering for the left camera image by adding the steering_correction
measurements.extend([(steering, throttle, brake, speed),
(steering + steering_correction, throttle, brake, speed)])
# if the car drifts right, use center and the right camera image
if steering < -config.STEERING_THRESHOLD:
image_paths.extend([center, right])
# correct steering for the right camera image by subtracting the steering_correction
measurements.extend([(steering, throttle, brake, speed),
(steering - steering_correction, throttle, brake, speed)])
# if the car drives straight ahead use only the center image
else:
image_paths.append(center)
measurements.append((steering, throttle, brake, speed))
return np.array(image_paths), np.array(measurements)
# compensate the steering distribution of the dataset
def distribute_data(image_paths, measurements):
# group the data based on the steering angle
num_hist, idx_hist = np.histogram(measurements[:, 0], config.NUM_DATA_BINS)
# number of entries to which each group must be populated
max_count = int(max(num_hist) * 0.75)
# populate groups
for i in range(len(num_hist)):
if num_hist[i] < max_count:
# find the index where values fall within the range
match_idx = np.where((measurements[:, 0] >= idx_hist[i]) & (measurements[:, 0] < idx_hist[i + 1]))[0]
if len(match_idx) < 1:
continue
# randomly choose up to the max_count
to_be_added = np.random.choice(match_idx, max_count - num_hist[i])
image_paths = np.append(image_paths, image_paths[to_be_added])
measurements = np.vstack((measurements, measurements[to_be_added]))
return shuffle(image_paths, measurements)