-
Notifications
You must be signed in to change notification settings - Fork 40
/
data_shuffled.py
177 lines (137 loc) · 4.93 KB
/
data_shuffled.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
#!/usr/bin/env python
from __future__ import division
import random
import os
import sys
from collections import OrderedDict
import cv2
import params
import preprocess
import local_common as cm
################ parameters ###############
data_dir = params.data_dir
epochs = params.epochs
img_height = params.img_height
img_width = params.img_width
img_channels = params.img_channels
purposes = ['train', 'val']
imgs = OrderedDict()
wheels = OrderedDict()
for purpose in purposes:
imgs[purpose] = []
wheels[purpose] = []
categories = ['center', 'curve']
imgs_cat = OrderedDict()
wheels_cat = OrderedDict()
for p in purposes:
imgs_cat[p] = OrderedDict()
wheels_cat[p] = OrderedDict()
for c in categories:
imgs_cat[p][c] = []
wheels_cat[p][c] = []
def load_imgs_v2():
global imgs
global wheels
for epoch_id in epochs['all']:
print ('processing and loading epoch {} into memorys. train:{}, val:{}'.format(
epoch_id, len(imgs['train']), len(imgs['val'])))
# vid_path = cm.jn(data_dir, 'epoch{:0>2}_front.mkv'.format(epoch_id))
vid_path = cm.jn(data_dir, 'out-video-{}.avi'.format(epoch_id))
if not os.path.isfile(vid_path):
continue
frame_count = cm.frame_count(vid_path)
cap = cv2.VideoCapture(vid_path)
# csv_path = cm.jn(data_dir, 'epoch{:0>2}_steering.csv'.format(epoch_id))
csv_path = cm.jn(data_dir, 'out-key-{}.csv'.format(epoch_id))
assert os.path.isfile(csv_path)
rows = cm.fetch_csv_data(csv_path)
print ("{}, {}".format(len(rows), frame_count))
assert frame_count == len(rows)
for row in rows:
ret, img = cap.read()
if not ret:
break
img = preprocess.preprocess(img)
angle = float(row['wheel'])
if random.random() < params.train_pct:
imgs['train'].append(img)
wheels['train'].append([angle])
else:
imgs['val'].append(img)
wheels['val'].append([angle])
cap.release()
print ('Total data: train:{}, val:{}'.format(len(imgs['train']), len(imgs['val'])))
# load all preprocessed training images into memory
def load_imgs():
global imgs
global wheels
for p in purposes:
for epoch_id in epochs[p]:
print ('processing and loading "{}" epoch {} into memory, current num of imgs is {}...'.format(p, epoch_id, len(imgs[p])))
# vid_path = cm.jn(data_dir, 'epoch{:0>2}_front.mkv'.format(epoch_id))
vid_path = cm.jn(data_dir, 'out-video-{}.avi'.format(epoch_id))
assert os.path.isfile(vid_path)
frame_count = cm.frame_count(vid_path)
cap = cv2.VideoCapture(vid_path)
# csv_path = cm.jn(data_dir, 'epoch{:0>2}_steering.csv'.format(epoch_id))
csv_path = cm.jn(data_dir, 'out-key-{}.csv'.format(epoch_id))
assert os.path.isfile(csv_path)
rows = cm.fetch_csv_data(csv_path)
print ("{}, {}".format(len(rows), frame_count))
assert frame_count == len(rows)
yy = [[float(row['wheel'])] for row in rows]
while True:
ret, img = cap.read()
if not ret:
break
img = preprocess.preprocess(img)
imgs[p].append(img)
wheels[p].extend(yy)
assert len(imgs[p]) == len(wheels[p])
cap.release()
def load_batch(purpose):
p = purpose
assert len(imgs[p]) == len(wheels[p])
n = len(imgs[p])
assert n > 0
ii = random.sample(xrange(0, n), params.batch_size)
assert len(ii) == params.batch_size
xx, yy = [], []
for i in ii:
xx.append(imgs[p][i])
yy.append(wheels[p][i])
return xx, yy
def categorize_imgs():
global imgs
global wheels
global imgs_cat
global wheels_cat
for p in purposes:
n = len(imgs[p])
for i in range(n):
# print 'wheels[{}][{}]:{}'.format(p, i, wheels[p][i])
if abs(wheels[p][i][0]) < 0.001:
imgs_cat[p]['center'].append(imgs[p][i])
wheels_cat[p]['center'].append(wheels[p][i])
else:
imgs_cat[p]['curve'].append(imgs[p][i])
wheels_cat[p]['curve'].append(wheels[p][i])
print ('---< {} >---'.format(p))
for c in categories:
print ('# {} imgs: {}'.format(c, len(imgs_cat[p][c])))
def load_batch_category_normal(purpose):
p = purpose
xx, yy = [], []
nc = len(categories)
for c in categories:
n = len(imgs_cat[p][c])
assert n > 0
ii = random.sample(xrange(0, n), int(params.batch_size/nc))
assert len(ii) == int(params.batch_size/nc)
for i in ii:
xx.append(imgs_cat[p][c][i])
yy.append(wheels_cat[p][c][i])
return xx, yy
if __name__ == '__main__':
load_imgs()
load_batch()