forked from jbkjr/train-procgen-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_metrics.py
170 lines (137 loc) · 7.31 KB
/
compute_metrics.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
from torch._C import Value
from test import load_env_and_agent, run
import argparse
from common import set_global_seeds, set_global_log_levels
import numpy as np
from pathlib import Path
import csv
if __name__=='__main__':
raise NotImplementedError("I made changes to test.py, so now this script needs to be overhauled")
parser = argparse.ArgumentParser()
parser.add_argument('--random_percent_model_dir', type=str, default=None,
help="directory with saved coinrun models trained on "
"environments with coin position randomized "
"0, 1, 2, 5, and 10 percent of the time.")
parser.add_argument('--num_levels_model_dir', type=str, default=None,
help="directory with saved coinrun models trained on "
"environments with different numbers of "
"distinct levels.")
parser.add_argument('--results_dir', type=str, default=None)
parser.add_argument('--num_timesteps', type=int, default = 10_000)
parser.add_argument('--exp_name', type=str, default = 'compute_metrics', help='experiment name')
parser.add_argument('--start_level', type=int, default = np.random.randint(0, 10**9), help='start-level for environment')
parser.add_argument('--distribution_mode',type=str, default = 'hard', help='distribution mode for environment')
parser.add_argument('--param_name', type=str, default = 'hard', help='hyper-parameter ID')
parser.add_argument('--device', type=str, default = 'cpu', required = False, help='whether to use gpu')
parser.add_argument('--gpu_device', type=int, default = int(0), required = False, help = 'visible device in CUDA')
parser.add_argument('--seed', type=int, default = np.random.randint(0,9999), help='Random generator seed')
parser.add_argument('--log_level', type=int, default = int(40), help='[10,20,30,40]')
parser.add_argument('--logdir', type=str, default = None)
parser.add_argument('--num_threads', type=int, default=8)
parser.add_argument('--num_envs', type=int, default=1)
args = parser.parse_args()
set_global_seeds(args.seed)
set_global_log_levels(args.log_level)
# deploy saved models in --random_percent_model_dir and compute
# how often the models navigate to the end of the level instead of getting
# the coin
def random_percent_ablation():
def get_agent_path(random_percent):
"""return path of saved agent trained on env with coin randomized
random_percent of the time"""
model_path = Path(args.random_percent_model_dir)
model_path = model_path / f"random_percent_{random_percent}" / "model_200015872.pth"
return model_path
if args.results_dir:
results_dir = Path(args.results_dir)
else:
results_dir = Path(args.random_percent_model_dir)
for random_percent in [0, 1, 2, 5, 10]:
model_path = get_agent_path(random_percent)
print(f"Loading agent trained on distribution random_percent_{random_percent}")
print(f"Loading from {model_path}...")
print()
agent = load_env_and_agent(exp_name=args.exp_name,
env_name="coinrun",
num_envs=args.num_envs,
logdir=args.logdir,
model_file=model_path,
start_level=args.start_level,
num_levels=0, # this means start_level is meaningless (level seeds are drawn randomly)
distribution_mode=args.distribution_mode,
param_name=args.param_name,
device=args.device,
gpu_device=args.gpu_device,
seed=args.seed,
num_checkpoints=0,
random_percent=100,
num_threads=args.num_threads)
print()
print("Running...")
results = run(agent, args.num_timesteps, args.logdir)
results.update({"random_percent": random_percent})
results_file = str(results_dir / "results.csv")
print()
print(f"Saving results to {results_file}")
print()
# write results to csv
if random_percent == 0:
with open(results_file, "w") as f:
w = csv.DictWriter(f, results.keys())
w.writeheader()
w.writerow(results)
else:
with open(results_file, "a") as f:
w = csv.DictWriter(f, results.keys())
w.writerow(results)
def num_levels_ablation():
def get_agent_path(num_levels):
model_path = Path(args.num_levels_model_dir)
model_path = model_path / f"nr_levels_{num_levels}" / "model_200015872.pth"
return model_path
if args.results_dir:
results_dir = Path(args.results_dir)
else:
results_dir = Path(args.num_levels_model_dir)
for num_levels in [100, 316, 1000, 3160, 10_000, 31_600, 100_000]:
model_path = get_agent_path(num_levels)
print(f"Loading agent trained on distribution nr_levels_{num_levels}")
print(f"Loading from {model_path}...")
print()
agent = load_env_and_agent(exp_name=args.exp_name,
env_name="coinrun",
num_envs=args.num_envs,
logdir=args.logdir,
model_file=model_path,
start_level=args.start_level,
num_levels=0,
distribution_mode=args.distribution_mode,
param_name=args.param_name,
device=args.device,
gpu_device=args.gpu_device,
seed=args.seed,
num_checkpoints=0,
random_percent=100,
num_threads=args.num_threads)
print()
print("Running...")
results = run(agent, args.num_timesteps, args.logdir)
results.update({"num_levels": num_levels})
results_file = str(results_dir / "results.csv")
print()
print(f"Saving results to {results_file}")
print()
# write results to csv
if num_levels == 100:
with open(results_file, "w") as f:
w = csv.DictWriter(f, results.keys())
w.writeheader()
w.writerow(results)
else:
with open(results_file, "a") as f:
w = csv.DictWriter(f, results.keys())
w.writerow(results)
if args.random_percent_model_dir:
random_percent_ablation()
if args.num_levels_model_dir:
num_levels_ablation()