-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataLoader.py
475 lines (408 loc) · 19.2 KB
/
dataLoader.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
''' A dataloader for training Mask+Transformers
'''
import torch
from torch.utils.data import Dataset
import skimage.io
import pickle
import numpy as np
import os
from os import path as osp
from einops import rearrange
from torch.nn.utils.rnn import pad_sequence
from utils import geom2pix
def PaddedSequence(batch):
'''
This should be passed to DataLoader class to collate batched samples with various length.
:param batch: The batch to consolidate
'''
data = {}
data['map'] = torch.cat([batch_i['map'][None, :] for batch_i in batch if batch_i is not None])
data['anchor'] = pad_sequence([batch_i['anchor'] for batch_i in batch if batch_i is not None], batch_first=True)
data['labels'] = pad_sequence([batch_i['labels'] for batch_i in batch if batch_i is not None], batch_first=True)
data['length'] = torch.tensor([batch_i['anchor'].shape[0] for batch_i in batch if batch_i is not None])
return data
def PaddedSequenceUnet(batch):
'''
This should be passed to DataLoader class to collate batched samples with various length.
:param batch: The batch to consolidate
'''
data = {}
data['map'] = torch.cat([batch_i['map'][None, :] for batch_i in batch if batch_i is not None])
data['mask'] = torch.cat([batch_i['mask'][None, :] for batch_i in batch if batch_i is not None])
return data
def PaddedSequenceMPnet(batch):
'''
This should be passed to the dataLoader class to collate batched samples with various lengths
:param batch: The batch to consolidate.
'''
data = {}
data['map'] = torch.cat([batch_i['map'][None, :, :] for batch_i in batch])
data['inputs'] = pad_sequence([batch_i['inputs'] for batch_i in batch], batch_first=True)
data['targets'] = pad_sequence([batch_i['targets'] for batch_i in batch], batch_first=True)
data['length'] = torch.tensor([batch_i['inputs'].size(0) for batch_i in batch])
return data
map_size = (480, 480)
receptive_field = 32
res = 0.05 # meter/pixels
# Convert Anchor points to points on the axis.
X = np.arange(4, 24*20+4, 20)*res
Y = 24-np.arange(4, 24*20+4, 20)*res
grid_2d = np.meshgrid(X, Y)
grid_points = rearrange(grid_2d, 'c h w->(h w) c')
hashTable = [(20*r+4, 20*c+4) for c in range(24) for r in range(24)]
def geom2pixMatpos(pos, res=0.05, size=(480, 480)):
"""
Find the nearest index of the discrete map state.
:param pos: The (x,y) geometric co-ordinates.
:param res: The distance represented by each pixel.
:param size: The size of the map image
:returns (int, int): The associated pixel co-ordinates.
"""
indices = np.where(np.linalg.norm(grid_points-pos, axis=1)<=receptive_field*res*0.7)
return indices
def geom2pixMatneg(pos, res=0.05, size=(480, 480), num=1):
"""
Find the nearest index of the discrete map state.
:param pos: The (x,y) geometric co-ordinates.
:param res: The distance represented by each pixel.
:param size: The size of the map image
:param num: The number of random sample index to select.
:returns (int, int): The associated pixel co-ordinates.
"""
dist = np.linalg.norm(grid_points-pos, axis=1)
indices, = np.where(dist>receptive_field*res*0.7)
indices = np.random.choice(indices, size=num)
return indices,
def get_encoder_input(InputMap, goal_pos, start_pos):
'''
Returns the input map appended with the goal, and start position encoded.
:param InputMap: The grayscale map
:param goal_pos: The goal pos of the robot on the costmap.
:param start_pos: The start pos of the robot on the costmap.
:returns np.array: The map concatentated with the encoded start and goal pose.
'''
map_size = InputMap.shape
assert len(map_size) == 2, "This only works for 2D maps"
context_map = np.zeros(map_size)
goal_start_y = max(0, goal_pos[0]- receptive_field//2)
goal_start_x = max(0, goal_pos[1]- receptive_field//2)
goal_end_y = min( map_size[1], goal_pos[0]+ receptive_field//2)
goal_end_x = min( map_size[0], goal_pos[1]+ receptive_field//2)
context_map[goal_start_x:goal_end_x, goal_start_y:goal_end_y] = 1.0
# Mark start region
start_start_y = max(0, start_pos[0]- receptive_field//2)
start_start_x = max(0, start_pos[1]- receptive_field//2)
start_end_y = min( map_size[1], start_pos[0]+ receptive_field//2)
start_end_x = min( map_size[0], start_pos[1]+ receptive_field//2)
context_map[start_start_x:start_end_x, start_start_y:start_end_y] = -1.0
return torch.as_tensor(np.concatenate((InputMap[None, :], context_map[None, :])))
class PathPatchDataLoader(Dataset):
'''Loads each image, with input images and output patches. Used to train
UNet architecture model.
'''
def __init__(self, env_list, dataFolder):
'''
:param env_list: The list of map environmens to collect data from.
:param samples: The number of paths to use from each folder.
:param dataFolder: The parent folder where the files are located.
It should follow the following format:
env1/path_0.p
...
env2/path_0.p
...
...
'''
assert isinstance(env_list, list), "Needs to be a list"
self.num_env = len(env_list)
self.env_list = env_list
# capture only the successful trajectories
self.indexDict = []
for envNum in env_list:
for i in range(len(os.listdir(osp.join(dataFolder, f'env{envNum:06d}')))-1):
with open(osp.join(dataFolder, f'env{envNum:06d}', f'path_{i}.p'), 'rb') as f:
if pickle.load(f)['success']:
self.indexDict.append((envNum, i))
# self.indexDict = [(envNum, i)
# for envNum in env_list
# for i in range(len(os.listdir(osp.join(dataFolder, f'env{envNum:06d}')))-1)
# ]
self.dataFolder = dataFolder
def __len__(self):
return len(self.indexDict)
def __getitem__(self, idx):
'''
Returns the sample at index idx.
returns dict: A dictionary of the encoded map and target classes.
'''
env, idx_sample = self.indexDict[idx]
mapEnvg = skimage.io.imread(osp.join(self.dataFolder, f'env{env:06d}', f'map_{env}.png'), as_gray=True)
with open(osp.join(self.dataFolder, f'env{env:06d}', f'path_{idx_sample}.p'), 'rb') as f:
data = pickle.load(f)
if data['success']:
path = data['path_interpolated']
# Mark goal region
goal_index = geom2pix(path[-1, :])
start_index = geom2pix(path[0, :])
mapEncoder = get_encoder_input(mapEnvg, goal_index, start_index)
AnchorPointsPos = []
AnchorPointsXY = []
for pos in path:
indices, = geom2pixMatpos(pos)
for index in indices:
if index not in AnchorPointsPos:
AnchorPointsPos.append(index)
AnchorPointsXY.append(hashTable[index])
# Generate patch map
maskMap = np.zeros_like(mapEnvg)
for pos in AnchorPointsXY:
goal_start_x = max(0, pos[0]- receptive_field//2)
goal_start_y = max(0, pos[1]- receptive_field//2)
goal_end_x = min(map_size[1], pos[0]+ receptive_field//2)
goal_end_y = min(map_size[0], pos[1]+ receptive_field//2)
maskMap[goal_start_y:goal_end_y, goal_start_x:goal_end_x] = 1.0
return {
'map':torch.as_tensor(mapEncoder),
'mask': torch.as_tensor(maskMap, dtype=int)
}
class PathSeqDataLoader(Dataset):
'''Loads each path, and the the sequence of current the future sampled points
for planning.
'''
def __init__(self, env_list, dataFolder, worldMapBounds):
'''
:param env_list: The list of map environments to collect data from.
:param samples: The number of paths to use from each folder.
:param dataFolder: The parent folder where the files are located.
It should follow the following format:
env1/path_0.p
...
env2/path_0.p
...
...
:param worldMapBounds: The 2D bounds of the map [L x H]
'''
assert isinstance(env_list, list), "Needs to be a list"
self.num_env = len(env_list)
self.env_list = env_list
# capture only the successful trajectories
self.indexDict = []
for envNum in env_list:
for i in range(len(os.listdir(osp.join(dataFolder, f'env{envNum:06d}')))-1):
with open(osp.join(dataFolder, f'env{envNum:06d}', f'path_{i}.p'), 'rb') as f:
if pickle.load(f)['success']:
self.indexDict.append((envNum, i))
self.dataFolder = dataFolder
self.worldMapBounds = worldMapBounds if isinstance(worldMapBounds, np.ndarray) else np.array(worldMapBounds)
def __len__(self):
return len(self.indexDict)
def __getitem__(self, idx):
'''
Returns the sample at index idx.
returns dict: A dictonary containing maps and different combination of start and goal
'''
env, idx_sample = self.indexDict[idx]
mapEnvg = skimage.io.imread(osp.join(self.dataFolder, f'env{env:06d}', f'map_{env}.png'), as_gray=True)
with open(osp.join(self.dataFolder, f'env{env:06d}', f'path_{idx_sample}.p'), 'rb') as f:
data = pickle.load(f)
# Noramalize the data
path = (data['path']/self.worldMapBounds)*2-1
# Combine goal and start position
nInput = np.c_[path[:-1, :], np.array([[1]]*(path.shape[0]-1))*path[-1][None,:]]
# Get Predicted point
nTarget = path[1:, :]
return {
'map':torch.as_tensor(mapEnvg[None, :], dtype=torch.float),
'inputs': torch.as_tensor(nInput, dtype=torch.float),
'targets': torch.as_tensor(nTarget, dtype=torch.float)
}
class PathDataLoader(Dataset):
'''Loads each path, and extracts the masked positive and negative regions
'''
def __init__(self, env_list, dataFolder):
'''
:param env_list: The list of map environments to collect data from.
:param samples: The number of paths to use from each folder.
:param dataFolder: The parent folder where the files are located.
It should follow the following format:
env1/path_0.p
...
env2/path_0.p
...
...
'''
assert isinstance(env_list, list), "Needs to be a list"
self.num_env = len(env_list)
self.env_list = env_list
self.indexDict = [(envNum, i)
for envNum in env_list
for i in range(len(os.listdir(osp.join(dataFolder, f'env{envNum:06d}')))-1)
]
self.dataFolder = dataFolder
def __len__(self):
return len(self.indexDict)
def __getitem__(self, idx):
'''
Returns the sample at index idx.
returns dict: A dictonary of the encoded map and target points.
'''
env, idx_sample = self.indexDict[idx]
mapEnvg = skimage.io.imread(osp.join(self.dataFolder, f'env{env:06d}', f'map_{env}.png'), as_gray=True)
with open(osp.join(self.dataFolder, f'env{env:06d}', f'path_{idx_sample}.p'), 'rb') as f:
data = pickle.load(f)
if data['success']:
path = data['path_interpolated']
# Mark goal region
goal_index = geom2pix(path[-1, :])
start_index = geom2pix(path[0, :])
mapEncoder = get_encoder_input(mapEnvg, goal_index, start_index)
AnchorPointsPos = []
for pos in path:
indices, = geom2pixMatpos(pos)
for index in indices:
if index not in AnchorPointsPos:
AnchorPointsPos.append(index)
backgroundPoints = list(set(range(len(hashTable)))-set(AnchorPointsPos))
numBackgroundSamp = min(len(backgroundPoints), 2*len(AnchorPointsPos))
AnchorPointsNeg = np.random.choice(backgroundPoints, size=numBackgroundSamp, replace=False).tolist()
anchor = torch.cat((torch.tensor(AnchorPointsPos), torch.tensor(AnchorPointsNeg)))
labels = torch.zeros_like(anchor)
labels[:len(AnchorPointsPos)] = 1
return {
'map':torch.as_tensor(mapEncoder),
'anchor':anchor,
'labels':labels
}
class PathMixedDataLoader(Dataset):
'''Loads each path, and extracts the masked positive and negative regions.
The data is indexed in such a way that "hard" planning problems are equally distributed
uniformly throughout the dataloading process.
'''
def __init__(self, envListMaze, dataFolderMaze, envListForest, dataFolderForest):
'''
:param envListMaze: The list of map environments to collect data from Maze.
:param dataFolderMaze: The parent folder where the maze path files are located.
:param envListForest: The list of map environments to collect data from Forest.
:param dataFodlerForest: The parent folder where the forest path files are located.
It should follow the following format:
env1/path_0.p
...
env2/path_0.p
...
...
'''
assert isinstance(envListMaze, list), "Needs to be a list"
assert isinstance(envListForest, list), "Needs to be a list"
self.num_env = len(envListForest) + len(envListMaze)
self.indexDictMaze = [('M', envNum, i)
for envNum in envListMaze
for i in range(len(os.listdir(osp.join(dataFolderMaze, f'env{envNum:06d}')))-1)
]
self.indexDictForest = [('F', envNum, i)
for envNum in envListForest
for i in range(len(os.listdir(osp.join(dataFolderForest, f'env{envNum:06d}')))-1)
]
self.dataFolder = {'F': dataFolderForest, 'M':dataFolderMaze}
self.envList = {'F': envListForest, 'M': envListMaze}
def __len__(self):
return len(self.indexDictForest)+len(self.indexDictMaze)
def __getitem__(self, idx):
'''
Returns the sample at index idx.
returns dict: A dictonary of the encoded map and target points.
'''
try:
DF, env, idx_sample = idx
except ValueError:
print(idx)
dataFolder = self.dataFolder[DF]
mapEnvg = skimage.io.imread(osp.join(dataFolder, f'env{env:06d}', f'map_{env}.png'), as_gray=True)
with open(osp.join(dataFolder, f'env{env:06d}', f'path_{idx_sample}.p'), 'rb') as f:
data = pickle.load(f)
if data['success']:
path = data['path_interpolated']
# Mark goal region
goal_index = geom2pix(path[-1, :])
start_index = geom2pix(path[0, :])
mapEncoder = get_encoder_input(mapEnvg, goal_index, start_index)
AnchorPointsPos = []
for pos in path:
indices, = geom2pixMatpos(pos)
for index in indices:
if index not in AnchorPointsPos:
AnchorPointsPos.append(index)
backgroundPoints = list(set(range(len(hashTable)))-set(AnchorPointsPos))
numBackgroundSamp = min(len(backgroundPoints), 2*len(AnchorPointsPos))
AnchorPointsNeg = np.random.choice(backgroundPoints, size=numBackgroundSamp, replace=False).tolist()
anchor = torch.cat((torch.tensor(AnchorPointsPos), torch.tensor(AnchorPointsNeg)))
labels = torch.zeros_like(anchor)
labels[:len(AnchorPointsPos)] = 1
return {
'map':torch.as_tensor(mapEncoder),
'anchor':anchor,
'labels':labels
}
class PathHardMineDataLoader(Dataset):
'''Loads each path, and extracts the masked positive and negative regions.
The data is indexed in such a way that "hard" planning problems are equally distributed
uniformly throughout the dataloading process.
'''
def __init__(self, env_list, dataFolderHard, dataFolderEasy):
'''
:param env_list: The list of map environments to collect data from.
:param samples: The number of paths to use from each folder.
:param dataFolderHard: The parent folder where the Hard path files are located.
:param dataFodlerEasy: The parent folder where the Easy path fiies are located.
It should follow the following format:
env1/path_0.p
...
env2/path_0.p
...
...
'''
assert isinstance(env_list, list), "Needs to be a list"
self.num_env = len(env_list)
self.env_list = env_list
self.indexDictHard = [('H', envNum, i)
for envNum in env_list
for i in range(len(os.listdir(osp.join(dataFolderHard, f'env{envNum:06d}')))-1)
]
self.indexDictEasy = [('E', envNum, i)
for envNum in env_list
for i in range(len(os.listdir(osp.join(dataFolderEasy, f'env{envNum:06d}')))-1)
]
self.dataFolder = {'E': dataFolderEasy, 'H':dataFolderHard}
def __len__(self):
return len(self.indexDictEasy)+len(self.indexDictHard)
def __getitem__(self, idx):
'''
Returns the sample at index idx.
returns dict: A dictonary of the encoded map and target points.
'''
DF, env, idx_sample = idx
dataFolder = self.dataFolder[DF]
mapEnvg = skimage.io.imread(osp.join(dataFolder, f'env{env:06d}', f'map_{env}.png'), as_gray=True)
with open(osp.join(dataFolder, f'env{env:06d}', f'path_{idx_sample}.p'), 'rb') as f:
data = pickle.load(f)
if data['success']:
path = data['path_interpolated']
# Mark goal region
goal_index = geom2pix(path[-1, :])
start_index = geom2pix(path[0, :])
mapEncoder = get_encoder_input(mapEnvg, goal_index, start_index)
AnchorPointsPos = []
for pos in path:
indices, = geom2pixMatpos(pos)
for index in indices:
if index not in AnchorPointsPos:
AnchorPointsPos.append(index)
backgroundPoints = list(set(range(len(hashTable)))-set(AnchorPointsPos))
numBackgroundSamp = min(len(backgroundPoints), len(AnchorPointsPos))
AnchorPointsNeg = np.random.choice(backgroundPoints, size=numBackgroundSamp, replace=False).tolist()
anchor = torch.cat((torch.tensor(AnchorPointsPos), torch.tensor(AnchorPointsNeg)))
labels = torch.zeros_like(anchor)
labels[:len(AnchorPointsPos)] = 1
return {
'map':torch.as_tensor(mapEncoder),
'anchor':anchor,
'labels':labels
}