-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevaluate.py
164 lines (145 loc) · 7.18 KB
/
evaluate.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
import argparse
import os
import glob
import re
import time
import numpy as np
import importlib
import torch
from torch_ac.utils.penv import ParallelEnv
import tensorboardX
import utils
from utils import device
import crafter
if __name__ == "__main__":
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("--env", required=True,
help="name of the environment (REQUIRED)")
parser.add_argument("--model", required=True,
help="name of the trained model (REQUIRED)")
parser.add_argument("--episodes", type=int, default=100,
help="number of episodes of evaluation (default: 100)")
parser.add_argument("--seed", type=int, default=0,
help="random seed (default: 0)")
parser.add_argument("--procs", type=int, default=16,
help="number of processes (default: 16)")
parser.add_argument("--argmax", action="store_true", default=False,
help="action with highest probability is selected")
parser.add_argument("--worst-episodes-to-show", type=int, default=10,
help="how many worst episodes to show")
parser.add_argument("--record-video", action="store_true", default=False,
help="record evaluation videos")
parser.add_argument("--all-checkpoints", action="store_true", default=False,
help="evaluate all checkpoints saved in model dir")
args = parser.parse_args()
# assertions to ensure that metrics are logged properly
assert(args.episodes % args.procs == 0)
# Set seed for all randomness sources
utils.seed(args.seed)
# Set device
print(f"Device: {device}\n")
# Load environments
env_module = importlib.import_module(f'envs.env_{args.env}')
envs = []
model_dir = utils.get_model_dir(args.model)
for i in range(args.procs):
env = env_module.Env(seed=args.seed + 100 * i)
if args.record_video:
env = crafter.Recorder(
env, f"{model_dir}",
save_stats=False,
save_video=True,
save_episode=False,
)
envs.append(env)
env = ParallelEnv(envs)
print("Environments loaded\n")
if args.all_checkpoints:
# Load Tensorboard writer
tb_writer = tensorboardX.SummaryWriter(model_dir)
# Find checkpoints
checkpoints = []
if args.all_checkpoints:
filepath = os.path.join(os.getcwd(), model_dir)
files = glob.glob(filepath + "/status_*.pt")
checkpoints = [re.search(f"{filepath}/status_(.*).pt", f).group(1) for f in files]
checkpoints = sorted(checkpoints, key=lambda x: (len(x), x))
checkpoints += [""]
for checkpoint in checkpoints:
# Load agent
agent = utils.Agent.dir_init(env.observation_space, env.action_space, model_dir,
argmax=args.argmax, num_envs=args.procs, model_suffix=checkpoint)
print("Agent loaded\n")
# Initialize logs
logs = {"num_frames_per_episode": [], "return_per_episode": []}
logs_info_startkeys = [
# "craftscore", "craftscore_followed",
# "craftscore_int",
# "craftscore_bor_followed", "craftscore_int_followed"
]
logs_info = {k: [] for k in logs_info_startkeys}
# Run agent
start_time = time.time()
obss = env.reset()
log_done_counter = 0
log_episode_return = torch.zeros(args.procs, device=device)
log_episode_num_frames = torch.zeros(args.procs, device=device)
while log_done_counter < args.episodes:
actions = agent.get_actions(obss)
obss, rewards, terminateds, truncateds, infos = env.step(actions)
dones = tuple(a | b for a, b in zip(terminateds, truncateds))
agent.analyze_feedbacks(rewards, dones)
log_episode_return += torch.tensor(rewards, device=device, dtype=torch.float)
log_episode_num_frames += torch.ones(args.procs, device=device)
for i, done in enumerate(dones):
if done:
log_done_counter += 1
logs["return_per_episode"].append(log_episode_return[i].item())
logs["num_frames_per_episode"].append(log_episode_num_frames[i].item())
# get metrics in logs_info
for key in logs_info_startkeys:
logs_info[key].append(infos[i][key])
# get follow, given achievemenets metrics
if "taskcond" in args.env:
all_given_counts = 0
all_follow_counts = 0
for key, val in infos[i]["given_achs"].items():
logs_key = f'given_{key}'
logs_info[logs_key] = logs_info.get(logs_key, [])
logs_info[logs_key].append(val)
all_given_counts += val
logs_key = f'fpercent_{key}'
fpercent = val and infos[i]["follow_achs"][key] / val or 0
logs_info[logs_key] = logs_info.get(logs_key, [])
logs_info[logs_key].append(fpercent)
all_follow_counts += infos[i]["follow_achs"][key]
logs_info["follow_percent"] = logs_info.get("follow_percent", [])
logs_info["follow_percent"].append(all_follow_counts / all_given_counts)
mask = 1 - torch.tensor(dones, device=device, dtype=torch.float)
log_episode_return *= mask
log_episode_num_frames *= mask
end_time = time.time()
# Print logs
num_frames = sum(logs["num_frames_per_episode"])
fps = num_frames / (end_time - start_time)
duration = int(end_time - start_time)
return_per_episode = utils.synthesize(logs["return_per_episode"])
num_frames_per_episode = utils.synthesize(logs["num_frames_per_episode"])
print("F {} | FPS {:.0f} | D {} | R:μσmM {:.2f} {:.2f} {:.2f} {:.2f} | F:μσmM {:.1f} {:.1f} {} {}"
.format(num_frames, fps, duration,
*return_per_episode.values(),
*num_frames_per_episode.values()))
# Write to Tensorboard
header = [f"eval/{h}" for h in logs_info.keys()]
data = [np.median(v) if type(v)==list else v/args.episodes for k, v in logs_info.items()]
if checkpoint:
for field, value in zip(header, data):
tb_writer.add_scalar(field, value, int(checkpoint))
# Print worst episodes
n = args.worst_episodes_to_show
if n > 0:
print("\n{} worst episodes:".format(n))
indexes = sorted(range(len(logs["return_per_episode"])), key=lambda k: logs["return_per_episode"][k])
for i in indexes[:n]:
print("- episode {}: R={}, F={}".format(i, logs["return_per_episode"][i], logs["num_frames_per_episode"][i]))