This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
generated from minerllabs/basalt_2022_competition_submission_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_tester.py
195 lines (141 loc) · 6.34 KB
/
main_tester.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
from argparse import ArgumentParser
import os
from fractal_zero.data.tree_sampler import TreeSampler
from fractal_zero.search.fmc import FMC
import minerl
import gym
import cv2
import torch
import numpy as np
from vpt.agent import AGENT_RESOLUTION
from fgz.architecture.xirl_model import XIRLModel
from xirl_zero.main_trainer import Trainer, now_filename
from xirl_zero.architecture.dynamics_function import DynamicsFunction, MineRLDynamicsEnvironment
from tqdm import tqdm
from xirl_zero.search.dynamics_fmc import DynamicsFMC
class Tester:
env: gym.Env = None
representation_function: XIRLModel
dynamics_function: DynamicsFunction
def __init__(self, path_to_experiment: str, iteration: int=None, device=None):
self.path_to_experiment = path_to_experiment
checkpoint_dir = os.path.join(path_to_experiment, "checkpoints")
# default pick the last iteration
if iteration is None:
iterations = [int(fn.split(".")[0]) for fn in os.listdir(checkpoint_dir)]
iteration = max(iterations)
self.iteration = iteration
fn = f"{iteration}.pth"
checkpoint_path = os.path.join(checkpoint_dir, fn)
target_state_path = os.path.join(path_to_experiment, "target_states", fn)
print(f"Loading {fn} checkpoint and target state from {path_to_experiment}")
trainer: Trainer = torch.load(checkpoint_path, map_location=device)
target_state: torch.Tensor = torch.load(target_state_path, map_location=device)
# TODO: handle this better?
self.device = device if device is not None else trainer.representation_trainer.config.device
self.minerl_env_id = trainer.config.minerl_env_id
self.representation_function = trainer.representation_trainer.model
self.dynamics_function = trainer.dynamics_trainer.model
self.target_state = target_state
self.new_video_paths = []
def load_environment(self, env: gym.Env=None):
if env is None:
env = gym.make(self.minerl_env_id)
actual_env_id = env.unwrapped.spec.id
if actual_env_id != self.minerl_env_id:
raise ValueError(f"Cross-task testing is not recommended. The actual env ID loaded was {actual_env_id}, but we expected {self.minerl_env_id}.")
self.env = env
def get_action(self, obs, force_no_escape: bool):
self.representation_function.eval()
self.dynamics_function.eval()
# x = torch.tensor(obs["pov"], device=self.device)
x = self.representation_function.prepare_observation(obs).to(self.device)
state = self.representation_function.embed(x).squeeze()
fmc = DynamicsFMC(
self.dynamics_function,
self.target_state,
self.env.action_space,
num_walkers=512,
steps=128,
balance=3.0,
)
actions = fmc.get_actions(state)
action_percent = 0.5
max_action_index = max(1, int(np.ceil(len(actions) * action_percent)))
print("total actions", len(actions), "max", max_action_index)
actions = actions[:max_action_index].flatten().tolist()
for action in actions:
if force_no_escape:
action["ESC"] = 0
return actions
def play_episode(
self,
min_steps: int,
max_steps: int,
render: bool = False,
smoke_test: bool = False,
use_tqdm: bool = True,
save_video: bool = False,
):
if self.env is None:
raise ValueError("load_environment must be called first.")
if smoke_test:
obs = np.random.uniform(size=(*AGENT_RESOLUTION, 3))
else:
obs = self.env.reset()
if save_video:
video_dir = os.path.join(self.path_to_experiment, "videos")
os.makedirs(video_dir, exist_ok=True)
video_path = os.path.join(video_dir, f"{self.iteration}___{now_filename()}.mp4")
resolution = AGENT_RESOLUTION if smoke_test else (640, 360)
video_recorder = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*"mp4v"), 20, resolution)
print(f"Saving video at {video_path}")
self.new_video_paths.append(video_path)
self.play_step = 0
def _after_step():
if save_video:
if smoke_test:
frame = obs[..., ::-1].astype(np.uint8)
else:
frame = obs["pov"][..., ::-1]
video_recorder.write(frame)
if render:
self.env.render()
self.play_step += 1
# for step in tqdm(range(max_steps), desc=f"Playing {self.minerl_env_id} Episode", disable=not use_tqdm):
while True:
actions = self.get_action(obs, force_no_escape=self.play_step < min_steps)
if smoke_test:
obs = np.random.uniform(size=(*AGENT_RESOLUTION, 3))
reward = 0
done = False
info = {}
_after_step()
else:
for action in actions:
obs, reward, done, info = self.env.step(action)
_after_step()
if done:
break
if done or self.play_step >= max_steps:
break
self.env.close()
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--experiment-directory", type=str)
parser.add_argument("--iteration", type=int, default=None, help="The checkpoint iteration to use.")
parser.add_argument("--min-steps", type=int, default=16)
parser.add_argument("--max-steps", type=int, default=64)
parser.add_argument("--smoke-test", action="store_true")
parser.add_argument("--save-video", action="store_true")
parser.add_argument("--render", action="store_true")
parser.add_argument("--force-cpu", action="store_true")
args = dict(parser.parse_args().__dict__)
# tester = Tester("./train/xirl_zero/MineRLBasaltMakeWaterfall-v0/2022-10-28_02-52-40_PM")
experiment_dir = args.pop("experiment_directory")
device = None
if args.pop("force_cpu"):
device = torch.device("cpu")
tester = Tester(experiment_dir, iteration=args.pop("iteration"), device=device)
tester.load_environment()
tester.play_episode(**args)