removing gail-trpo since performance is about the same as gail, adding experiment evaluation script, updating metric averaging to work
This commit is contained in:
@@ -15,7 +15,7 @@ from src.options import envs as options_envs2
|
||||
from src.safe_options.policy import SetMaskedDiscretePolicy
|
||||
from src.safe_options import options as options_envs3
|
||||
from src.util.wrappers import IntersimpleTimeLimit
|
||||
|
||||
import os
|
||||
from typing import Optional, List, Dict, Tuple
|
||||
import torch
|
||||
import numpy as np
|
||||
@@ -44,29 +44,29 @@ def load_policy(method:str,
|
||||
policy = SetPolicy(env.action_space.shape[-1])
|
||||
policy.load_state_dict(torch.load(policy_file, map_location=ml))
|
||||
policy.eval()
|
||||
elif method == 'gail':
|
||||
elif method == 'gail-trpo':
|
||||
policy = SetPolicy(env.action_space.shape[-1])
|
||||
policy(torch.zeros(env.observation_space.shape))
|
||||
policy = ReparamPolicy(policy)
|
||||
policy.load_state_dict(torch.load(policy_file, map_location=ml))
|
||||
policy.eval()
|
||||
elif method == 'gail-ppo':
|
||||
elif method == 'gail':
|
||||
policy = SetPolicy(env.action_space.shape[-1])
|
||||
policy.load_state_dict(torch.load(policy_file, map_location=ml))
|
||||
policy.eval()
|
||||
elif method == 'rail':
|
||||
raise NotImplementedError
|
||||
elif method == 'ogail':
|
||||
elif method == 'hail-trpo':
|
||||
policy = SetDiscretePolicy(env.action_space.n)
|
||||
policy(torch.zeros(env.observation_space.shape))
|
||||
policy = ReparamPolicy(policy)
|
||||
policy.load_state_dict(torch.load(policy_file, map_location=ml))
|
||||
policy.eval()
|
||||
elif method == 'ogail-ppo':
|
||||
elif method == 'hail':
|
||||
policy = SetDiscretePolicy(env.action_space.n)
|
||||
policy.load_state_dict(torch.load(policy_file, map_location=ml))
|
||||
policy.eval()
|
||||
elif method == 'sgail':
|
||||
elif method == 'shail-trpo':
|
||||
policy = SetMaskedDiscretePolicy(env.action_space.n)
|
||||
policy(
|
||||
torch.zeros(env.observation_space['observation'].shape),
|
||||
@@ -75,7 +75,7 @@ def load_policy(method:str,
|
||||
policy = ReparamSafePolicy(policy)
|
||||
policy.load_state_dict(torch.load(policy_file, map_location=ml))
|
||||
policy.eval()
|
||||
elif method == 'sgail-ppo':
|
||||
elif method == 'shail':
|
||||
policy = SetMaskedDiscretePolicy(env.action_space.n)
|
||||
policy.load_state_dict(torch.load(policy_file, map_location=ml))
|
||||
policy.eval()
|
||||
@@ -375,6 +375,9 @@ def eval_main(
|
||||
policy_file (str): path to saved policy
|
||||
env (str): environment class
|
||||
method (str): method (expert, bc, gail, rail, hgail, hrail)
|
||||
|
||||
Returns:
|
||||
outbase (str): string to outbase
|
||||
"""
|
||||
print(f'#############################################################################')
|
||||
print(f'Evaluating {method} from file {policy_file} on {env} at locations {locations}')
|
||||
@@ -383,9 +386,17 @@ def eval_main(
|
||||
# set seed
|
||||
np.random.seed(seed)
|
||||
torch.manual_seed(seed)
|
||||
pfilename = policy_file.split('/')[-1].split('.')[0]
|
||||
locstr = 'loc_'+'_'.join([f'r{ro}t{tr}' for (ro,tr) in locations])
|
||||
outbase = f'out/{method}/{locstr}/{pfilename}_seed{seed}'
|
||||
if policy_file == '':
|
||||
method_path = method
|
||||
name_base = method
|
||||
else:
|
||||
path_items = policy_file.split('/')
|
||||
name_base = path_items[-1].split('.')[0]
|
||||
method_path = ('/').join(path_items[1:-1])
|
||||
outfolder = os.path.join('out',method_path,locstr)
|
||||
filebase = name_base + f'_tseed{seed}'
|
||||
outbase = os.path.join(outfolder,filebase)
|
||||
|
||||
# load expert metrics
|
||||
expert_metrics = generate_expert_metrics(locations)
|
||||
@@ -404,6 +415,8 @@ def eval_main(
|
||||
save_metrics(smetrics, outbase+'_summary.pkl')
|
||||
cmetrics = comparison_metrics(policy_metrics, expert_metrics, outbase=outbase)
|
||||
save_metrics(cmetrics, outbase+'_comparison.pkl')
|
||||
|
||||
return outbase
|
||||
|
||||
if __name__=='__main__':
|
||||
import fire
|
||||
|
||||
@@ -34,13 +34,20 @@ def load_metrics(filestr:str):
|
||||
metrics = pickle.load(f)
|
||||
return metrics
|
||||
|
||||
def average_metrics(metric_list:List[Dict[str,float]]):
|
||||
def average_metrics(metric_list:List[Dict[str,float]], verbose:bool=True) ->Dict[str, tuple]:
|
||||
"""
|
||||
Average all the metrics in the list
|
||||
|
||||
Args:
|
||||
metric_list (list of dicts): list of metric dicts which each map a string to a float
|
||||
verbose (bool): whether to print avg metrics
|
||||
Returns:
|
||||
average_metrics (Dict[str, tuple])
|
||||
"""
|
||||
average_metrics = {}
|
||||
if len(metric_list) == 0:
|
||||
return average_metrics
|
||||
|
||||
keys = list(metric_list[0].keys())
|
||||
N = len(metric_list)
|
||||
master_dict = {key:[] for key in keys}
|
||||
@@ -50,29 +57,39 @@ def average_metrics(metric_list:List[Dict[str,float]]):
|
||||
master_dict[key] = np.array(master_dict[key])
|
||||
mu = np.nanmean(master_dict[key])
|
||||
std2 = np.nanstd(master_dict[key])*2
|
||||
print(f'{key}: {mu} \pm {std2}')
|
||||
if verbose:
|
||||
print(f'{key}: {mu} \pm {std2}')
|
||||
average_metrics[key] = (mu, std2)
|
||||
return average_metrics
|
||||
|
||||
|
||||
def load_and_average(path:str):
|
||||
def load_and_average(path:str, verbose:bool=True):
|
||||
"""
|
||||
Load and average all metric files in a particular folder
|
||||
|
||||
Args:
|
||||
path (str)
|
||||
verbose (bool): whether to print avg metrics
|
||||
Returns:
|
||||
avg_metrics (Dict[str, tuple])
|
||||
"""
|
||||
assert os.path.isdir(path)
|
||||
|
||||
# summary metrics
|
||||
summary_files = [os.path.join(path,f) for f in os.listdir(path) if f.endswith('summary.pkl')]
|
||||
print(*summary_files, sep='\n')
|
||||
if verbose:
|
||||
print(*summary_files, sep='\n')
|
||||
all_summary_metrics = [load_metrics(f) for f in summary_files]
|
||||
average_metrics(all_summary_metrics)
|
||||
avg_metrics = average_metrics(all_summary_metrics, verbose=verbose)
|
||||
|
||||
# comparison metrics
|
||||
comp_files = [os.path.join(path,f) for f in os.listdir(path) if f.endswith('comparison.pkl')]
|
||||
print(*comp_files, sep='\n')
|
||||
if verbose:
|
||||
print(*comp_files, sep='\n')
|
||||
all_comp_metrics = [load_metrics(f) for f in comp_files]
|
||||
average_metrics(all_comp_metrics)
|
||||
comp_avg = average_metrics(all_comp_metrics, verbose=verbose)
|
||||
avg_metrics.update(comp_avg)
|
||||
return avg_metrics
|
||||
|
||||
if __name__=='__main__':
|
||||
import fire
|
||||
|
||||
Reference in New Issue
Block a user