-
Notifications
You must be signed in to change notification settings - Fork 6
/
eval_model_car.py
344 lines (289 loc) · 12.4 KB
/
eval_model_car.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
'''A script for evaluating the Car Model.
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
import skimage.io
import skimage.morphology as skim
import numpy as np
import pickle
from os import path as osp
import argparse
import json
from functools import partial
try:
from ompl import base as ob
# from ompl import geometric as og
from ompl import control as oc
from ompl import util as ou
except ImportError:
raise ImportError("Container does not have OMPL installed")
from transformer import Models
from utils import geom2pix
from math import sin, cos, tan, pi
from pathlib import Path
from dataLoader import get_encoder_input
res = 0.05
dist_resl = res
length = 24
robot_radius = 0.2
carLength = 0.3
def pix2geom(pos, res=0.05, length=24):
"""
Converts pixel co-ordinates to geometrical positions.
:param pos: The (x,y) pixel co-ordinates.
:param res: The distance represented by each pixel.
:param length: The length of the map in meters.
:returns (float, float): The associated eucledian co-ordinates.
"""
return (pos[0]*res, length-pos[1]*res)
receptive_field = 32
hashTable = [(20*r+4, 20*c+4) for c in range(24) for r in range(24)]
# Planning parameters
space = ob.SE2StateSpace()
# Set the bounds
bounds = ob.RealVectorBounds(2)
bounds.setLow(0)
bounds.setHigh(length)
space.setBounds(bounds)
cspace = oc.RealVectorControlSpace(space, 2)
cbounds = ob.RealVectorBounds(2)
cbounds.setLow(0, 0.0)
cbounds.setHigh(0, 1)
cbounds.setLow(1, -.5)
cbounds.setHigh(1, .5)
cspace.setBounds(cbounds)
ss = oc.SimpleSetup(cspace)
si = ob.SpaceInformation(space)
def kinematicCarODE(q, u, qdot):
theta = q[2]
qdot[0] = u[0] * cos(theta)
qdot[1] = u[0] * sin(theta)
qdot[2] = u[0] * tan(u[1]) / carLength
class ValidityChecker(ob.StateValidityChecker):
'''A class to check if an obstacle is in collision or not.
'''
def __init__(self, si, CurMap, MapMask=None, res=0.05, robot_radius=robot_radius):
'''
Intialize the class object, with the current map and mask generated
from the transformer model.
:param si: an object of type ompl.base.SpaceInformation
:param CurMap: A np.array with the current map.
:param MapMask: Areas of the map to be masked.
'''
super().__init__(si)
self.size = CurMap.shape
# Dilate image for collision checking
InvertMap = np.abs(1-CurMap)
InvertMapDilate = skim.dilation(InvertMap, skim.disk((0.1)/res))
MapDilate = abs(1-InvertMapDilate)
if MapMask is None:
self.MaskMapDilate = MapDilate>0.5
else:
self.MaskMapDilate = np.logical_and(MapDilate, MapMask)
def isValid(self, state):
'''
Check if the given state is valid.
:param state: An ob.State object to be checked.
:returns bool: True if the state is valid.
'''
x, y = state.getX(), state.getY()
pix_dim = geom2pix([x, y], size=self.size)
if pix_dim[0] < 0 or pix_dim[0] >= self.size[0] or pix_dim[1] < 0 or pix_dim[1] >= self.size[1]:
return False
return self.MaskMapDilate[pix_dim[1], pix_dim[0]]
# def get_path_sst(start, goal, input_map, patch_map):
# '''
# Plan a path using SST, but invert the start and goal location.
# :param start: The starting position on map co-ordinates.
# :param goal: The goal position on the map co-ordinates.
# :param input_map: The input map
# :param patch_map: The patch map
# :returns
# '''
# success, time, _, path = get_path(start, goal, input_map, patch_map, use_valid_sampler=True)
# return path, time, [], success
def get_path(start, goal, input_map, patch_map, step_time=0.1, max_time=300, exp=False):
'''
Plan a path given the start, goal and patch_map.
:param start: The SE(2) co-ordinate of start position
:param goal: The SE(2) co-ordinate of goal position
:param patch_map: The patch map from MPT
:param step_time: The time step for the planner.
:param max_time: The maximum time to plan
:param exp: If true, the planner switches between exploration and exploitation.
returns tuple: Returns path array, time of solution, number of vertices, success.
'''
# Tried importance sampling, but seems like it makes not much improvement
# over rejection sampling.
StartState = ob.State(space)
# import pdb;pdb.set_trace()
StartState().setX(start[0])
StartState().setY(start[1])
StartState().setYaw(start[2])
GoalState = ob.State(space)
GoalState().setX(goal[0])
GoalState().setY(goal[1])
GoalState().setYaw(goal[2])
success = False
ss = oc.SimpleSetup(cspace)
# setup validity checker
ValidityCheckerObj = ValidityChecker(si, input_map, patch_map)
NewValidityCheckerObj = ValidityChecker(si, input_map)
ss.setStateValidityChecker(ValidityCheckerObj)
# Set the start and goal States:
ss.setStartAndGoalStates(StartState, GoalState, 1.0)
ode = oc.ODE(kinematicCarODE)
odeSolver = oc.ODEBasicSolver(ss.getSpaceInformation(), ode)
propagator = oc.ODESolver.getStatePropagator(odeSolver)
ss.setStatePropagator(propagator)
ss.getSpaceInformation().setPropagationStepSize(0.1)
# ss.getSpaceInformation().setMinMaxControlDuration(1, 10)
planner = oc.SST(ss.getSpaceInformation())
ss.setPlanner(planner)
time = step_time
if exp:
solved = ss.solve(time)
while not ss.haveExactSolutionPath():
solved = ss.solve(step_time)
time += step_time
if time>2:
break
ss.setStateValidityChecker(NewValidityCheckerObj)
solved = ss.solve(step_time)
while not ss.haveExactSolutionPath():
solved = ss.solve(step_time)
time += step_time
if time > max_time:
break
if ss.haveExactSolutionPath():
success = True
print("Found Solution")
path = np.array([[ss.getSolutionPath().getState(i).getX(), ss.getSolutionPath().getState(i).getY(), ss.getSolutionPath().getState(i).getYaw()]
for i in range(ss.getSolutionPath().getStateCount())
])
path_quality = 0
for i in range(len(path)-1):
path_quality += np.linalg.norm(path[i+1, :2]-path[i, :2])
else:
success = False
path_quality = np.inf
path = []
plannerData = ob.PlannerData(si)
planner.getPlannerData(plannerData)
numVertices = plannerData.numVertices()
return path, time, numVertices, success
device='cuda' if torch.cuda.is_available() else 'cpu'
def get_patch(model, start_pos, goal_pos, input_map):
'''
Return the patch map for the given start and goal position, and the network
architecture.
:param model:
:param start:
:param goal:
:param input_map:
'''
# Identitfy Anchor points
encoder_input = get_encoder_input(input_map, goal_pos, start_pos)
predVal = model(encoder_input[None,:].float().cuda())
predClass = predVal[0, :, :].max(1)[1]
predProb = F.softmax(predVal[0, :, :], dim=1)
possAnchor = [hashTable[i] for i, label in enumerate(predClass) if label==1]
# Generate Patch Maps
patch_map = np.zeros_like(input_map)
map_size = input_map.shape
for pos in possAnchor:
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[0], pos[0]+ receptive_field//2)
goal_end_y = min(map_size[1], pos[1]+ receptive_field//2)
patch_map[goal_start_y:goal_end_y, goal_start_x:goal_end_x] = 1.0
return patch_map
device='cuda' if torch.cuda.is_available() else 'cpu'
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--modelFolder', help='Directory where model_params.json exists', required=True)
parser.add_argument('--valDataFolder', help='Directory where training data exists', required=True)
parser.add_argument('--start', help='Start of environment number', required=True, type=int)
parser.add_argument('--numEnv', help='Number of environments', required=True, type=int)
parser.add_argument('--numPaths', help='Number of start and goal pairs for each env', default=1, type=int)
parser.add_argument('--use_sst', help='Use only SST without mask', dest='use_sst', action='store_true')
parser.add_argument('--explore', help='Use only SST without mask', dest='explore', action='store_true')
args = parser.parse_args()
modelFolder = args.modelFolder
modelFile = osp.join(modelFolder, f'model_params.json')
assert osp.isfile(modelFile), f"Cannot find the model_params.json file in {modelFolder}"
# env_num = args.envNum
start = args.start
use_sst = args.use_sst
valDataFolder = args.valDataFolder
if not use_sst:
model_param = json.load(open(modelFile))
transformer = Models.Transformer(
**model_param
)
transformer.to(device)
receptive_field=32
# Load model parameters
epoch = 69
checkpoint = torch.load(osp.join(modelFolder, f'model_epoch_{epoch}.pkl'))
transformer.load_state_dict(checkpoint['state_dict'])
# Only do evaluation
transformer.eval()
# Get path data
pathSuccess = []
pathTime = []
pathVertices = []
for env_num in range(start, start+args.numEnv):
temp_map = osp.join(valDataFolder, f'env{env_num:06d}/map_{env_num}.png')
small_map = skimage.io.imread(temp_map, as_gray=True)
for pathNum in range(args.numPaths):
print(f"planning on env_{env_num} path_{pathNum}")
pathFile = osp.join(valDataFolder, f'env{env_num:06d}/path_{pathNum}.p')
data = pickle.load(open(pathFile, 'rb'))
path = data['path_interpolated']
if data['success']:
if not use_sst:
goal_pos = geom2pix(path[-1, :])
start_pos = geom2pix(path[0, :])
# Identitfy Anchor points
encoder_input = get_encoder_input(small_map, goal_pos, start_pos)
# predVal = transformer(encoder_input[None,:].float().cuda())
with torch.no_grad():
predVal = transformer(encoder_input[None,:].float().cuda())
predProb = F.softmax(predVal[0, :, :], dim=1)
predClass = predVal[0, :, :].max(1)[1]
possAnchor = [hashTable[i] for i, label in enumerate(predClass) if label==1]
# Generate Patch Maps
patch_map = np.zeros_like(small_map)
map_size = small_map.shape
for pos in possAnchor:
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[0], pos[0]+ receptive_field//2)
goal_end_y = min(map_size[1], pos[1]+ receptive_field//2)
patch_map[goal_start_y:goal_end_y, goal_start_x:goal_end_x] = 1.0
_, time, numVer, success = get_path(path[0, :], path[-1, :], small_map, patch_map.T, max_time=90, exp=args.explore)
else:
_, time, numVer, success = get_path(path[0, :], path[-1, :], small_map, patch_map=None, max_time=150)
pathSuccess.append(success)
pathTime.append(time)
pathVertices.append(numVer)
else:
pathSuccess.append(False)
pathTime.append(time)
pathVertices.append(numVer)
# np.save(f'{result_folder}/PathSuccess_{start}.npy', PathSuccess)
# np.save(f'{result_folder}/TimeSuccess_{start}.npy', TimeSuccess)
# np.save(f'{result_folder}/QualitySuccess{start}.npy', QualitySuccess)
# pickle.dump(PathSuccess, open(osp.join(modelFolder, f'eval_unknown_plan_{start:06d}.p'), 'wb'))
pathData = {'Time': pathTime, 'Success': pathSuccess, 'Vertices': pathVertices}
if use_sst:
fileName = osp.join(modelFolder, f'eval_val_plan_sst_{start:06d}.p')
else:
if args.explore:
fileName = osp.join(modelFolder, f'eval_val_plan_exp_mpt_sst_{start:06d}.p')
else:
fileName = osp.join(modelFolder, f'eval_val_plan_mpt_sst_{start:06d}.p')
pickle.dump(pathData, open(fileName, 'wb'))
print(len(pathSuccess))