forked from Atcold/pytorch-PPUU
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataloader.py
More file actions
243 lines (221 loc) · 11.1 KB
/
dataloader.py
File metadata and controls
243 lines (221 loc) · 11.1 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import sys
import numpy, random, pdb, math, pickle, glob, time, os, re
import torch
from torch.autograd import Variable
class DataLoader:
def __init__(self, fname, opt, dataset='simulator', single_shard=False):
if opt.debug:
single_shard = True
self.opt = opt
self.random = random.Random()
self.random.seed(12345) # use this so that the same batches will always be picked
if dataset == 'i80' or dataset == 'us101':
data_dir = 'traffic-data/state-action-cost/data_{}_v0'.format(dataset)
if single_shard:
# quick load for debugging
data_files = ['{}.txt/'.format(next(os.walk(data_dir))[1][0])]
else:
data_files = next(os.walk(data_dir))[1]
self.images = []
self.actions = []
self.costs = []
self.states = []
self.ids = []
for df in data_files:
combined_data_path = f'{data_dir}/{df}/all_data.pth'
if os.path.isfile(combined_data_path):
print('[loading data shard: {}]'.format(combined_data_path))
data = torch.load(combined_data_path)
self.images += data.get('images')
self.actions += data.get('actions')
self.costs += data.get('costs')
self.states += data.get('states')
self.ids += data.get('ids')
else:
print(data_dir)
images = []
actions = []
costs = []
states = []
ids = glob.glob(f'{data_dir}/{df}/car*.pkl').sort()
for f in ids:
print(f'[loading {f}]')
fd = pickle.load(open(f, 'rb'))
Ta = fd['actions'].size(0)
Tp = fd['pixel_proximity_cost'].size(0)
Tl = fd['lane_cost'].size(0)
# assert Ta == Tp == Tl # TODO Check why there are more costs than actions
# if not(Ta == Tp == Tl): pdb.set_trace()
images.append(fd['images'])
actions.append(fd['actions'])
costs.append(torch.cat((
fd.get('pixel_proximity_cost')[:Ta].view(-1, 1),
fd.get('lane_cost')[:Ta].view(-1, 1),
), 1),)
states.append(fd['states'])
print(f'Saving {combined_data_path} to disk')
torch.save({
'images': images,
'actions': actions,
'costs': costs,
'states': states,
'ids': ids,
}, combined_data_path)
self.images += images
self.actions += actions
self.costs += costs
self.states += states
self.ids += ids
else:
assert False, 'Data set not supported'
self.n_episodes = len(self.images)
print(f'Number of episodes: {self.n_episodes}')
splits_path = data_dir + '/splits.pth'
if os.path.exists(splits_path):
print(f'[loading data splits: {splits_path}]')
self.splits = torch.load(splits_path)
self.train_indx = self.splits.get('train_indx')
self.valid_indx = self.splits.get('valid_indx')
self.test_indx = self.splits.get('test_indx')
else:
print('[generating data splits]')
rgn = numpy.random.RandomState(0)
perm = rgn.permutation(self.n_episodes)
n_train = int(math.floor(self.n_episodes * 0.8))
n_valid = int(math.floor(self.n_episodes * 0.1))
self.train_indx = perm[0 : self.n_train]
self.valid_indx = perm[n_train : n_train + n_valid]
self.test_indx = perm[n_train + n_valid :]
torch.save(dict(
train_indx=self.train_indx,
valid_indx=self.valid_indx,
test_indx=self.test_indx,
), splits_path)
stats_path = data_dir + '/data_stats.pth'
if os.path.isfile(stats_path):
print(f'[loading data stats: {stats_path}]')
stats = torch.load(stats_path)
self.a_mean = stats.get('a_mean')
self.a_std = stats.get('a_std')
self.s_mean = stats.get('s_mean')
self.s_std = stats.get('s_std')
else:
print('[computing action stats]')
all_actions = []
for i in self.train_indx:
all_actions.append(self.actions[i])
all_actions = torch.cat(all_actions, 0)
self.a_mean = torch.mean(all_actions, 0)
self.a_std = torch.std(all_actions, 0)
print('[computing state stats]')
all_states = []
for i in self.train_indx:
all_states.append(self.states[i][:, 0])
all_states = torch.cat(all_states, 0)
self.s_mean = torch.mean(all_states, 0)
self.s_std = torch.std(all_states, 0)
torch.save({'a_mean': self.a_mean,
'a_std': self.a_std,
's_mean': self.s_mean,
's_std': self.s_std}, stats_path)
car_sizes_path = data_dir + '/car_sizes.pth'
print(f'[loading car sizes: {car_sizes_path}]')
self.car_sizes = torch.load(car_sizes_path)
# get batch to use for forward modeling
# a sequence of ncond given states, a sequence of npred actions,
# and a sequence of npred states to be predicted
def get_batch_fm(self, split, npred=-1, cuda=True):
# Choose the correct device
device = torch.device('cuda') if cuda else torch.device('cpu')
if split == 'train':
indx = self.train_indx
elif split == 'valid':
indx = self.valid_indx
elif split == 'test':
indx = self.test_indx
if npred == -1:
npred = self.opt.npred
images, states, actions, costs, ids, sizes = [], [], [], [], [], []
nb = 0
T = self.opt.ncond + npred
while nb < self.opt.batch_size:
s = self.random.choice(indx)
# min is important since sometimes numbers do not align causing issues in stack operation below
episode_length = min(self.images[s].size(0), self.states[s].size(0))
if episode_length >= T:
t = self.random.randint(0, episode_length - T)
images.append(self.images[s][t : t + T].to(device))
actions.append(self.actions[s][t : t + T].to(device))
states.append(self.states[s][t : t + T].to(device))
costs.append(self.costs[s][t : t + T].to(device))
ids.append(self.ids[s])
splits = self.ids[s].split('/')
timeslot = splits[-2]
car_id = int(re.findall(r'car(\d+).pkl', splits[-1])[0])
size = self.car_sizes[timeslot][car_id]
sizes.append([size[0], size[1]])
nb += 1
images = torch.stack(images).float()
images.div_(255.0)
states = torch.stack(states)
states = states[:, :, 0].contiguous()
actions = torch.stack(actions)
sizes = torch.tensor(sizes)
if not self.opt.debug:
actions -= self.a_mean.view(1, 1, 2).expand(actions.size()).to(device)
actions /= (1e-8 + self.a_std.view(1, 1, 2).expand(actions.size())).to(device)
states -= self.s_mean.view(1, 1, 4).expand(states.size()).to(device)
states /= (1e-8 + self.s_std.view(1, 1, 4).expand(states.size())).to(device)
costs = torch.stack(costs)
# |-----ncond-----||------------npred------------||
# ^ ^ ^
# 0 t0 t1
t0 = self.opt.ncond
t1 = T
input_images = images [:, :t0].float().contiguous()
input_states = states [:, :t0].float().contiguous()
target_images = images [:, t0:t1].float().contiguous()
target_states = states [:, t0:t1].float().contiguous()
target_costs = costs [:, t0:t1].float().contiguous()
t0 -= 1; t1 -= 1
actions = actions[:, t0:t1].float().contiguous()
# input_actions = actions[:, :t0].float().contiguous()
# n_cond n_pred
# <---------------------><---------------------------------->
# . .. .
# +---------------------+. . ^ ^
# |i|i|i|i|i|i|i|i|i|i|i|. 3 × 117 × 24 . | |
# +---------------------+. . | inputs |
# +---------------------+. . | |
# |s|s|s|s|s|s|s|s|s|s|s|. 4 . | |
# +---------------------+. . v |
# . +-----------------------------------+ . ^ |
# . 2 |a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a| . | actions |
# . +-----------------------------------+ . v |
# . +-----------------------------------+ ^ | tensors
# . 3 × 117 × 24 |i|i|i|i|i|i|i|i|i|i|i|i|i|i|i|i|i|i| | |
# . +-----------------------------------+ | |
# . +-----------------------------------+ | |
# . 4 |s|s|s|s|s|s|s|s|s|s|s|s|s|s|s|s|s|s| | targets |
# . +-----------------------------------+ | |
# . +-----------------------------------+ | |
# . 2 |c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c| | |
# . +-----------------------------------+ v v
# +---------------------------------------------------------+ ^
# | car_id | | string
# +---------------------------------------------------------+ v
# +---------------------------------------------------------+ ^
# | car_size | 2 | tensor
# +---------------------------------------------------------+ v
return [input_images, input_states], actions, [target_images, target_states, target_costs], ids, sizes
if __name__ == '__main__':
# Create some dummy options
class my_opt():
debug = False
batch_size = 4
npred = 20
ncond = 10
# Instantiate data set object
d = DataLoader(None, opt=my_opt, dataset='i80')
# Retrieve first training batch
x = d.get_batch_fm('train', cuda=False)