forked from mila-iqia/babyai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_agent_demos.py
executable file
·219 lines (179 loc) · 7.77 KB
/
make_agent_demos.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
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
#!/usr/bin/env python3
"""
Generate a set of agent demonstrations.
The agent can either be a trained model or the heuristic expert (bot).
Demonstration generation can take a long time, but it can be parallelized
if you have a cluster at your disposal. Provide a script that launches
make_agent_demos.py at your cluster as --job-script and the number of jobs as --jobs.
"""
import argparse
import gym
import logging
import sys
import subprocess
import os
import time
import numpy as np
import blosc
import torch
import babyai.utils as utils
# Parse arguments
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--env", required=True,
help="name of the environment to be run (REQUIRED)")
parser.add_argument("--model", default='BOT',
help="name of the trained model (REQUIRED)")
parser.add_argument("--demos", default=None,
help="path to save demonstrations (based on --model and --origin by default)")
parser.add_argument("--episodes", type=int, default=1000,
help="number of episodes to generate demonstrations for")
parser.add_argument("--valid-episodes", type=int, default=512,
help="number of validation episodes to generate demonstrations for")
parser.add_argument("--seed", type=int, default=1,
help="random seed")
parser.add_argument("--shift", type=int, default=0,
help="skip this many mission from the given seed")
parser.add_argument("--argmax", action="store_true", default=False,
help="action with highest probability is selected")
parser.add_argument("--log-interval", type=int, default=100,
help="interval between progress reports")
parser.add_argument("--save-interval", type=int, default=10000,
help="interval between demonstrations saving")
parser.add_argument("--filter-steps", type=int, default=0,
help="filter out demos with number of steps more than filter-steps")
parser.add_argument("--on-exception", type=str, default='warn', choices=('warn', 'crash'),
help="How to handle exceptions during demo generation")
parser.add_argument("--job-script", type=str, default=None,
help="The script that launches make_agent_demos.py at a cluster.")
parser.add_argument("--jobs", type=int, default=0,
help="Split generation in that many jobs")
args = parser.parse_args()
logger = logging.getLogger(__name__)
# Set seed for all randomness sources
if args.seed == 0:
raise ValueError("seed == 0 is reserved for validation purposes")
def print_demo_lengths(demos):
num_frames_per_episode = [len(demo[2]) for demo in demos]
logger.info('Demo length: {:.3f}+-{:.3f}'.format(
np.mean(num_frames_per_episode), np.std(num_frames_per_episode)))
def generate_demos(n_episodes, valid, seed, shift=0):
utils.seed(seed)
# Generate environment
env = gym.make(args.env)
env.seed(seed)
for i in range(shift):
env.reset()
agent = utils.load_agent(env, args.model, args.demos, 'agent', args.argmax, args.env)
demos_path = utils.get_demos_path(args.demos, args.env, 'agent', valid)
demos = []
checkpoint_time = time.time()
while True:
# Run the expert for one episode
done = False
obs = env.reset()
agent.on_reset()
actions = []
mission = obs["mission"]
images = []
directions = []
try:
while not done:
action = agent.act(obs)['action']
if isinstance(action, torch.Tensor):
action = action.item()
new_obs, reward, done, _ = env.step(action)
agent.analyze_feedback(reward, done)
actions.append(action)
images.append(obs['image'])
directions.append(obs['direction'])
obs = new_obs
if reward > 0 and (args.filter_steps == 0 or len(images) <= args.filter_steps):
demos.append((mission, blosc.pack_array(np.array(images)), directions, actions))
if len(demos) >= n_episodes:
break
if reward == 0:
if args.on_exception == 'crash':
raise Exception("mission failed")
logger.info("mission failed")
except Exception:
if args.on_exception == 'crash':
raise
logger.exception("error while generating demo #{}".format(len(demos)))
continue
if len(demos) and len(demos) % args.log_interval == 0:
now = time.time()
demos_per_second = args.log_interval / (now - checkpoint_time)
to_go = (n_episodes - len(demos)) / demos_per_second
logger.info("demo #{}, {:.3f} demos per second, {:.3f} seconds to go".format(
len(demos), demos_per_second, to_go))
checkpoint_time = now
# Save demonstrations
if args.save_interval > 0 and len(demos) < n_episodes and len(demos) % args.save_interval == 0:
logger.info("Saving demos...")
utils.save_demos(demos, demos_path)
logger.info("Demos saved")
# print statistics for the last 100 demonstrations
print_demo_lengths(demos[-100:])
# Save demonstrations
logger.info("Saving demos...")
utils.save_demos(demos, demos_path)
logger.info("Demos saved")
print_demo_lengths(demos[-100:])
def generate_demos_cluster():
demos_per_job = args.episodes // args.jobs
demos_path = utils.get_demos_path(args.demos, args.env, 'agent')
job_demo_names = [os.path.realpath(demos_path + '.shard{}'.format(i))
for i in range(args.jobs)]
for demo_name in job_demo_names:
job_demos_path = utils.get_demos_path(demo_name)
if os.path.exists(job_demos_path):
os.remove(job_demos_path)
command = [args.job_script]
command += sys.argv[1:]
for i in range(args.jobs):
cmd_i = list(map(str,
command
+ ['--seed', args.seed + i]
+ ['--demos', job_demo_names[i]]
+ ['--episodes', demos_per_job]
+ ['--jobs', 0]
+ ['--valid-episodes', 0]))
logger.info('LAUNCH COMMAND')
logger.info(cmd_i)
output = subprocess.check_output(cmd_i)
logger.info('LAUNCH OUTPUT')
logger.info(output.decode('utf-8'))
job_demos = [None] * args.jobs
while True:
jobs_done = 0
for i in range(args.jobs):
if job_demos[i] is None or len(job_demos[i]) < demos_per_job:
try:
logger.info("Trying to load shard {}".format(i))
job_demos[i] = utils.load_demos(utils.get_demos_path(job_demo_names[i]))
logger.info("{} demos ready in shard {}".format(
len(job_demos[i]), i))
except Exception:
logger.exception("Failed to load the shard")
if job_demos[i] and len(job_demos[i]) == demos_per_job:
jobs_done += 1
logger.info("{} out of {} shards done".format(jobs_done, args.jobs))
if jobs_done == args.jobs:
break
logger.info("sleep for 60 seconds")
time.sleep(60)
# Training demos
all_demos = []
for demos in job_demos:
all_demos.extend(demos)
utils.save_demos(all_demos, demos_path)
logging.basicConfig(level='INFO', format="%(asctime)s: %(levelname)s: %(message)s")
logger.info(args)
# Training demos
if args.jobs == 0:
generate_demos(args.episodes, False, args.seed, args.shift)
else:
generate_demos_cluster()
# Validation demos
if args.valid_episodes:
generate_demos(args.valid_episodes, True, 0)