File size: 9,676 Bytes
da2e2ac |
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 |
import time
from typing import List
import numpy as np
import numpy.typing as npt
import yaml
from nuplan.common.actor_state.ego_state import EgoState
from nuplan.common.actor_state.state_representation import StateSE2, TimePoint
from nuplan.common.geometry.convert import relative_to_absolute_poses
from nuplan.planning.simulation.planner.ml_planner.transform_utils import (
_get_fixed_timesteps,
_se2_vel_acc_to_ego_state,
)
from nuplan.planning.simulation.trajectory.interpolated_trajectory import InterpolatedTrajectory
from nuplan.planning.simulation.trajectory.trajectory_sampling import TrajectorySampling
from navsim.common.dataclasses import PDMResults, Trajectory
from navsim.planning.metric_caching.metric_cache import MetricCache
from navsim.planning.simulation.planner.pdm_planner.scoring.pdm_scorer import (
PDMScorer,
)
from navsim.planning.simulation.planner.pdm_planner.scoring.pdm_scorer_progress import PDMScorerProgress
from navsim.planning.simulation.planner.pdm_planner.simulation.pdm_simulator import (
PDMSimulator,
)
from navsim.planning.simulation.planner.pdm_planner.utils.pdm_array_representation import (
ego_states_to_state_array,
)
from navsim.planning.simulation.planner.pdm_planner.utils.pdm_enums import (
MultiMetricIndex,
WeightedMetricIndex,
)
def transform_trajectory(
pred_trajectory: Trajectory, initial_ego_state: EgoState
) -> InterpolatedTrajectory:
"""
Transform trajectory in global frame and return as InterpolatedTrajectory
:param pred_trajectory: trajectory dataclass in ego frame
:param initial_ego_state: nuPlan's ego state object
:return: nuPlan's InterpolatedTrajectory
"""
future_sampling = pred_trajectory.trajectory_sampling
timesteps = _get_fixed_timesteps(
initial_ego_state, future_sampling.time_horizon, future_sampling.interval_length
)
relative_poses = np.array(pred_trajectory.poses, dtype=np.float64)
relative_states = [StateSE2.deserialize(pose) for pose in relative_poses]
absolute_states = relative_to_absolute_poses(initial_ego_state.rear_axle, relative_states)
# NOTE: velocity and acceleration ignored by LQR + bicycle model
agent_states = [
_se2_vel_acc_to_ego_state(
state,
[0.0, 0.0],
[0.0, 0.0],
timestep,
initial_ego_state.car_footprint.vehicle_parameters,
)
for state, timestep in zip(absolute_states, timesteps)
]
# NOTE: maybe make addition of initial_ego_state optional
return InterpolatedTrajectory([initial_ego_state] + agent_states)
def get_trajectory_as_array(
trajectory: InterpolatedTrajectory,
future_sampling: TrajectorySampling,
start_time: TimePoint,
) -> npt.NDArray[np.float64]:
"""
Interpolated trajectory and return as numpy array
:param trajectory: nuPlan's InterpolatedTrajectory object
:param future_sampling: Sampling parameters for interpolation
:param start_time: TimePoint object of start
:return: Array of interpolated trajectory states.
"""
times_s = np.arange(
0.0,
future_sampling.time_horizon + future_sampling.interval_length,
future_sampling.interval_length,
)
times_s += start_time.time_s
times_us = [int(time_s * 1e6) for time_s in times_s]
times_us = np.clip(times_us, trajectory.start_time.time_us, trajectory.end_time.time_us)
time_points = [TimePoint(time_us) for time_us in times_us]
trajectory_ego_states: List[EgoState] = trajectory.get_state_at_times(time_points)
return ego_states_to_state_array(trajectory_ego_states)
def pdm_score(
metric_cache: MetricCache,
model_trajectory: Trajectory,
future_sampling: TrajectorySampling,
simulator: PDMSimulator,
scorer: PDMScorer,
use_pdm_closed: bool = False
) -> PDMResults:
"""
Runs PDM-Score and saves results in dataclass.
:param metric_cache: Metric cache dataclass
:param model_trajectory: Predicted trajectory in ego frame.
:return: Dataclass of PDM-Subscores.
"""
initial_ego_state = metric_cache.ego_state
pdm_trajectory = metric_cache.trajectory
pred_trajectory = transform_trajectory(model_trajectory, initial_ego_state)
pdm_states, pred_states = (
get_trajectory_as_array(pdm_trajectory, future_sampling, initial_ego_state.time_point),
get_trajectory_as_array(pred_trajectory, future_sampling, initial_ego_state.time_point),
)
trajectory_states = np.concatenate([pdm_states[None, ...], pred_states[None, ...]], axis=0)
simulated_states = simulator.simulate_proposals(trajectory_states, initial_ego_state)
scores = scorer.score_proposals(
simulated_states,
metric_cache.observation,
metric_cache.centerline,
metric_cache.route_lane_ids,
metric_cache.drivable_area_map,
)
# TODO: Refactor & add / modify existing metrics.
pred_idx = 0 if use_pdm_closed else 1
no_at_fault_collisions = scorer._multi_metrics[MultiMetricIndex.NO_COLLISION, pred_idx]
drivable_area_compliance = scorer._multi_metrics[MultiMetricIndex.DRIVABLE_AREA, pred_idx]
driving_direction_compliance = scorer._multi_metrics[
MultiMetricIndex.DRIVING_DIRECTION, pred_idx
]
ego_progress = scorer._weighted_metrics[WeightedMetricIndex.PROGRESS, pred_idx]
time_to_collision_within_bound = scorer._weighted_metrics[WeightedMetricIndex.TTC, pred_idx]
comfort = scorer._weighted_metrics[WeightedMetricIndex.COMFORTABLE, pred_idx]
score = scores[pred_idx]
return PDMResults(
no_at_fault_collisions,
drivable_area_compliance,
driving_direction_compliance,
ego_progress,
time_to_collision_within_bound,
comfort,
score,
)
def pdm_score_vocab(
metric_cache: MetricCache,
vocab_trajectory: npt.NDArray,
future_sampling: TrajectorySampling,
simulator: PDMSimulator,
scorer: PDMScorer,
) -> npt.NDArray:
"""
Runs PDM-Score and saves results in dataclass.
:param metric_cache: Metric cache dataclass
:param vocab_trajectory: Predicted trajectory in ego frame.
:return: Dataclass of PDM-Subscores.
"""
initial_ego_state = metric_cache.ego_state
# a = time.time()
transformed_ones = [transform_trajectory(Trajectory(pose, TrajectorySampling(
time_horizon=4, interval_length=0.1
)), initial_ego_state) for pose in vocab_trajectory]
# b = time.time()
vocab_states = [
get_trajectory_as_array(
transformed,
future_sampling,
initial_ego_state.time_point
)[None] for transformed in transformed_ones
]
# c = time.time()
trajectory_states = np.concatenate(vocab_states, axis=0)
simulated_states = simulator.simulate_proposals(trajectory_states, initial_ego_state)
# d = time.time()
scores = scorer.score_proposals(
simulated_states,
metric_cache.observation,
metric_cache.centerline,
metric_cache.route_lane_ids,
metric_cache.drivable_area_map,
)
# e = time.time()
# print(f'transform: {b-a}, get_trajectory_as_array: {c-b}, simulate: {d-c}, score: {e-d}')
return scores
def pdm_score_full(
metric_cache: MetricCache,
vocab_trajectory: npt.NDArray,
future_sampling: TrajectorySampling,
simulator: PDMSimulator,
scorer: PDMScorerProgress,
) -> npt.NDArray:
"""
Runs PDM-Score and saves results in dataclass.
:param metric_cache: Metric cache dataclass
:param vocab_trajectory: Predicted trajectory in ego frame.
:return: Dataclass of PDM-Subscores.
"""
initial_ego_state = metric_cache.ego_state
transformed_ones = [transform_trajectory(Trajectory(pose, TrajectorySampling(
time_horizon=4, interval_length=0.1
)), initial_ego_state) for pose in vocab_trajectory]
pdm_states = get_trajectory_as_array(
metric_cache.trajectory,
future_sampling,
initial_ego_state.time_point
)[None]
# pdm, vocab-0, vocab-1, ..., vocab-n
all_states = [pdm_states]
all_states += [
get_trajectory_as_array(
transformed,
future_sampling,
initial_ego_state.time_point
)[None] for transformed in transformed_ones
]
all_states = np.concatenate(all_states, axis=0)
simulated_states = simulator.simulate_proposals(all_states, initial_ego_state)
scores = scorer.score_proposals(
simulated_states,
metric_cache.observation,
metric_cache.centerline,
metric_cache.route_lane_ids,
metric_cache.drivable_area_map,
)
return {
'noc': scorer._multi_metrics[MultiMetricIndex.NO_COLLISION].astype(np.float16)[1:],
'da': scorer._multi_metrics[MultiMetricIndex.DRIVABLE_AREA].astype(np.bool)[1:],
'dd': scorer._multi_metrics[MultiMetricIndex.DRIVING_DIRECTION].astype(np.float16)[1:],
'ttc': scorer._weighted_metrics[WeightedMetricIndex.TTC].astype(np.bool)[1:],
'progress': scorer._weighted_metrics[WeightedMetricIndex.PROGRESS].astype(np.float16)[1:],
'comfort': scorer._weighted_metrics[WeightedMetricIndex.COMFORTABLE].astype(np.bool)[1:],
'total': scores.astype(np.float16)[1:]
}
|