-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
179 lines (162 loc) · 5.15 KB
/
visualize.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
"""
Partially from https://github.com/RobertTLange/gymnax-blines
"""
import numpy as np
import jax
from tqdm import tqdm
from utils.models import get_model_ready
from utils.helpers import load_pkl_object
from terra.env import TerraEnvBatch
import jax.numpy as jnp
from utils.utils_ppo import obs_to_model_input, wrap_action
from terra.state import State
import matplotlib.animation as animation
# from utils.curriculum import Curriculum
from tensorflow_probability.substrates import jax as tfp
from train import TrainConfig # needed for unpickling checkpoints
from terra.config import EnvConfig
def load_neural_network(config, env):
rng = jax.random.PRNGKey(0)
model, _ = get_model_ready(rng, config, env)
return model
def rollout_episode(
env: TerraEnvBatch, model, model_params, env_cfgs, rl_config, max_frames, seed
):
print(f"Using {seed=}")
rng = jax.random.PRNGKey(seed)
rng, _rng = jax.random.split(rng)
rng_reset = jax.random.split(_rng, rl_config.num_test_rollouts)
timestep = env.reset(env_cfgs, rng_reset)
t_counter = 0
reward_seq = []
obs_seq = []
while True:
obs_seq.append(timestep.observation)
rng, rng_act, rng_step = jax.random.split(rng, 3)
if model is not None:
obs = obs_to_model_input(timestep.observation, rl_config)
v, logits_pi = model.apply(model_params, obs)
pi = tfp.distributions.Categorical(logits=logits_pi)
action = pi.sample(seed=rng_act)
else:
raise RuntimeError("Model is None!")
rng_step = jax.random.split(rng_step, rl_config.num_test_rollouts)
timestep = env.step(
timestep, wrap_action(action, env.batch_cfg.action_type), rng_step
)
reward_seq.append(timestep.reward)
print(t_counter, timestep.reward, action, timestep.done)
print(10 * "=")
t_counter += 1
# if done or t_counter == max_frames:
# break
# else:
if jnp.all(timestep.done).item() or t_counter == max_frames:
break
# env_state = next_env_state
# obs = next_obs
print(f"Terra - Steps: {t_counter}, Return: {np.sum(reward_seq)}")
return obs_seq, np.cumsum(reward_seq)
def update_render(seq, env: TerraEnvBatch, frame):
obs = {k: v[:, frame] for k, v in seq.items()}
return env.terra_env.render_obs(obs, mode="gif")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"-run",
"--run_name",
type=str,
default="ppo_2023_05_09_10_01_23",
help="es/ppo trained agent.",
)
parser.add_argument(
"-env",
"--env_name",
type=str,
default="Terra",
help="Environment name.",
)
parser.add_argument(
"-nx",
"--n_envs_x",
type=int,
default=1,
help="Number of environments on x.",
)
parser.add_argument(
"-ny",
"--n_envs_y",
type=int,
default=1,
help="Number of environments on y.",
)
parser.add_argument(
"-steps",
"--n_steps",
type=int,
default=10,
help="Number of steps.",
)
parser.add_argument(
"-o",
"--out_path",
type=str,
default=".",
help="Output path.",
)
parser.add_argument(
"-s",
"--seed",
type=int,
default=0,
help="Random seed for the environment.",
)
parser.add_argument(
"-pg",
"--progressive_gif",
type=int,
default=0,
help="Random seed for the environment.",
)
args, _ = parser.parse_known_args()
n_envs = args.n_envs_x * args.n_envs_y
log = load_pkl_object(f"{args.run_name}")
config = log["train_config"]
config.num_test_rollouts = n_envs
config.num_devices = 1
# curriculum = Curriculum(rl_config=config, n_devices=n_devices)
# env_cfgs, dofs_count_dict = curriculum.get_cfgs_eval()
env_cfgs = log["env_config"]
env_cfgs = jax.tree_map(
lambda x: x[0][None, ...].repeat(n_envs, 0), env_cfgs
) # take first config and replicate
progressive_gif = bool(args.progressive_gif)
print(f"Using progressive_gif = {progressive_gif}")
suffle_maps = True
env = TerraEnvBatch(
rendering=True,
n_envs_x_rendering=args.n_envs_x,
n_envs_y_rendering=args.n_envs_y,
display=False,
progressive_gif=args.progressive_gif,
rendering_engine="pygame",
shuffle_maps=suffle_maps,
)
config.num_embeddings_agent_min = 60 # curriculum.get_num_embeddings_agent_min()
model = load_neural_network(config, env)
model_params = log["model"]
# replicated_params = log['network']
# model_params = jax.tree_map(lambda x: x[0], replicated_params)
obs_seq, cum_rewards = rollout_episode(
env,
model,
model_params,
env_cfgs,
config,
max_frames=args.n_steps,
seed=args.seed,
)
for o in tqdm(obs_seq, desc="Rendering"):
env.terra_env.render_obs_pygame(o, generate_gif=True)
env.terra_env.rendering_engine.create_gif(args.out_path)