BUG FIXES: moving around when policy is loaded, adding BaseAlgorithm abstract classes, correcting metrics, normalizng actions if idm environment is a normalized action one, manually updating environment graph when using idm, implementing idm forward class
This commit is contained in:
162
src/eval_main.py
162
src/eval_main.py
@@ -2,18 +2,23 @@ from tqdm import tqdm
|
||||
from copy import deepcopy
|
||||
import stable_baselines3 as sb3
|
||||
import intersim
|
||||
from intersim.envs import Intersimple
|
||||
from stable_baselines3.common.base_class import BaseAlgorithm
|
||||
from src.baselines import IDMRulePolicy
|
||||
from src.evaluation import IntersimpleEvaluation
|
||||
import src.options.envs as options_envs
|
||||
import src.gail.options as options_envs
|
||||
from src.evaluation.metrics import divergence, visualize_distribution
|
||||
|
||||
from typing import Optional, List, Dict, Tuple
|
||||
import torch
|
||||
import numpy as np
|
||||
|
||||
def load_policy(policy_file:str, method:str,
|
||||
policy_kwargs:dict, skip_load:bool=False) -> Optional[BaseAlgorithm]:
|
||||
#(method, policy_file, policy_kwargs, eval_env)
|
||||
|
||||
def load_policy(method:str,
|
||||
policy_file:str,
|
||||
policy_kwargs:dict,
|
||||
env: Intersimple) -> BaseAlgorithm:
|
||||
"""
|
||||
Load a model given a path and the method
|
||||
|
||||
@@ -21,13 +26,12 @@ def load_policy(policy_file:str, method:str,
|
||||
load_policy (str): the path to the model
|
||||
method (str): the method for the model
|
||||
skip_load (bool): whether to skip loading
|
||||
env (Intersimple): Intersimple environment for evaluation (necessary for IDM policy)
|
||||
Returns:
|
||||
policy (Optional[BaseAlgorithm]): the policy to evaluate
|
||||
"""
|
||||
if skip_load:
|
||||
return None
|
||||
elif method == 'idm':
|
||||
policy = IDMRulePolicy(policy_kwargs)
|
||||
if method == 'idm':
|
||||
policy = IDMRulePolicy(env, **policy_kwargs)
|
||||
elif method == 'bc':
|
||||
raise NotImplementedError
|
||||
elif method == 'gail':
|
||||
@@ -67,9 +71,6 @@ def form_expert_metrics(states:torch.Tensor, actions:torch.Tensor) ->Dict[str, l
|
||||
keys = ['col_all','v_all', 'a_all','j_all', 'v_avg', 'a_avg', 'col', 'brake', 't']
|
||||
metrics = {key:[None]*nv for key in keys}
|
||||
|
||||
import pdb
|
||||
pdb.set_trace()
|
||||
|
||||
for i in range(nv):
|
||||
|
||||
nni = ~torch.isnan(states[:,i,0])
|
||||
@@ -115,7 +116,6 @@ def generate_expert_metrics(locations: List[Tuple[int,int]]) -> List[Dict[str, l
|
||||
expert_metrics.append(form_expert_metrics(states, actions))
|
||||
return expert_metrics
|
||||
|
||||
|
||||
def load_expert_states(roundabout:int, track:int):
|
||||
"""
|
||||
Load expert states from roundabout/track info
|
||||
@@ -127,16 +127,18 @@ def load_expert_states(roundabout:int, track:int):
|
||||
actions (torch.tensor): (T, nv, 1) expert actions for track file
|
||||
"""
|
||||
rname = intersim.LOCATIONS[roundabout]
|
||||
import pdb
|
||||
pdb.set_trace()
|
||||
state_path = 'expert_data/%s/track%04i/joint_expert_states.pt'%(rname, track)
|
||||
action_path = 'expert_data/%s/track%04i/joint_expert_actions.pt'%(rname, track)
|
||||
states = torch.load(state_path)
|
||||
actions = torch.load(action_path)
|
||||
return states, actions
|
||||
|
||||
def evaluate_policy(policy:BaseAlgorithm, locations:List[Tuple[int,int]],
|
||||
env_class:str, env_kwargs:dict) -> List[Dict[str,list]]:
|
||||
def evaluate_policy(locations:List[Tuple[int,int]],
|
||||
env_class:str,
|
||||
env_kwargs:dict,
|
||||
method: str,
|
||||
policy_file: str,
|
||||
policy_kwargs:dict) -> List[Dict[str,list]]:
|
||||
"""
|
||||
Evaluate policy on an incrementing agent environment at all locations.
|
||||
Return metrics for that policy
|
||||
@@ -146,14 +148,16 @@ def evaluate_policy(policy:BaseAlgorithm, locations:List[Tuple[int,int]],
|
||||
locations (list of tuples): list of locations to evaluate policy
|
||||
env_class (str): name of environment to evaluate policy with
|
||||
env_kwargs (dict): key word arguments to initialize environment with
|
||||
method (str): policy method
|
||||
policy_file (str): policy file path
|
||||
policy_kwargs (dict): policy kwargs
|
||||
|
||||
Returns:
|
||||
policy_metrics (list of dicts): policy_metrics[i][j][k] returns the value of metric 'j'
|
||||
evaluated on the kth episode (car) of the ith roundabout trackfile under a policy
|
||||
"""
|
||||
envs_dict = deepcopy(intersim.envs.intersimple.__dict__)
|
||||
envs_dict.update(deepcopy(options_envs.__dict__))
|
||||
|
||||
envs_dict = dict(intersim.envs.intersimple.__dict__)
|
||||
envs_dict.update(dict(options_envs.__dict__))
|
||||
policy_metrics = [None]* len(locations)
|
||||
|
||||
# iterate through vehicles
|
||||
@@ -173,62 +177,71 @@ def evaluate_policy(policy:BaseAlgorithm, locations:List[Tuple[int,int]],
|
||||
Env = envs_dict[env_class]
|
||||
eval_env = Env(**env_kwargs)
|
||||
evaluator = IntersimpleEvaluation(eval_env)
|
||||
|
||||
# load policy
|
||||
policy = load_policy(method, policy_file, policy_kwargs, eval_env)
|
||||
|
||||
# run policy on environment
|
||||
policy_metrics[i] = evaluator.evaluate(policy)
|
||||
|
||||
return policy_metrics
|
||||
|
||||
|
||||
|
||||
def summary_metrics(metrics:List[Dict[str,list]]):
|
||||
def summary_metrics(metrics:List[Dict[str,list]]) -> Dict[str,float]:
|
||||
"""
|
||||
Summarize and print metrics averaged over vehicles and roundabouts
|
||||
|
||||
Args:
|
||||
metrics (list of dicts): policy_metrics[i][j][k] returns the value of metric 'j'
|
||||
evaluated on the kth episode (car) of the ith roundabout trackfile under a policy
|
||||
|
||||
Returns:
|
||||
summary_metrics (Dict[str,float]): maps summary metric descriptions to values
|
||||
"""
|
||||
# keys = ['col_all','v_all', 'a_all','j_all', 'v_avg', 'a_avg', 'col', 'brake', 't']
|
||||
import pdb
|
||||
pdb.set_trace()
|
||||
summary_metrics = {}
|
||||
|
||||
# average average-velocity
|
||||
all_vavgs = sum([d['v_avg'] for d in metrics],[]) # aggregate to single list
|
||||
mean_vavg = sum(all_vavgs)/len(all_vavgs)
|
||||
print(f'Mean Average Velocity: {mean_vavg}')
|
||||
|
||||
summary_metrics['mean average velocity'] = sum(all_vavgs)/len(all_vavgs)
|
||||
|
||||
# average acceleration
|
||||
all_aalls = np.concatenate([np.concatenate(d['a_all']) for d in metrics])
|
||||
summary_metrics['mean acceleartion'] = np.mean(all_aalls)
|
||||
|
||||
# average +acceleration
|
||||
pos_accels = all_aalls[all_aalls>0]
|
||||
mean_pos_accels = np.mean(pos_accels)
|
||||
print(f'Mean positive acceleration: {mean_pos_accels}')
|
||||
summary_metrics['mean positive acceleration'] = np.mean(pos_accels)
|
||||
|
||||
# average deceleration
|
||||
decels = all_aalls[all_aalls<0]
|
||||
mean_decels = np.mean(decels)
|
||||
print(f'Mean positive acceleration: {mean_decels}')
|
||||
summary_metrics['mean deceleration'] = np.mean(decels)
|
||||
|
||||
# average jerk
|
||||
all_jerks = np.concatenate([np.concatenate(d['j_all']) for d in metrics])
|
||||
summary_metrics['mean jerk'] = np.mean(all_jerks)
|
||||
|
||||
# average |jerk|
|
||||
all_jerks = np.concatenate([np.concatenate(d['j_all']) for d in metrics])
|
||||
mean_abs_jerk = np.mean(np.abs(all_jerks))
|
||||
print(f'Mean |Jerk|: {mean_abs_jerk}')
|
||||
summary_metrics['mean |jerk|'] = np.mean(np.abs(all_jerks))
|
||||
|
||||
# collision rate
|
||||
all_collisions = sum([d['col'] for d in metrics],[]) # aggregate to single list
|
||||
collision_rate = sum(all_collisions)/len(all_collisions)
|
||||
print(f'Collision Rate: {collision_rate}')
|
||||
summary_metrics['collision rate'] = sum(all_collisions)/len(all_collisions)
|
||||
|
||||
# hard brake rate
|
||||
all_hard_brakes = sum([d['brake'] for d in metrics],[]) # aggregate to single list
|
||||
hard_brake_rate = sum(all_hard_brakes)/len(all_hard_brakes)
|
||||
print(f'Hard Brake Rate: {hard_brake_rate}')
|
||||
summary_metrics['hard brake rate'] = sum(all_hard_brakes)/len(all_hard_brakes)
|
||||
|
||||
# average number of timesteps
|
||||
all_ts = sum([d['t'] for d in metrics],[]) # aggregate to single list
|
||||
mean_t = sum(all_ts)/len(all_ts)
|
||||
print(f'Mean episode length: {mean_t}')
|
||||
summary_metrics['mean episode length'] = sum(all_ts)/len(all_ts)
|
||||
|
||||
def comparison_metrics(policy_metrics:List[Dict[str,list]], expert_metrics:List[Dict[str,list]]):
|
||||
for key in summary_metrics.keys():
|
||||
print(f'{key}: {summary_metrics[key]}')
|
||||
|
||||
return summary_metrics
|
||||
|
||||
def comparison_metrics(policy_metrics:List[Dict[str,list]],
|
||||
expert_metrics:List[Dict[str,list]]) -> Dict[str,float]:
|
||||
"""
|
||||
Provide distributional comparison between different sets of metrics
|
||||
|
||||
@@ -237,40 +250,42 @@ def comparison_metrics(policy_metrics:List[Dict[str,list]], expert_metrics:List[
|
||||
evaluated on the kth episode (car) of the ith roundabout trackfile under a policy
|
||||
expert_metrics (list of dicts): expert_metrics[i][j][k] returns the value of metric 'j'
|
||||
evaluated on the kth episode (car) of the ith expert roundabout trackfile
|
||||
|
||||
|
||||
Returns:
|
||||
comparison_metrics (Dict[str,float]): dict mapping comparison metric description to value
|
||||
"""
|
||||
import pdb
|
||||
pdb.set_trace()
|
||||
comparison_metrics = {}
|
||||
|
||||
# average velocity shortfall
|
||||
expert_vavg = np.array(sum([d['v_avg'] for d in expert_metrics],[]))
|
||||
policy_vavg = np.array(sum([d['v_avg'] for d in policy_metrics],[]))
|
||||
assert(len(expert_vavg)==len(policy_vavg))
|
||||
mean_shortfall = np.mean(expert_vavg - policy_vavg)
|
||||
print(f'Mean shortfall velocity: {mean_shortfall}')
|
||||
assert len(expert_vavg)==len(policy_vavg)
|
||||
comparison_metrics['mean shortfall velocity'] = np.mean(expert_vavg - policy_vavg)
|
||||
|
||||
# velocity JSD
|
||||
expert_vs = np.concatenate([np.concatenate(d['v_all']) for d in expert_metrics])
|
||||
policy_vs = np.concatenate([np.concatenate(d['v_all']) for d in policy_metrics])
|
||||
vel_div = divergence(expert_vs, policy_vs)
|
||||
print(f'Velocity distribution divergence: {vel_div}')
|
||||
visualize_distribution(expert_vs, policy_vs, 'velocity_jsd.png')
|
||||
expert_vs = torch.tensor(np.concatenate([np.concatenate(d['v_all']) for d in expert_metrics]))
|
||||
policy_vs = torch.tensor(np.concatenate([np.concatenate(d['v_all']) for d in policy_metrics]))
|
||||
comparison_metrics['velocity distribution divergence'] = divergence(expert_vs, policy_vs)
|
||||
visualize_distribution(expert_vs, policy_vs, 'velocity_jsd')
|
||||
|
||||
# acceleration JSD
|
||||
expert_as = np.concatenate([np.concatenate(d['a_all']) for d in expert_metrics])
|
||||
policy_as = np.concatenate([np.concatenate(d['a_all']) for d in policy_metrics])
|
||||
accel_div = divergence(expert_as, policy_as)
|
||||
print(f'Velocity distribution divergence: {accel_div}')
|
||||
visualize_distribution(expert_as, policy_as, 'accel_jsd.png')
|
||||
expert_as = torch.tensor(np.concatenate([np.concatenate(d['a_all']) for d in expert_metrics]))
|
||||
policy_as = torch.tensor(np.concatenate([np.concatenate(d['a_all']) for d in policy_metrics]))
|
||||
comparison_metrics['acceleration distribution divergence'] = divergence(expert_as, policy_as)
|
||||
visualize_distribution(expert_as, policy_as, 'accel_jsd')
|
||||
|
||||
# jerk JSD
|
||||
expert_jerks = np.concatenate([np.concatenate(d['j_all']) for d in expert_metrics])
|
||||
policy_jerks = np.concatenate([np.concatenate(d['j_all']) for d in policy_metrics])
|
||||
jerk_div = divergence(expert_jerks, policy_jerks)
|
||||
print(f'Jerk distribution divergence: {jerk_div}')
|
||||
visualize_distribution(expert_jerks, policy_jerks, 'jerk_jsd.png')
|
||||
expert_jerks = torch.tensor(np.concatenate([np.concatenate(d['j_all']) for d in expert_metrics]))
|
||||
policy_jerks = torch.tensor(np.concatenate([np.concatenate(d['j_all']) for d in policy_metrics]))
|
||||
comparison_metrics['jerk distribution divergence'] = divergence(expert_jerks, policy_jerks)
|
||||
visualize_distribution(expert_jerks, policy_jerks, 'jerk_jsd')
|
||||
|
||||
def test_model(
|
||||
for key in comparison_metrics.keys():
|
||||
print(f'{key}: {comparison_metrics[key]}')
|
||||
|
||||
return comparison_metrics
|
||||
|
||||
def eval_main(
|
||||
locations: List[Tuple[int,int]]= [(0,0)],
|
||||
method: str='expert',
|
||||
policy_file: str='',
|
||||
@@ -289,30 +304,27 @@ def test_model(
|
||||
env (str): environment class
|
||||
method (str): method (expert, bc, gail, rail, hgail, hrail)
|
||||
"""
|
||||
print(f'Evaluating {method} on {env}')
|
||||
|
||||
# set seed
|
||||
np.random.seed(seed)
|
||||
torch.manual_seed(seed)
|
||||
|
||||
# load policy
|
||||
policy = load_policy(policy_file, method, policy_kwargs, skip_load=(method=='expert'))
|
||||
|
||||
# load expert metrics
|
||||
expert_metrics = generate_expert_metrics(locations)
|
||||
|
||||
# if we have a policy
|
||||
if policy:
|
||||
# no comparison for expert
|
||||
if method=='expert':
|
||||
summary_metrics(expert_metrics)
|
||||
|
||||
# otherwise evaluate policy on roundabouts and generate metrics
|
||||
else:
|
||||
|
||||
# evaluate it on the given roundabouts
|
||||
policy_metrics = evaluate_policy(policy, locations, env, env_kwargs)
|
||||
|
||||
#
|
||||
policy_metrics = evaluate_policy(locations, env, env_kwargs, method, policy_file, policy_kwargs)
|
||||
summary_metrics(policy_metrics)
|
||||
comparison_metrics(policy_metrics, expert_metrics)
|
||||
|
||||
else:
|
||||
# if no policy, only generate summary metrics for the expert
|
||||
summary_metrics(expert_metrics)
|
||||
|
||||
if __name__=='__main__':
|
||||
import fire
|
||||
fire.Fire()
|
||||
fire.Fire(eval_main)
|
||||
Reference in New Issue
Block a user