This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval.py
202 lines (168 loc) · 6.22 KB
/
eval.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
import random
from typing import Tuple, Optional
from copy import deepcopy
from pathlib import Path
import torch
import numpy as np
import tap
from filelock import FileLock
from network import Hiveformer
from utils import (
RLBenchEnv,
load_episodes,
get_max_episode_length,
Actioner,
load_instructions,
)
from train import Arguments as TrainArguments
class Arguments(tap.Tap):
checkpoint: Path
seed: int = 2
save_img: bool = False
device: str = "cuda"
num_episodes: int = 100
headless: bool = False
offset: int = 0
name: str = "autol"
max_tries: int = 10
output: Path = Path(__file__).parent / "records.txt"
xp: Path = Path(__file__).parent / "xp"
test_xp: Path = Path(__file__).parent / "test-xp"
data_dir: Path = Path(__file__).parent / "demos"
record_actions: bool = False
replay_actions: Optional[Path] = None
ground_truth_rotation: bool = False
ground_truth_position: bool = False
ground_truth_gripper: bool = False
tasks: Optional[Tuple[str, ...]] = None
instructions: Optional[Path] = None
arch: Optional[str] = None
variations: Tuple[int, ...] = (0,)
attention: bool = False # saving attention maps
# model
depth: Optional[int] = None
dim_feedforward: Optional[int] = None
hidden_dim: Optional[int] = None
instr_size: Optional[int] = None
mask_obs_prob: float = 0.0
num_layers: Optional[int] = None
def get_log_dir(args: Arguments) -> Path:
log_dir = args.test_xp / args.name
version = 0
while (log_dir / f"test-version{version}").is_dir():
version += 1
return log_dir / f"test-version{version}"
def copy_args(checkpoint: Path, args: Arguments) -> Arguments:
args = deepcopy(args)
if not checkpoint.is_file():
files = list((args.xp / checkpoint).rglob("mtl_*.pth"))
assert files != [], args.checkpoint
files = sorted(files, key=lambda x: x.stat().st_mtime)
checkpoint = files[0]
print("Copying args from", checkpoint)
# Update args accordingly:
hparams = checkpoint.parent / "hparams.json"
print(hparams, hparams.is_file())
if hparams.is_file():
print("Loading args from checkpoint")
train_args = TrainArguments()
train_args.load(str(hparams))
for key in args.class_variables:
v = getattr(args, key)
if v is None and key in train_args.class_variables:
setattr(args, key, getattr(train_args, key))
print("Copying", key, ":", getattr(args, key))
return args
def load_model(checkpoint: Path, args: Arguments) -> Hiveformer:
args = copy_args(checkpoint, args)
device = torch.device(args.device)
if not checkpoint.is_file():
files = list((args.xp / checkpoint).rglob("mtl_*.pth"))
assert files != [], args.checkpoint
files = sorted(files, key=lambda x: x.stat().st_mtime)
checkpoint = files[0]
print("Loading model from...", checkpoint, flush=True)
if args.tasks is None:
raise RuntimeError("Can't find tasks")
if (
args.depth is None
or args.dim_feedforward is None
or args.hidden_dim is None
or args.instr_size is None
or args.mask_obs_prob is None
or args.num_layers is None
):
raise ValueError("Please provide the missing parameters")
max_episode_length = get_max_episode_length(args.tasks, args.variations)
model = Hiveformer(
depth=args.depth,
dim_feedforward=args.dim_feedforward,
hidden_dim=args.hidden_dim,
instr_size=args.instr_size,
mask_obs_prob=args.mask_obs_prob,
max_episode_length=max_episode_length,
num_layers=args.num_layers,
).to(device)
if hasattr(model, "film_gen") and model.film_gen is not None:
model.film_gen.build(device)
model.eval()
return model
def find_checkpoint(checkpoint: Path) -> Path:
if checkpoint.is_dir():
candidates = [c for c in checkpoint.rglob("*.pth") if c.name != "best"]
candidates = sorted(candidates, key=lambda p: p.lstat().st_mtime)
assert candidates != [], checkpoint
return candidates[-1]
return checkpoint
if __name__ == "__main__":
args = Arguments().parse_args()
print(args)
log_dir = get_log_dir(args)
log_dir.mkdir(exist_ok=True, parents=True)
print("log dir", log_dir)
# args.save(str(log_dir / "hparams.json"))
torch.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
# load model and args
checkpoint = find_checkpoint(args.checkpoint)
args = copy_args(checkpoint, args)
if checkpoint is None:
raise RuntimeError()
model = load_model(checkpoint, args)
if args.tasks is None or args.gripper_pose is None or args.taskvar_token is None:
raise ValueError()
# load RLBench environment
env = RLBenchEnv(
data_path=args.data_dir,
apply_rgb=True,
apply_pc=True,
headless=args.headless,
apply_cameras=("left_shoulder", "right_shoulder", "wrist"),
)
device = torch.device(args.device)
instruction = load_instructions(args.instructions)
if instruction is None:
raise NotImplementedError()
max_eps_dict = load_episodes()["max_episode_length"]
actioner = Actioner(model=model, instructions=instruction)
for task_str in args.tasks:
for variation in args.variations:
success_rate = env.evaluate(
task_str,
max_episodes=max_eps_dict[task_str],
variation=variation,
num_demos=args.num_episodes,
demos=None,
offset=args.offset,
actioner=actioner,
log_dir=log_dir / task_str if args.save_img else None,
max_tries=args.max_tries,
save_attn=args.attention,
)
print("Testing Success Rate {}: {:.04f}".format(task_str, success_rate))
with FileLock(str(args.output.parent / f"{args.output.name}.lock")):
with open(args.output, "a") as output_id:
output_id.write(
f"{task_str}-{variation}, {checkpoint}, seed={args.seed}, {success_rate}, {log_dir}\n"
)