-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathviz.py
67 lines (55 loc) · 2.25 KB
/
viz.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
from deeplearning import tf_util as U
from init import make_env_fn, make_model_fn
from collections import namedtuple
import os, argparse, json
from imageio import imwrite
import subprocess as sp
def main(args):
U.reset()
with open(os.path.join(args.logdir, 'hyps.json'), 'r') as f:
hyps = json.load(f)
train_args = namedtuple('Args', hyps.keys())(**hyps)
env_fn = make_env_fn(train_args)
model_fn = make_model_fn(train_args)
env = env_fn(0)
env.unwrapped.set_render_ground(True)
model = model_fn(env)
model.build('model', 1, 1)
model.sampler.build('model', 1, 1)
sess = U.make_session()
sess.__enter__()
U.initialize()
t = U.Experiment(args.logdir).load(args.ckpt)
# load mode of design distribution
env.update_robot(model.sampler.sample(stochastic=False)[0])
i = 0
if not args.save:
env.reset()
env.render()
else:
outdir = './video_tmp'
os.makedirs(outdir, exist_ok=True)
os.makedirs(os.path.join(args.logdir, 'video'), exist_ok=True)
for j in range(args.nepisodes):
done = False
ob = env.reset()
while not done:
if args.save:
rgb = env.render('rgb_array')
ac = model.actor.mode(ob[None])[0]
ob, rew, done, _ = env.step(ac)
if args.save:
imwrite(os.path.join(outdir, '{:05d}.png'.format(i)), rgb)
i += 1
if args.save:
outfile = str(t) + '.mp4'
sp.call(['ffmpeg', '-r', '60', '-f', 'image2', '-i', os.path.join(outdir, '%05d.png'), '-vcodec', 'libx264', '-pix_fmt', 'yuv420p', os.path.join(outdir, 'out.mp4')])
sp.call(['mv', os.path.join(outdir, 'out.mp4'), os.path.join(args.logdir, 'video', outfile)])
sp.call(['rm', '-rf', outdir])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Make videos')
parser.add_argument('logdir', type=str, help='log directory')
parser.add_argument('-t', '--ckpt', type=int, default=None, help='which checkpoint file to use')
parser.add_argument('-n', '--nepisodes', type=int, default=1, help='n episodes to show')
parser.add_argument('--save', default=False, action='store_true', help='save videos')
main(parser.parse_args())